/** Returns the dependencies from the given injection points. */ public static Set<Dependency<?>> forInjectionPoints(Set<InjectionPoint> injectionPoints) { List<Dependency<?>> dependencies = Lists.newArrayList(); for (InjectionPoint injectionPoint : injectionPoints) { dependencies.addAll(injectionPoint.getDependencies()); } return ImmutableSet.copyOf(dependencies); }
public AbstractInjectionProvider(Key<T> key) { this.key = key; constructorInjectionPoint = InjectionPoint.forConstructorOf(key.getTypeLiteral()); Set<Dependency<?>> dependencyBuilder = new HashSet<Dependency<?>>(); dependencyBuilder.addAll(constructorInjectionPoint.getDependencies()); for (InjectionPoint injectionPoint : InjectionPoint.forInstanceMethodsAndFields(key.getTypeLiteral())) { dependencyBuilder.addAll(injectionPoint.getDependencies()); } this.dependencies = Collections.unmodifiableSet(dependencyBuilder); }
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); }
/** * Returns a Guice-friendly {@code com.google.inject.Provider} for the given JSR-330 {@code * javax.inject.Provider}. The converse method is unnecessary, since Guice providers directly * implement the JSR-330 interface. * * @since 3.0 */ public static <T> Provider<T> guicify(javax.inject.Provider<T> provider) { if (provider instanceof Provider) { return (Provider<T>) provider; } final javax.inject.Provider<T> delegate = checkNotNull(provider, "provider"); // Ensure that we inject all injection points from the delegate provider. Set<InjectionPoint> injectionPoints = InjectionPoint.forInstanceMethodsAndFields(provider.getClass()); if (injectionPoints.isEmpty()) { return new GuicifiedProvider<T>(delegate); } else { Set<Dependency<?>> mutableDeps = Sets.newHashSet(); for (InjectionPoint ip : injectionPoints) { mutableDeps.addAll(ip.getDependencies()); } final Set<Dependency<?>> dependencies = ImmutableSet.copyOf(mutableDeps); return new GuicifiedProviderWithDependencies<T>(dependencies, delegate); } }
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); }
SingleMethodInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors) throws ErrorsException { this.injectionPoint = injectionPoint; final Method method = (Method) injectionPoint.getMember(); methodInvoker = createMethodInvoker(method); parameterInjectors = injector.getParametersInjectors(injectionPoint.getDependencies(), errors); }
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()); } }
private <T> ProviderMethod<T> createProviderMethod( Binder binder, Method method, Annotation annotation) { binder = binder.withSource(method); Errors errors = new Errors(method); // prepare the parameter providers InjectionPoint point = InjectionPoint.forMethod(method, typeLiteral); @SuppressWarnings("unchecked") // Define T as the method's return type. TypeLiteral<T> returnType = (TypeLiteral<T>) typeLiteral.getReturnType(method); Key<T> key = getKey(errors, returnType, method, method.getAnnotations()); try { key = scanner.prepareMethod(binder, annotation, key, point); } catch (Throwable t) { binder.addError(t); } Class<? extends Annotation> scopeAnnotation = Annotations.findScopeAnnotation(errors, method.getAnnotations()); for (Message message : errors.getMessages()) { binder.addError(message); } return ProviderMethod.create( key, method, delegate, ImmutableSet.copyOf(point.getDependencies()), scopeAnnotation, skipFastClassGeneration, annotation); }
injector.getParametersInjectors(injectionPoint.getDependencies(), errors);
/** Returns the dependencies from the given injection points. */ public static Set<Dependency<?>> forInjectionPoints(Set<InjectionPoint> injectionPoints) { List<Dependency<?>> dependencies = Lists.newArrayList(); for (InjectionPoint injectionPoint : injectionPoints) { dependencies.addAll(injectionPoint.getDependencies()); } return ImmutableSet.copyOf(dependencies); }
/** Calculates all dependencies required by the implementation and constructor. */ private Set<Dependency<?>> getDependencies( InjectionPoint ctorPoint, TypeLiteral<?> implementation) { ImmutableSet.Builder<Dependency<?>> builder = ImmutableSet.builder(); builder.addAll(ctorPoint.getDependencies()); if (!implementation.getRawType().isInterface()) { for (InjectionPoint ip : InjectionPoint.forInstanceMethodsAndFields(implementation)) { builder.addAll(ip.getDependencies()); } } return builder.build(); }
public AbstractInjectionProvider(Key<T> key) { this.key = key; constructorInjectionPoint = InjectionPoint.forConstructorOf(key.getTypeLiteral()); Set<Dependency<?>> dependencyBuilder = new HashSet<Dependency<?>>(); dependencyBuilder.addAll(constructorInjectionPoint.getDependencies()); for (InjectionPoint injectionPoint : InjectionPoint.forInstanceMethodsAndFields(key.getTypeLiteral())) { dependencyBuilder.addAll(injectionPoint.getDependencies()); } this.dependencies = Collections.unmodifiableSet(dependencyBuilder); }
static Set<Key<?>> getUnboundDirectDependencies(Set<Dependency<?>> dependencies, Set<Key<?>> boundKeys) { Set<Key<?>> unboundKeys = new HashSet<>(); for (Dependency<?> dep : dependencies) { for (Dependency<?> dep2 : dep.getInjectionPoint().getDependencies()) { if (!boundKeys.contains(dep2.getKey())) { unboundKeys.add(dep2.getKey()); } } } return unboundKeys; }
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); }
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); factory = injector.getInternalFactory(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 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)); } } }
SingleMethodInjector(InjectorImpl injector, InjectionPoint injectionPoint, Errors errors) throws ErrorsException { this.injectionPoint = injectionPoint; final Method method = (Method) injectionPoint.getMember(); methodInvoker = createMethodInvoker(method); parameterInjectors = injector.getParametersInjectors(injectionPoint.getDependencies(), errors); }
private void addInjectionPointDependencies(InjectionPoint injectionPoint, Set<Key<?>> keysObserved, Set<Key<?>> keysNeeded) { // Do not consider dependencies coming from optional injections if (injectionPoint.isOptional()) { return; } for (Dependency<?> dependency : injectionPoint.getDependencies()) { Key<?> key = dependency.getKey(); addKeyDependency(key, keysObserved, keysNeeded); } }