use of com.google.inject.spi.InjectionPoint in project roboguice by roboguice.
the class InjectorImpl method cleanup.
/**
* Iterates through the binding's dependencies to clean up any stray bindings that were leftover
* from a failed JIT binding. This is required because the bindings are eagerly &
* optimistically added to allow circular dependency support, so dependencies may pass where they
* should have failed.
*/
private boolean cleanup(BindingImpl<?> binding, Set<Key> encountered) {
boolean bindingFailed = false;
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
BindingImpl depBinding = jitBindings.get(depKey);
if (depBinding != null) {
// if the binding still exists, validate
// if children fail, we fail
boolean failed = cleanup(depBinding, encountered);
if (depBinding instanceof ConstructorBindingImpl) {
ConstructorBindingImpl ctorBinding = (ConstructorBindingImpl) depBinding;
ip = ctorBinding.getInternalConstructor();
if (!ctorBinding.isInitialized()) {
failed = true;
}
}
if (failed) {
removeFailedJitBinding(depBinding, ip);
bindingFailed = true;
}
} else if (state.getExplicitBinding(depKey) == null) {
// ignore keys if they were explicitly bound, but if neither JIT
// nor explicit, it's also invalid & should let parent know.
bindingFailed = true;
}
}
}
return bindingFailed;
}
use of com.google.inject.spi.InjectionPoint in project roboguice by roboguice.
the class FactoryProvider2 method getDependencies.
/** 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();
}
use of com.google.inject.spi.InjectionPoint in project roboguice by roboguice.
the class FactoryProvider2 method findMatchingConstructorInjectionPoint.
/**
* Finds a constructor suitable for the method. If the implementation contained any constructors
* marked with {@link AssistedInject}, this requires all {@link Assisted} parameters to exactly
* match the parameters (in any order) listed in the method. Otherwise, if no
* {@link AssistedInject} constructors exist, this will default to looking for an
* {@literal @}{@link Inject} constructor.
*/
private <T> InjectionPoint findMatchingConstructorInjectionPoint(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("%s is an interface, not a concrete class. Unable to create AssistedInject factory.", implementation);
throw errors.toException();
} else if (Modifier.isAbstract(rawType.getModifiers())) {
errors.addMessage("%s is abstract, not a concrete class. Unable to create AssistedInject factory.", implementation);
throw errors.toException();
} else if (Classes.isInnerClass(rawType)) {
errors.cannotInjectInnerClass(rawType);
throw errors.toException();
}
Constructor<?> matchingConstructor = null;
boolean anyAssistedInjectConstructors = false;
// Look for AssistedInject constructors...
for (Constructor<?> constructor : rawType.getDeclaredConstructors()) {
if (constructor.isAnnotationPresent(AssistedInject.class)) {
anyAssistedInjectConstructors = true;
if (constructorHasMatchingParams(implementation, constructor, paramList, errors)) {
if (matchingConstructor != null) {
errors.addMessage("%s has more than one constructor annotated with @AssistedInject" + " that matches the parameters in method %s. Unable to create " + "AssistedInject factory.", implementation, method);
throw errors.toException();
} else {
matchingConstructor = constructor;
}
}
}
}
if (!anyAssistedInjectConstructors) {
// If none existed, use @Inject.
try {
return InjectionPoint.forConstructorOf(implementation);
} catch (ConfigurationException e) {
errors.merge(e.getErrorMessages());
throw errors.toException();
}
} else {
// Otherwise, use it or fail with a good error message.
if (matchingConstructor != null) {
// safe because we got the constructor from this implementation.
@SuppressWarnings("unchecked") InjectionPoint ip = InjectionPoint.forConstructor((Constructor<? super T>) matchingConstructor, implementation);
return ip;
} else {
errors.addMessage("%s has @AssistedInject constructors, but none of them match the" + " parameters in method %s. Unable to create AssistedInject factory.", implementation, method);
throw errors.toException();
}
}
}
use of com.google.inject.spi.InjectionPoint in project spock by spockframework.
the class GuiceInterceptor method injectValues.
private void injectValues(Object target, boolean sharedFields) 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());
field.setAccessible(true);
field.set(target, value);
}
}
use of com.google.inject.spi.InjectionPoint in project guice by google.
the class Providers method guicify.
/**
* 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);
}
}
Aggregations