use of com.google.inject.spi.InterceptorBinding in project guice by google.
the class MethodInterceptionTest method testGetElements_interceptorBindings.
@Test
public void testGetElements_interceptorBindings() throws Exception {
@SuppressWarnings("rawtypes") Matcher<Class> classMatcher = Matchers.subclassesOf(List.class);
Matcher<Method> methodMatcher = Matchers.returns(Matchers.identicalTo(int.class));
MethodInterceptor interceptor = new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
return null;
}
};
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(classMatcher, methodMatcher, interceptor);
}
});
final List<InterceptorBinding> interceptorBindings = new ArrayList<>();
for (Element element : injector.getElements()) {
element.acceptVisitor(new DefaultElementVisitor<Void>() {
@Override
public Void visit(InterceptorBinding interceptorBinding) {
interceptorBindings.add(interceptorBinding);
return null;
}
});
}
assertThat(interceptorBindings).hasSize(1);
InterceptorBinding extractedBinding = interceptorBindings.get(0);
assertSame(classMatcher, extractedBinding.getClassMatcher());
assertSame(methodMatcher, extractedBinding.getMethodMatcher());
assertSame(interceptor, extractedBinding.getInterceptors().get(0));
}
use of com.google.inject.spi.InterceptorBinding in project guice by google.
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);
ConstructionProxyFactory<T> factory = null;
if (InternalFlags.isBytecodeGenEnabled()) {
ImmutableList<InterceptorBinding> injectorBindings = injector.getBindingData().getInterceptorBindings();
ImmutableList<MethodAspect> methodAspects = ImmutableList.<MethodAspect>builder().addAll(Lists.transform(injectorBindings, MethodAspect::fromBinding)).addAll(membersInjector.getAddedAspects()).build();
factory = new ProxyFactory<>(injectionPoint, methodAspects);
} else {
factory = new DefaultConstructionProxyFactory<>(injectionPoint);
}
errors.throwIfNewErrors(numErrorsBefore);
return new ConstructorInjector<T>(membersInjector.getInjectionPoints(), factory.create(), constructorParameterInjectors, membersInjector);
}
use of com.google.inject.spi.InterceptorBinding in project shiro by apache.
the class ShiroAopModuleTest method testBindShiroInterceptor.
@Test
public void testBindShiroInterceptor() {
ShiroAopModule underTest = new ShiroAopModule() {
@Override
protected void configureInterceptors(AnnotationResolver resolver) {
bindShiroInterceptor(new MyAnnotationMethodInterceptor());
}
};
List<Element> elements = Elements.getElements(underTest);
for (Element element : elements) {
if (element instanceof InterceptorBinding) {
InterceptorBinding binding = (InterceptorBinding) element;
assertTrue(binding.getClassMatcher().matches(getClass()));
Method method = null;
Class<? extends Annotation> theAnnotation = null;
for (Class<? extends Annotation> annotation : protectedMethods.keySet()) {
if (binding.getMethodMatcher().matches(protectedMethods.get(annotation))) {
method = protectedMethods.get(annotation);
theAnnotation = annotation;
protectedMethods.remove(annotation);
break;
}
}
if (method == null) {
fail("Did not expect interceptor binding " + binding.getInterceptors());
}
List<MethodInterceptor> interceptors = binding.getInterceptors();
assertEquals(1, interceptors.size());
assertTrue(interceptors.get(0) instanceof AopAllianceMethodInterceptorAdapter);
assertTrue(interceptorTypes.get(theAnnotation).isInstance(((AopAllianceMethodInterceptorAdapter) interceptors.get(0)).shiroInterceptor));
}
}
assertTrue("Not all interceptors were bound.", protectedMethods.isEmpty());
}
Aggregations