use of com.google.inject.spi.InjectionPoint in project guice by google.
the class ProviderMethodsModule method createProviderMethod.
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);
// Define T as the method's return type.
@SuppressWarnings("unchecked") 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);
}
use of com.google.inject.spi.InjectionPoint in project guice by google.
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 guice by google.
the class DefaultConstructionProxyFactory method create.
@Override
public ConstructionProxy<T> create() {
// the injection point is for a constructor of T
@SuppressWarnings("unchecked") final Constructor<T> constructor = (Constructor<T>) injectionPoint.getMember();
/*if[AOP]*/
try {
net.sf.cglib.reflect.FastClass fc = BytecodeGen.newFastClassForMember(constructor);
if (fc != null) {
int index = fc.getIndex(constructor.getParameterTypes());
// We could just fall back to reflection in this case but I believe this should actually
// be impossible.
Preconditions.checkArgument(index >= 0, "Could not find constructor %s in fast class", constructor);
return new FastClassProxy<T>(injectionPoint, constructor, fc, index);
}
} catch (net.sf.cglib.core.CodeGenerationException e) {
/* fall-through */
}
return new ReflectiveProxy<T>(injectionPoint, constructor);
}
use of com.google.inject.spi.InjectionPoint in project guice by google.
the class BindingBuilder method toConstructor.
@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;
}
use of com.google.inject.spi.InjectionPoint in project guice by google.
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();
}
Aggregations