use of com.google.inject.spi.InjectionPoint in project roboguice by roboguice.
the class ProxyFactoryTest method testSimpleCase.
public void testSimpleCase() throws NoSuchMethodException, InvocationTargetException, ErrorsException {
SimpleInterceptor interceptor = new SimpleInterceptor();
InjectionPoint injectionPoint = InjectionPoint.forConstructorOf(Simple.class);
aspects.add(new MethodAspect(any(), any(), interceptor));
ProxyFactory<Simple> factory = new ProxyFactory<Simple>(injectionPoint, aspects);
ConstructionProxy<Simple> constructionProxy = factory.create();
Simple simple = constructionProxy.newInstance();
simple.invoke();
assertTrue(simple.invoked);
assertTrue(interceptor.invoked);
}
use of com.google.inject.spi.InjectionPoint in project roboguice by roboguice.
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);
}
}
use of com.google.inject.spi.InjectionPoint in project roboguice by roboguice.
the class ConstructorBindingImpl method create.
/**
* @param constructorInjector the constructor to use, or {@code null} to use the default.
* @param failIfNotLinked true if this ConstructorBindingImpl's InternalFactory should
* only succeed if retrieved from a linked binding
*/
static <T> ConstructorBindingImpl<T> create(InjectorImpl injector, Key<T> key, InjectionPoint constructorInjector, Object source, Scoping scoping, Errors errors, boolean failIfNotLinked, boolean failIfNotExplicit) throws ErrorsException {
int numErrors = errors.size();
// constructorBinding guarantees type is consistent
@SuppressWarnings("unchecked") Class<? super T> rawType = constructorInjector == null ? key.getTypeLiteral().getRawType() : (Class) constructorInjector.getDeclaringType().getRawType();
// We can't inject abstract classes.
if (Modifier.isAbstract(rawType.getModifiers())) {
errors.missingImplementation(key);
}
// Error: Inner class.
if (Classes.isInnerClass(rawType)) {
errors.cannotInjectInnerClass(rawType);
}
errors.throwIfNewErrors(numErrors);
// Find a constructor annotated @Inject
if (constructorInjector == null) {
try {
constructorInjector = InjectionPoint.forConstructorOf(key.getTypeLiteral());
if (failIfNotExplicit && !hasAtInject((Constructor) constructorInjector.getMember())) {
errors.atInjectRequired(rawType);
}
} catch (ConfigurationException e) {
throw errors.merge(e.getErrorMessages()).toException();
}
}
// if no scope is specified, look for a scoping annotation on the concrete class
if (!scoping.isExplicitlyScoped()) {
Class<?> annotatedType = constructorInjector.getMember().getDeclaringClass();
Class<? extends Annotation> scopeAnnotation = findScopeAnnotation(errors, annotatedType);
if (scopeAnnotation != null) {
scoping = Scoping.makeInjectable(Scoping.forAnnotation(scopeAnnotation), injector, errors.withSource(rawType));
}
}
errors.throwIfNewErrors(numErrors);
Factory<T> factoryFactory = new Factory<T>(failIfNotLinked, key);
InternalFactory<? extends T> scopedFactory = Scoping.scope(key, injector, factoryFactory, source, scoping);
return new ConstructorBindingImpl<T>(injector, key, source, scopedFactory, scoping, factoryFactory, constructorInjector);
}
use of com.google.inject.spi.InjectionPoint in project roboguice by roboguice.
the class ConstructorBindingImpl method applyTo.
// the raw constructor member and declaring type always agree
@SuppressWarnings("unchecked")
public void applyTo(Binder binder) {
InjectionPoint constructor = getConstructor();
getScoping().applyTo(binder.withSource(getSource()).bind(getKey()).toConstructor((Constructor) getConstructor().getMember(), (TypeLiteral) constructor.getDeclaringType()));
}
use of com.google.inject.spi.InjectionPoint in project roboguice by roboguice.
the class ConstructorInjectorStore method createConstructor.
private <T> ConstructorInjector<T> createConstructor(InjectionPoint injectionPoint, Errors errors) throws ErrorsException {
int numErrorsBefore = errors.size();
SingleParameterInjector<?>[] constructorParameterInjectors = injector.getParametersInjectors(injectionPoint.getDependencies(), errors);
// the injector type agrees with the injection point type
@SuppressWarnings("unchecked") MembersInjectorImpl<T> membersInjector = (MembersInjectorImpl<T>) injector.membersInjectorStore.get(injectionPoint.getDeclaringType(), errors);
/*if[AOP]*/
ImmutableList<MethodAspect> injectorAspects = injector.state.getMethodAspects();
ImmutableList<MethodAspect> methodAspects = membersInjector.getAddedAspects().isEmpty() ? injectorAspects : ImmutableList.copyOf(concat(injectorAspects, membersInjector.getAddedAspects()));
ConstructionProxyFactory<T> factory = new ProxyFactory<T>(injectionPoint, methodAspects);
/*end[AOP]*/
/*if[NO_AOP]
ConstructionProxyFactory<T> factory = new DefaultConstructionProxyFactory<T>(injectionPoint);
end[NO_AOP]*/
errors.throwIfNewErrors(numErrorsBefore);
return new ConstructorInjector<T>(membersInjector.getInjectionPoints(), factory.create(), constructorParameterInjectors, membersInjector);
}
Aggregations