Refine search
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); }
boolean failIfNotExplicit) throws ErrorsException { int numErrors = errors.size(); ? key.getTypeLiteral().getRawType() : (Class) constructorInjector.getDeclaringType().getRawType(); errors.missingImplementationWithHint(key, injector); errors.cannotInjectInnerClass(rawType); constructorInjector = InjectionPoint.forConstructorOf(key.getTypeLiteral()); if (failIfNotExplicit && !hasAtInject((Constructor) constructorInjector.getMember())) { errors.atInjectRequired(rawType); throw errors.merge(e.getErrorMessages()).toException(); Class<?> annotatedType = constructorInjector.getMember().getDeclaringClass(); Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType); if (scopeAnnotation != null) {
@Override public <S extends T> ScopedBindingBuilder toConstructor( Constructor<S> constructor, TypeLiteral<? extends S> type) { checkNotNull(constructor, "constructor"); checkNotNull(type, "type"); checkNotTargetted(); BindingImpl<T> base = getBinding(); Set<InjectionPoint> injectionPoints; try { injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type); } catch (ConfigurationException e) { copyErrorsToBinder(e); injectionPoints = e.getPartialValue(); } try { InjectionPoint constructorPoint = InjectionPoint.forConstructor(constructor, type); setBinding( new ConstructorBindingImpl<T>( base.getKey(), base.getSource(), base.getScoping(), constructorPoint, injectionPoints)); } catch (ConfigurationException e) { copyErrorsToBinder(e); } return this; }
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); }
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); }
@SuppressWarnings("unchecked") private <T> ScopedBindingBuilder bindNewSpyProvider(Key<T> key) { TypeLiteral<T> type = key.getTypeLiteral(); InjectionPoint constructorInjectionPoint = InjectionPoint.forConstructorOf(type); Key<T> relayingKey = Key.get(type, JukitoInternal.class); bind(relayingKey).toConstructor((Constructor<T>) constructorInjectionPoint.getMember()); return bind(key).toProvider(new SpyProvider<T>(getProvider(relayingKey), relayingKey)); }
@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; fail("Did not expect method dependency with key " + dependency.getKey()); } else if (dependency.getInjectionPoint().getMember() instanceof Field) { if (dependency.getKey().equals(keyF1)) { foundF1 = true;
Method method, Key<?> returnType, TypeLiteral<T> implementation, List<Key<?>> paramList) throws ErrorsException { Errors errors = new Errors(method); if (returnType.getTypeLiteral().equals(implementation)) { errors = errors.withSource(implementation); } else { errors = errors.withSource(returnType).withSource(implementation); Class<?> rawType = implementation.getRawType(); if (Modifier.isInterface(rawType.getModifiers())) { errors.addMessage( return InjectionPoint.forConstructorOf(implementation); } catch (ConfigurationException e) { errors.merge(e.getErrorMessages()); throw errors.toException(); InjectionPoint.forConstructor( (Constructor<? super T>) matchingConstructor, implementation); return ip;
NodeId.newInstanceId(Key.get(IA.class)), null, wrapper.value, a2Node.getId(), stringNode.getId(), InjectionPoint.forConstructor(A2.class.getConstructor(Provider.class))), new DependencyEdge(a2ProviderNode.getId(), a2Node.getId(), null)); assertEquals("wrong nodes", expectedNodes, grapher.nodes);
private Set<Key<?>> getDependencies(Key<?> clazz) { Set<Key<?>> dependencies = new HashSet<>(); if (clazz.getTypeLiteral().getRawType().isInterface()) { dependencies.add(clazz); return dependencies; } try { dependencies.addAll(getDependencies(InjectionPoint.forConstructorOf(clazz.getTypeLiteral()))); } catch (ConfigurationException e) { // nothing to do } for (InjectionPoint point : InjectionPoint.forInstanceMethodsAndFields(clazz.getTypeLiteral())) { dependencies.addAll(getDependencies(point)); } for (InjectionPoint point : InjectionPoint.forStaticMethodsAndFields(clazz.getTypeLiteral())) { dependencies.addAll(getDependencies(point)); } return dependencies; }
/** * Returns all static method and field injection points on {@code type}. * * @return a possibly empty set of injection points. The set has a specified iteration order. All * fields are returned and then all methods. Within the fields, supertype fields are returned * before subtype fields. Similarly, supertype methods are returned before subtype methods. * @throws ConfigurationException if there is a malformed injection point on {@code type}, such as * a field with multiple binding annotations. The exception's {@link * ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>} of * the valid injection points. */ public static Set<InjectionPoint> forStaticMethodsAndFields(Class<?> type) { return forStaticMethodsAndFields(TypeLiteral.get(type)); }
private List<Class<? extends Module>> getIncludeList() { // Look for @Modules(includes={..}) Builder<Class<? extends Module>> builder = ImmutableList.<Class<? extends Module>>builder(); if (type != null) { Modules annot = type.getAnnotation(Modules.class); if (annot != null && annot.include() != null) { builder.add(annot.include()); } // Look for injected modules for (Dependency<?> dep : InjectionPoint.forConstructorOf(type).getDependencies()) { Class<?> depType = dep.getKey().getTypeLiteral().getRawType(); if (Module.class.isAssignableFrom(depType)) { builder.add((Class<? extends Module>) depType); } } } return builder.build(); }
/** * 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; }
/** * Returns all instance method and field injection points on {@code type}. * * @return a possibly empty set of injection points. The set has a specified iteration order. All * fields are returned and then all methods. Within the fields, supertype fields are returned * before subtype fields. Similarly, supertype methods are returned before subtype methods. * @throws ConfigurationException if there is a malformed injection point on {@code type}, such as * a field with multiple binding annotations. The exception's {@link * ConfigurationException#getPartialValue() partial value} is a {@code Set<InjectionPoint>} of * the valid injection points. */ public static Set<InjectionPoint> forInstanceMethodsAndFields(Class<?> type) { return forInstanceMethodsAndFields(TypeLiteral.get(type)); }
/** * Returns a new injection point for the injectable constructor of {@code type}. * * @param type a concrete type with exactly one constructor annotated {@literal @}{@link Inject}, * or a no-arguments constructor that is not private. * @throws ConfigurationException if there is no injectable constructor, more than one injectable * constructor, or if parameters of the injectable constructor are malformed, such as a * parameter with multiple binding annotations. */ public static InjectionPoint forConstructorOf(Class<?> type) { return forConstructorOf(TypeLiteral.get(type)); }
/** 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(); }
static Set<Key<?>> getUnboundDirectDependencies(TypeLiteral<?> type, Set<Key<?>> boundKeys) { if (type.getRawType().isInterface()) { return Collections.emptySet(); } Set<Key<?>> keys = new HashSet<>(); keys.addAll(getUnboundDirectDependencies(InjectionPoint.forConstructorOf(type), boundKeys)); keys.addAll(getUnboundDirectDependencies(InjectionPoint.forInstanceMethodsAndFields(type), boundKeys)); return keys; }
/** Creates a new members injector and attaches both injection listeners and method aspects. */ private <T> MembersInjectorImpl<T> createWithListeners(TypeLiteral<T> type, Errors errors) throws ErrorsException { int numErrorsBefore = errors.size(); Set<InjectionPoint> injectionPoints; try { injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type); } catch (ConfigurationException e) { errors.merge(e.getErrorMessages()); injectionPoints = e.getPartialValue(); } ImmutableList<SingleMemberInjector> injectors = getInjectors(injectionPoints, errors); errors.throwIfNewErrors(numErrorsBefore); EncounterImpl<T> encounter = new EncounterImpl<>(errors, injector.lookups); Set<TypeListener> alreadySeenListeners = Sets.newHashSet(); for (TypeListenerBinding binding : typeListenerBindings) { TypeListener typeListener = binding.getListener(); if (!alreadySeenListeners.contains(typeListener) && binding.getTypeMatcher().matches(type)) { alreadySeenListeners.add(typeListener); try { typeListener.hear(type, encounter); } catch (RuntimeException e) { errors.errorNotifyingTypeListener(binding, type, e); } } } encounter.invalidate(); errors.throwIfNewErrors(numErrorsBefore); return new MembersInjectorImpl<T>(injector, type, encounter, injectors); }
private boolean canBeInjected(TypeLiteral<?> type) { Class<?> rawType = type.getRawType(); if (isPrimitive(rawType) || isCoreGuiceType(rawType) || !isInstantiable(rawType)) { return false; } try { InjectionPoint.forConstructorOf(type); return true; } catch (ConfigurationException e) { return false; } }
@Override public Class<?> visit( final ConstructorBinding<?> binding ) { return binding.getConstructor().getDeclaringType().getRawType(); }