/** * 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 the instance methods and fields of {@code instance} that will be injected to fulfill * this request. * * @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 the class of {@code * instance}, 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 Set<InjectionPoint> getInjectionPoints() throws ConfigurationException { return InjectionPoint.forInstanceMethodsAndFields(instance.getClass()); }
public GuiceInterceptor(SpecInfo spec, Set<Class<? extends Module>> moduleClasses) { this.moduleClasses = moduleClasses; injectionPoints = InjectionPoint.forInstanceMethodsAndFields(spec.getReflection()); }
/** 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()); }
/** * 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); } }
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); }
@Override public BindingBuilder<T> toProvider(javax.inject.Provider<? extends T> provider) { checkNotNull(provider, "provider"); checkNotTargetted(); // lookup the injection points, adding any errors to the binder's errors list Set<InjectionPoint> injectionPoints; try { injectionPoints = InjectionPoint.forInstanceMethodsAndFields(provider.getClass()); } catch (ConfigurationException e) { copyErrorsToBinder(e); injectionPoints = e.getPartialValue(); } BindingImpl<T> base = getBinding(); setBinding( new ProviderInstanceBindingImpl<T>( base.getSource(), base.getKey(), base.getScoping(), injectionPoints, provider)); return this; }
@Override public void toInstance(T instance) { checkNotTargetted(); // lookup the injection points, adding any errors to the binder's errors list Set<InjectionPoint> injectionPoints; if (instance != null) { try { injectionPoints = InjectionPoint.forInstanceMethodsAndFields(instance.getClass()); } catch (ConfigurationException e) { copyErrorsToBinder(e); injectionPoints = e.getPartialValue(); } } else { binder.addError(BINDING_TO_NULL); injectionPoints = ImmutableSet.of(); } BindingImpl<T> base = getBinding(); setBinding( new InstanceBindingImpl<T>( base.getSource(), base.getKey(), Scoping.EAGER_SINGLETON, injectionPoints, instance)); }
@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; }
/** 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); }
public GuiceyInterceptor(final SpecInfo spec, final ExternalRuleAdapter externalRuleAdapter, final List<GuiceyConfigurationHook> hooks) { this.externalRuleAdapter = externalRuleAdapter; this.hooks = hooks; injectionPoints = InjectionPoint.forInstanceMethodsAndFields(spec.getReflection()); }
public GuiceyInterceptor(final SpecInfo spec, final ExternalRuleAdapter externalRuleAdapter, final List<GuiceyConfigurationHook> hooks) { this.externalRuleAdapter = externalRuleAdapter; this.hooks = hooks; injectionPoints = InjectionPoint.forInstanceMethodsAndFields(spec.getReflection()); }
public GuiceInterceptor(SpecInfo spec, Set<Class<? extends Module>> moduleClasses) { this.moduleClasses = moduleClasses; injectionPoints = InjectionPoint.forInstanceMethodsAndFields(spec.getReflection()); }
/** 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(); }
/** 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(); }
/** 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; }
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); }
/** 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(); }
/** 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(); }