Refine search
public SingleFieldInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors) throws ErrorsException { this.injectionPoint = injectionPoint; this.field = (Field) injectionPoint.getMember(); this.dependency = injectionPoint.getDependencies().get(0); // Ewwwww... field.setAccessible(true); binding = injector.getBindingOrThrow(dependency.getKey(), errors, JitLimitation.NO_JIT); }
@Override public Set<Dependency<?>> getDependencies() { // We depend on Provider<T>, not T directly. This is an important distinction // for dependency analysis tools that short-circuit on providers. Key<?> providerKey = getKey().ofType(Types.providerOf(getKey().getTypeLiteral().getType())); return ImmutableSet.<Dependency<?>>of(Dependency.get(providerKey)); }
throws InternalProvisionException { if (dependency.getInjectionPoint().getMember() instanceof Method) { Method annotated = (Method) dependency.getInjectionPoint().getMember(); if (annotated.isAnnotationPresent(Provides.class)) { switch (InternalFlags.getNullableProvidesOption()) { + " error.", new Object[] { Messages.formatParameter(dependency), Messages.convert(dependency.getKey()) }); (dependency.getParameterIndex() != -1) ? Messages.formatParameter(dependency) : StackTraceElements.forMember(dependency.getInjectionPoint().getMember());
static String formatParameter(Dependency<?> dependency) { int ordinal = dependency.getParameterIndex() + 1; return String.format( "the %s%s parameter of %s", ordinal, getOrdinalSuffix(ordinal), StackTraceElements.forMember(dependency.getInjectionPoint().getMember())); }
public T get() { Constructor<T> constructor = getConstructor(); Object[] params = new Object[constructor.getParameterTypes().length]; for (Dependency<?> dependency : constructorInjectionPoint.getDependencies()) { params[dependency.getParameterIndex()] = injector.getInstance(dependency.getKey()); } T t; try { t = constructor.newInstance(params); } catch (Exception e) { throw new ProvisionException("Could not instantiate " + key + "", e); } injector.injectMembers(t); return postProcess(t); }
@Override public Set<Dependency<?>> getDependencies() { return instance instanceof HasDependencies ? ImmutableSet.copyOf(((HasDependencies) instance).getDependencies()) : Dependency.forInjectionPoints(injectionPoints); }
@Override protected void doInitialize(InjectorImpl injector, Errors errors) { ImmutableMap.Builder<K, Provider<V>> mapOfProvidersBuilder = ImmutableMap.builder(); ImmutableSet.Builder<Dependency<?>> dependenciesBuilder = ImmutableSet.builder(); for (Map.Entry<K, Binding<V>> entry : bindingSelection.getMapBindings().entrySet()) { mapOfProvidersBuilder.put(entry.getKey(), entry.getValue().getProvider()); dependenciesBuilder.add(Dependency.get(getKeyOfProvider(entry.getValue().getKey()))); } mapOfProviders = mapOfProvidersBuilder.build(); dependencies = dependenciesBuilder.build(); }
@Override protected void doInitialize(InjectorImpl injector, Errors errors) throws ErrorsException { @SuppressWarnings("unchecked") K[] keysArray = (K[]) new Object[bindingSelection.getMapBindings().size()]; keys = keysArray; ImmutableSet.Builder<Dependency<?>> dependenciesBuilder = ImmutableSet.builder(); int i = 0; for (Map.Entry<K, Binding<V>> entry : bindingSelection.getMapBindings().entrySet()) { dependenciesBuilder.add(Dependency.get(entry.getValue().getKey())); keys[i] = entry.getKey(); i++; } ImmutableSet<Dependency<?>> localDependencies = dependenciesBuilder.build(); dependencies = localDependencies; List<Dependency<?>> dependenciesList = localDependencies.asList(); // We know the type because we built up our own sets of dependencies, it's just // that the interface uses a "?" generic @SuppressWarnings("unchecked") SingleParameterInjector<V>[] typedInjectors = (SingleParameterInjector<V>[]) injector.getParametersInjectors(dependenciesList, errors); injectors = typedInjectors; }
/** Returns a set of dependencies that can be iterated over to clean up stray JIT bindings. */ Set<Dependency<?>> getInternalDependencies() { ImmutableSet.Builder<InjectionPoint> builder = ImmutableSet.builder(); if (factory.constructorInjector == null) { builder.add(constructorInjectionPoint); // If the below throws, it's OK -- we just ignore those dependencies, because no one // could have used them anyway. try { builder.addAll( InjectionPoint.forInstanceMethodsAndFields( constructorInjectionPoint.getDeclaringType())); } catch (ConfigurationException ignored) { } } else { builder.add(getConstructor()).addAll(getInjectableMembers()); } return Dependency.forInjectionPoints(builder.build()); }
/** Return all non-assisted dependencies. */ private Set<Dependency<?>> removeAssistedDeps(Set<Dependency<?>> deps) { ImmutableSet.Builder<Dependency<?>> builder = ImmutableSet.builder(); for (Dependency<?> dep : deps) { Class<?> annotationType = dep.getKey().getAnnotationType(); if (annotationType == null || !annotationType.equals(Assisted.class)) { builder.add(dep); } } return builder.build(); }
@Override // from |com.google.inject.spi.HasDependencies| public Set<Dependency<?>> getDependencies() { // get() depends on other modules final HashSet<Dependency<?>> built = new HashSet<>(); built.add(Dependency.get(Key.get(ModelManager.class))); built.add(Dependency.get(Key.get(BufferAllocator.class))); return Collections.unmodifiableSet(built); }
@SuppressWarnings("unchecked") public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked) throws ErrorsException { checkState(constructorInjector != null, "Constructor not ready"); if (failIfNotLinked && !linked) { throw errors.jitDisabled(key).toException(); } // This may not actually be safe because it could return a super type of T (if that's all the // client needs), but it should be OK in practice thanks to the wonders of erasure. return (T) constructorInjector.construct(errors, context, dependency.getKey().getTypeLiteral().getRawType(), provisionCallback); } }
@Test public void testGetDependencies() throws Exception { AbstractInjectionProvider<SomeInjectedClass> underTest = new AbstractInjectionProvider<SomeInjectedClass>(Key.get(SomeInjectedClass.class)); if (dependency.getInjectionPoint().getMember() instanceof Constructor) { if (dependency.getParameterIndex() == 0 && dependency.getKey().equals(keyC1)) { foundC1 = true; } else if (dependency.getParameterIndex() == 1 && dependency.getKey().equals(keyC2)) { foundC2 = true; } else { fail("Did not expect constructor dependency with key " + dependency.getKey() + " at parameter index " + dependency.getParameterIndex()); } else if (dependency.getInjectionPoint().getMember() instanceof Method) { if (dependency.getKey().equals(keyV1)) { foundV1 = true; } else if (dependency.getKey().equals(keyV2)) { foundV2 = true; } else { fail("Did not expect method dependency with key " + dependency.getKey()); } else if (dependency.getInjectionPoint().getMember() instanceof Field) { if (dependency.getKey().equals(keyF1)) { foundF1 = true; } else { fail("Did not expect field dependency with key " + dependency.getKey()); fail("Did not expect dependency with key " + dependency.getKey());
context.getInjectorOptions(), dependency.getKey().getTypeLiteral().getRawType()); if (context.getInjectorOptions().disableCircularProxies) { throw InternalProvisionException.circularDependenciesDisabled( dependency.getKey().getTypeLiteral().getRawType()); } else { return t;
/** * Visit a request to inject the instance fields and methods of an instance. */ @Override public Void visit(InjectionRequest<?> request) { for (InjectionPoint ip : request.getInjectionPoints()) { for (Dependency<?> dep : ip.getDependencies()) { foundKeys.addAll(getUnboundDirectDependencies(dep.getKey().getTypeLiteral(), boundKeys)); } } return null; }
public static void formatInjectionPoint(Formatter formatter, Dependency<?> dependency, InjectionPoint injectionPoint, ElementSource elementSource) { Member member = injectionPoint.getMember(); Class<? extends Member> memberType = Classes.memberType(member); if (memberType == Field.class) { dependency = injectionPoint.getDependencies().get(0); formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource)); formatter.format(" for field at %s%n", StackTraceElements.forMember(member)); } else if (dependency != null) { formatter.format(" while locating %s%n", convert(dependency.getKey(), elementSource)); formatter.format(" for parameter %s at %s%n", dependency.getParameterIndex(), StackTraceElements.forMember(member)); } else { formatSource(formatter, injectionPoint.getMember()); } }
@Override public <T> Provider<T> getProvider(final Key<T> key) { Errors errors = new Errors(key); try { Provider<T> result = getProviderOrThrow(Dependency.get(key), errors); errors.throwIfNewErrors(0); return result; } catch (ErrorsException e) { throw new ConfigurationException(errors.merge(e.getErrors()).getMessages()); } }