Search in sources :

Example 1 with InterceptorBinding

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));
}
Also used : Element(com.google.inject.spi.Element) ArrayList(java.util.ArrayList) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Method(java.lang.reflect.Method) InterceptorBinding(com.google.inject.spi.InterceptorBinding) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Test(org.junit.Test)

Example 2 with InterceptorBinding

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);
}
Also used : InjectionPoint(com.google.inject.spi.InjectionPoint) InterceptorBinding(com.google.inject.spi.InterceptorBinding)

Example 3 with InterceptorBinding

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());
}
Also used : InterceptorBinding(com.google.inject.spi.InterceptorBinding) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Element(com.google.inject.spi.Element) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

InterceptorBinding (com.google.inject.spi.InterceptorBinding)3 Element (com.google.inject.spi.Element)2 Method (java.lang.reflect.Method)2 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)2 Test (org.junit.Test)2 InjectionPoint (com.google.inject.spi.InjectionPoint)1 ArrayList (java.util.ArrayList)1 MethodInvocation (org.aopalliance.intercept.MethodInvocation)1