private RecordingBinder(Stage stage) { this.stage = stage; this.modules = Maps.newLinkedHashMap(); this.scanners = Sets.newLinkedHashSet(); this.elements = Lists.newArrayList(); this.source = null; this.sourceProvider = SourceProvider.DEFAULT_INSTANCE.plusSkippedClasses( Elements.class, RecordingBinder.class, AbstractModule.class, ConstantBindingBuilderImpl.class, AbstractBindingBuilder.class, BindingBuilder.class); this.parent = null; this.privateElements = null; this.privateBinders = Lists.newArrayList(); }
/** Creates a recording binder that's backed by {@code prototype}. */ private RecordingBinder( RecordingBinder prototype, Object source, SourceProvider sourceProvider) { checkArgument(source == null ^ sourceProvider == null); this.stage = prototype.stage; this.modules = prototype.modules; this.elements = prototype.elements; this.scanners = prototype.scanners; this.source = source; this.moduleSource = prototype.moduleSource; this.sourceProvider = sourceProvider; this.parent = prototype.parent; this.privateElements = prototype.privateElements; this.privateBinders = prototype.privateBinders; }
private <T> BindingImpl<MembersInjector<T>> createMembersInjectorBinding( Key<MembersInjector<T>> key, Errors errors) throws ErrorsException { Type membersInjectorType = key.getTypeLiteral().getType(); if (!(membersInjectorType instanceof ParameterizedType)) { throw errors.cannotInjectRawMembersInjector().toException(); } @SuppressWarnings("unchecked") // safe because T came from Key<MembersInjector<T>> TypeLiteral<T> instanceType = (TypeLiteral<T>) TypeLiteral.get(((ParameterizedType) membersInjectorType).getActualTypeArguments()[0]); MembersInjector<T> membersInjector = membersInjectorStore.get(instanceType, errors); InternalFactory<MembersInjector<T>> factory = new ConstantFactory<MembersInjector<T>>(Initializables.of(membersInjector)); return new InstanceBindingImpl<MembersInjector<T>>( this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), membersInjector); }
public void add(Key<?> key, State state, Object source) { if (backingMap == null) { backingMap = Maps.newHashMap(); } // if it's an instanceof Class, it was a JIT binding, which we don't // want to retain. if (source instanceof Class || source == SourceProvider.UNKNOWN_SOURCE) { source = null; } Multiset<Object> sources = backingMap.get(key); if (sources == null) { sources = LinkedHashMultiset.create(); backingMap.put(key, sources); } Object convertedSource = Errors.convert(source); sources.add(convertedSource); // Avoid all the extra work if we can. if (state.parent() != State.NONE) { Set<KeyAndSource> keyAndSources = evictionCache.getIfPresent(state); if (keyAndSources == null) { evictionCache.put(state, keyAndSources = Sets.newHashSet()); } keyAndSources.add(new KeyAndSource(key, convertedSource)); } }
public void testSourceFormatting() { // Note that the duplicate source gets dropped as well as the unknown source assertThat( InternalProvisionException.create("An error") .addSource("Source1") .addSource(SourceProvider.UNKNOWN_SOURCE) .addSource("Source2") .addSource("Source2") .toProvisionException() .getMessage()) .isEqualTo( "" + "Unable to provision, see the following errors:\n" + "\n" + "1) An error\n" + " at Source1\n" + " at Source2\n" + "\n" + "1 error"); }
private <T> BindingImpl<MembersInjector<T>> createMembersInjectorBinding( Key<MembersInjector<T>> key, Errors errors) throws ErrorsException { Type membersInjectorType = key.getTypeLiteral().getType(); if (!(membersInjectorType instanceof ParameterizedType)) { throw errors.cannotInjectRawMembersInjector().toException(); } @SuppressWarnings("unchecked") // safe because T came from Key<MembersInjector<T>> TypeLiteral<T> instanceType = (TypeLiteral<T>) TypeLiteral.get( ((ParameterizedType) membersInjectorType).getActualTypeArguments()[0]); MembersInjector<T> membersInjector = membersInjectorStore.get(instanceType, errors); InternalFactory<MembersInjector<T>> factory = new ConstantFactory<MembersInjector<T>>( Initializables.of(membersInjector)); return new InstanceBindingImpl<MembersInjector<T>>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), membersInjector); }
/** * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's * a bit awkward because we have to pull out the inner type in the type literal. */ private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding( Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException { Type typeLiteralType = key.getTypeLiteral().getType(); if (!(typeLiteralType instanceof ParameterizedType)) { throw errors.cannotInjectRawTypeLiteral().toException(); } ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType; Type innerType = parameterizedType.getActualTypeArguments()[0]; // this is unforunate. We don't support building TypeLiterals for type variable like 'T'. If // this proves problematic, we can probably fix TypeLiteral to support type variables if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) { throw errors.cannotInjectTypeLiteralOf(innerType).toException(); } @SuppressWarnings("unchecked") // by definition, innerType == T, so this is safe TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType); InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<TypeLiteral<T>>( Initializables.of(value)); return new InstanceBindingImpl<TypeLiteral<T>>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), value); }
public void add(Key<?> key, Object source) { if (backingSet == null) { backingSet = Maps.newHashMap(); } // if it's an instanceof Class, it was a JIT binding, which we don't // want to retain. if (source instanceof Class || source == SourceProvider.UNKNOWN_SOURCE) { source = null; } String k = key.toString(); Set<Object> sources = backingSet.get(k); if (sources == null) { sources = Sets.newLinkedHashSet(); backingSet.put(k, sources); } sources.add(Errors.convert(source)); }
@Override public RecordingBinder skipSources(Class... classesToSkip) { // if a source is specified explicitly, we don't need to skip sources if (source != null) { return this; } SourceProvider newSourceProvider = sourceProvider.plusSkippedClasses(classesToSkip); return new RecordingBinder(this, null, newSourceProvider); }
/** * Prepends the given {@code source} to the stack of binding sources for the errors reported in * this exception. * * <p>See {@link Errors#withSource(Object)} * * <p>It is expected that this method is called as the exception propagates up the stack. * * @param source * @return {@code this} */ InternalProvisionException addSource(Object source) { if (source == SourceProvider.UNKNOWN_SOURCE) { return this; } int sz = sourcesToPrepend.size(); if (sz > 0 && sourcesToPrepend.get(sz - 1) == source) { // This is for when there are two identical sources added in a row. This behavior is copied // from Errors.withSource where it can happen when an constructor/provider method throws an // exception return this; } sourcesToPrepend.add(source); return this; }
/** * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's * a bit awkward because we have to pull out the inner type in the type literal. */ private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding( Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException { Type typeLiteralType = key.getTypeLiteral().getType(); if (!(typeLiteralType instanceof ParameterizedType)) { throw errors.cannotInjectRawTypeLiteral().toException(); } ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType; Type innerType = parameterizedType.getActualTypeArguments()[0]; // this is unforunate. We don't support building TypeLiterals for type variable like 'T'. If // this proves problematic, we can probably fix TypeLiteral to support type variables if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) { throw errors.cannotInjectTypeLiteralOf(innerType).toException(); } @SuppressWarnings("unchecked") // by definition, innerType == T, so this is safe TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType); InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<TypeLiteral<T>>(Initializables.of(value)); return new InstanceBindingImpl<TypeLiteral<T>>( this, key, SourceProvider.UNKNOWN_SOURCE, factory, ImmutableSet.<InjectionPoint>of(), value); }
/** * The Injector is a special case because we allow both parent and child injectors to both have a * binding for that key. */ private static void bindInjector(InjectorImpl injector) { Key<Injector> key = Key.get(Injector.class); InjectorFactory injectorFactory = new InjectorFactory(injector); injector.state.putBinding( key, new ProviderInstanceBindingImpl<Injector>( injector, key, SourceProvider.UNKNOWN_SOURCE, injectorFactory, Scoping.UNSCOPED, injectorFactory, ImmutableSet.<InjectionPoint>of())); }
/** * The Logger is a special case because it knows the injection point of the injected member. It's * the only binding that does this. */ private static void bindLogger(InjectorImpl injector) { Key<Logger> key = Key.get(Logger.class); LoggerFactory loggerFactory = new LoggerFactory(); injector.state.putBinding( key, new ProviderInstanceBindingImpl<Logger>( injector, key, SourceProvider.UNKNOWN_SOURCE, loggerFactory, Scoping.UNSCOPED, loggerFactory, ImmutableSet.<InjectionPoint>of())); }
private static void bindStage(InjectorImpl injector, Stage stage) { Key<Stage> key = Key.get(Stage.class); InstanceBindingImpl<Stage> stageBinding = new InstanceBindingImpl<Stage>( injector, key, SourceProvider.UNKNOWN_SOURCE, new ConstantFactory<Stage>(Initializables.of(stage)), ImmutableSet.<InjectionPoint>of(), stage); injector.state.putBinding(key, stageBinding); }
private List<Object> getSources() { List<Object> sources = Lists.newArrayList(); for (Errors e = this; e != null; e = e.parent) { if (e.source != SourceProvider.UNKNOWN_SOURCE) { sources.add(0, e.source); } } return sources; }
private RecordingBinder(Stage stage) { this.stage = stage; this.modules = Sets.newHashSet(); this.elements = Lists.newArrayList(); this.rehashables = Lists.newArrayList(); this.source = null; this.sourceProvider = SourceProvider.DEFAULT_INSTANCE.plusSkippedClasses( Elements.class, RecordingBinder.class, AbstractModule.class, ConstantBindingBuilderImpl.class, AbstractBindingBuilder.class, BindingBuilder.class); this.parent = null; this.privateElements = null; }
/** Creates a recording binder that's backed by {@code prototype}. */ private RecordingBinder( RecordingBinder prototype, Object source, SourceProvider sourceProvider) { checkArgument(source == null ^ sourceProvider == null); this.stage = prototype.stage; this.modules = prototype.modules; this.elements = prototype.elements; this.rehashables = prototype.rehashables; this.source = source; this.moduleSource = prototype.moduleSource; this.sourceProvider = sourceProvider; this.parent = prototype.parent; this.privateElements = prototype.privateElements; }
public RecordingBinder skipSources(Class... classesToSkip) { // if a source is specified explicitly, we don't need to skip sources if (source != null) { return this; } SourceProvider newSourceProvider = sourceProvider.plusSkippedClasses(classesToSkip); return new RecordingBinder(this, null, newSourceProvider); }
/** * The Injector is a special case because we allow both parent and child injectors to both have * a binding for that key. */ private static void bindInjector(InjectorImpl injector) { Key<Injector> key = Key.get(Injector.class); InjectorFactory injectorFactory = new InjectorFactory(injector); injector.state.putBinding(key, new ProviderInstanceBindingImpl<Injector>(injector, key, SourceProvider.UNKNOWN_SOURCE, injectorFactory, Scoping.UNSCOPED, injectorFactory, ImmutableSet.<InjectionPoint>of())); }
/** * The Logger is a special case because it knows the injection point of the injected member. It's * the only binding that does this. */ private static void bindLogger(InjectorImpl injector) { Key<Logger> key = Key.get(Logger.class); LoggerFactory loggerFactory = new LoggerFactory(); injector.state.putBinding(key, new ProviderInstanceBindingImpl<Logger>(injector, key, SourceProvider.UNKNOWN_SOURCE, loggerFactory, Scoping.UNSCOPED, loggerFactory, ImmutableSet.<InjectionPoint>of())); }
public void add(Key<?> key, State state, Object source) { if (backingSet == null) { backingSet = Maps.newHashMap(); } // if it's an instanceof Class, it was a JIT binding, which we don't // want to retain. if (source instanceof Class || source == SourceProvider.UNKNOWN_SOURCE) { source = null; } Object mapKey = toMapKey(key); Multiset<Object> sources = backingSet.get(mapKey); if (sources == null) { sources = LinkedHashMultiset.create(); backingSet.put(mapKey, sources); } Object convertedSource = Errors.convert(source); sources.add(convertedSource); // Avoid all the extra work if we can. if (state.parent() != State.NONE) { Set<KeyAndSource> keyAndSources = evictionCache.getIfPresent(state); if (keyAndSources == null) { evictionCache.put(state, keyAndSources = Sets.newHashSet()); } keyAndSources.add(new KeyAndSource(mapKey, convertedSource)); } }
public List<Object> getSources() { List<Object> sources = Lists.newArrayList(); for (Errors e = this; e != null; e = e.parent) { if (e.source != SourceProvider.UNKNOWN_SOURCE) { sources.add(0, e.source); } } return sources; }
private RecordingBinder(Stage stage) { this.stage = stage; this.modules = Sets.newHashSet(); this.elements = Lists.newArrayList(); this.source = null; this.sourceProvider = SourceProvider.DEFAULT_INSTANCE.plusSkippedClasses( Elements.class, RecordingBinder.class, AbstractModule.class, ConstantBindingBuilderImpl.class, AbstractBindingBuilder.class, BindingBuilder.class); this.parent = null; this.privateElements = null; }
/** Creates a recording binder that's backed by {@code prototype}. */ private RecordingBinder( RecordingBinder prototype, Object source, SourceProvider sourceProvider) { checkArgument(source == null ^ sourceProvider == null); this.stage = prototype.stage; this.modules = prototype.modules; this.elements = prototype.elements; this.source = source; this.sourceProvider = sourceProvider; this.parent = prototype.parent; this.privateElements = prototype.privateElements; }
@Override public String getSource() { return sources.isEmpty() ? SourceProvider.UNKNOWN_SOURCE.toString() : Errors.convert(sources.get(sources.size() - 1)).toString(); }
private static void internalConvertToTypes( InjectorImpl injector, Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { injector.state.addConverter( new TypeConverterBinding(SourceProvider.UNKNOWN_SOURCE, typeMatcher, converter)); }
@Override public void configure(Binder binder) { binder = binder.withSource(SourceProvider.UNKNOWN_SOURCE); binder.bindScope(Singleton.class, SINGLETON); binder.bindScope(javax.inject.Singleton.class, SINGLETON); }
public Errors() { this.root = this; this.parent = null; this.source = SourceProvider.UNKNOWN_SOURCE; }
/** Returns an instance that uses {@code source} as a reference point for newly added errors. */ public Errors withSource(Object source) { return source == this.source || source == SourceProvider.UNKNOWN_SOURCE ? this : new Errors(this, source); }
public String getSource() { return sources.isEmpty() ? SourceProvider.UNKNOWN_SOURCE.toString() : Errors.convert(sources.get(sources.size() - 1)).toString(); }
private static void internalConvertToTypes(InjectorImpl injector, Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) { injector.state.addConverter( new TypeConverterBinding(SourceProvider.UNKNOWN_SOURCE, typeMatcher, converter)); }
public void configure(Binder binder) { binder = binder.withSource(SourceProvider.UNKNOWN_SOURCE); binder.bindScope(Singleton.class, SINGLETON); binder.bindScope(javax.inject.Singleton.class, SINGLETON); }