public Key<T> getKey() { return dependency.getKey(); }
<T> SingleParameterInjector<T> createParameterInjector( final Dependency<T> dependency, final Errors errors) throws ErrorsException { BindingImpl<? extends T> binding = getBindingOrThrow(dependency.getKey(), errors, JitLimitation.NO_JIT); return new SingleParameterInjector<T>(dependency, binding); }
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); }
private void injectValues(Object target, boolean sharedFields, Specification specInstance) throws IllegalAccessException { for (InjectionPoint point : injectionPoints) { if (!(point.getMember() instanceof Field)) throw new GuiceExtensionException("Method injection is not supported; use field injection instead"); Field field = (Field)point.getMember(); if (field.isAnnotationPresent(Shared.class) != sharedFields) continue; Object value = injector.getInstance(point.getDependencies().get(0).getKey()); if (MOCK_UTIL.isMock(value)) { MOCK_UTIL.attachMock(value, specInstance); } field.setAccessible(true); field.set(target, value); } } }
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); }
private 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 %s%n", formatParameter(dependency)); } else { formatSource(formatter, injectionPoint.getMember()); } }
static void formatSource(Formatter formatter, Object source, ElementSource elementSource) { String modules = moduleSourceString(elementSource); if (source instanceof Dependency) { Dependency<?> dependency = (Dependency<?>) source; InjectionPoint injectionPoint = dependency.getInjectionPoint(); if (injectionPoint != null) { formatInjectionPoint(formatter, dependency, injectionPoint, elementSource); } else { formatSource(formatter, dependency.getKey(), elementSource); } } else if (source instanceof InjectionPoint) { formatInjectionPoint(formatter, null, (InjectionPoint) source, elementSource); } else if (source instanceof Class) { formatter.format(" at %s%s%n", StackTraceElements.forType((Class<?>) source), modules); } else if (source instanceof Member) { formatter.format(" at %s%s%n", StackTraceElements.forMember((Member) source), modules); } else if (source instanceof TypeLiteral) { formatter.format(" while locating %s%s%n", source, modules); } else if (source instanceof Key) { Key<?> key = (Key<?>) source; formatter.format(" while locating %s%n", convert(key, elementSource)); } else if (source instanceof Thread) { formatter.format(" in thread %s%n", source); } else { formatter.format(" at %s%s%n", source, modules); } }
<T> Provider<T> getProviderOrThrow(final Dependency<T> dependency, Errors errors) throws ErrorsException { Key<T> key = dependency.getKey(); BindingImpl<? extends T> binding = getBindingOrThrow(key, errors, JitLimitation.NO_JIT); final InternalFactory<? extends T> internalFactory = binding.getInternalFactory(); final Object source = binding.getSource(); return new Provider<T>() { @Override public T get() { InternalContext currentContext = enterContext(); Dependency previous = currentContext.pushDependency(dependency, source); try { T t = internalFactory.get(currentContext, dependency, false); return t; } catch (InternalProvisionException e) { throw e.addSource(dependency).toProvisionException(); } finally { currentContext.popStateAndSetDependency(previous); currentContext.close(); } } @Override public String toString() { return internalFactory.toString(); } }; }
Set<Dependency<?>> deps = getInternalDependencies(binding); for (Dependency dep : deps) { Key<?> depKey = dep.getKey(); InjectionPoint ip = dep.getInjectionPoint(); if (encountered.add(depKey)) { // only check if we haven't looked at this key yet
Preconditions.checkNotNull( context.getDependency(), "internalContext.getDependency()"); Class<?> rawType = dependency.getKey().getTypeLiteral().getRawType();
+ " error.", new Object[] { Messages.formatParameter(dependency), Messages.convert(dependency.getKey()) });
Class<?> expectedType = dependency.getKey().getTypeLiteral().getRawType(); @SuppressWarnings("unchecked") T proxyType =
Class<?> expectedType = dependency.getKey().getTypeLiteral().getRawType();
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()); 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()); 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());
/** 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(); }
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); }
private void addInjectionPointDependencies(InjectionPoint injectionPoint) { // Do not consider dependencies coming from optional injections. if (injectionPoint.isOptional()) { return; } for (Dependency<?> dependency : injectionPoint.getDependencies()) { Key<?> key = dependency.getKey(); bindingsObserved.add(BindingInfo.create(key)); } } }
private void validateDependencies(Set<Key<?>> expectedKeys, Binding<?> binding) { Set<Dependency<?>> dependencies = ((HasDependencies) binding).getDependencies(); Set<Key<?>> actualKeys = new HashSet<>(); for (Dependency<?> dependency : dependencies) { actualKeys.add(dependency.getKey()); } assertEquals(expectedKeys, actualKeys); }