Search in sources :

Example 31 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.

the class NullPrimitiveTests method testNullPrimitiveWithCglibProxy.

@Test
public void testNullPrimitiveWithCglibProxy() {
    Bar target = new Bar();
    ProxyFactory factory = new ProxyFactory(target);
    factory.addAdvice(new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            return null;
        }
    });
    Bar bar = (Bar) factory.getProxy();
    thrown.expect(AopInvocationException.class);
    thrown.expectMessage("Bar.getValue()");
    assertEquals(0, bar.getValue());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Test(org.junit.Test)

Example 32 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.

the class DefaultAdvisorChainFactory method getInterceptorsAndDynamicInterceptionAdvice.

@Override
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Advised config, Method method, Class<?> targetClass) {
    // This is somewhat tricky... We have to process introductions first,
    // but we need to preserve order in the ultimate list.
    List<Object> interceptorList = new ArrayList<>(config.getAdvisors().length);
    Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
    boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
    AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
    for (Advisor advisor : config.getAdvisors()) {
        if (advisor instanceof PointcutAdvisor) {
            // Add it conditionally.
            PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
            if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
                MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
                MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
                if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {
                    if (mm.isRuntime()) {
                        // isn't a problem as we normally cache created chains.
                        for (MethodInterceptor interceptor : interceptors) {
                            interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
                        }
                    } else {
                        interceptorList.addAll(Arrays.asList(interceptors));
                    }
                }
            }
        } else if (advisor instanceof IntroductionAdvisor) {
            IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
            if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
                Interceptor[] interceptors = registry.getInterceptors(advisor);
                interceptorList.addAll(Arrays.asList(interceptors));
            }
        } else {
            Interceptor[] interceptors = registry.getInterceptors(advisor);
            interceptorList.addAll(Arrays.asList(interceptors));
        }
    }
    return interceptorList;
}
Also used : PointcutAdvisor(org.springframework.aop.PointcutAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) ArrayList(java.util.ArrayList) PointcutAdvisor(org.springframework.aop.PointcutAdvisor) IntroductionAdvisor(org.springframework.aop.IntroductionAdvisor) Advisor(org.springframework.aop.Advisor) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) GlobalAdvisorAdapterRegistry(org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry) AdvisorAdapterRegistry(org.springframework.aop.framework.adapter.AdvisorAdapterRegistry) MethodMatcher(org.springframework.aop.MethodMatcher)

Example 33 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project perry by ca-cwds.

the class TestModule method configure.

@Override
protected void configure() {
    bind(TestService.class).to(TestServiceImpl.class);
    install(new SecurityModule(null).addAuthorizer("case:read", CaseAuthorizer.class).addStaticAuthorizer(TestStaticAuthorizer.class));
    bindInterceptor(Matchers.any(), Matchers.any(), new MethodInterceptor() {

        PermissionAnnotationMethodInterceptor permissionAnnotationMethodInterceptor = new PermissionAnnotationMethodInterceptor();

        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            return permissionAnnotationMethodInterceptor.invoke(new org.apache.shiro.aop.MethodInvocation() {

                @Override
                public Object proceed() throws Throwable {
                    return invocation.proceed();
                }

                @Override
                public Method getMethod() {
                    return invocation.getMethod();
                }

                @Override
                public Object[] getArguments() {
                    return invocation.getArguments();
                }

                @Override
                public Object getThis() {
                    return invocation.getThis();
                }
            });
        }
    });
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) PermissionAnnotationMethodInterceptor(org.apache.shiro.authz.aop.PermissionAnnotationMethodInterceptor) PermissionAnnotationMethodInterceptor(org.apache.shiro.authz.aop.PermissionAnnotationMethodInterceptor) TestService(gov.ca.cwds.testapp.service.TestService) TestStaticAuthorizer(gov.ca.cwds.security.authorizer.TestStaticAuthorizer) MethodInvocation(org.aopalliance.intercept.MethodInvocation) SecurityModule(gov.ca.cwds.security.module.SecurityModule)

Example 34 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-data-commons by spring-projects.

the class ProjectingMethodInterceptorUnitTests method considersPrimitivesAsWrappers.

// DATAREST-221
@Test
public void considersPrimitivesAsWrappers() throws Throwable {
    MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(factory, interceptor, conversionService);
    when(invocation.getMethod()).thenReturn(Helper.class.getMethod("getPrimitive"));
    when(interceptor.invoke(invocation)).thenReturn(1L);
    assertThat(methodInterceptor.invoke(invocation)).isEqualTo(1L);
    verify(factory, times(0)).createProjection((Class<?>) any(), any());
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Test(org.junit.Test)

Example 35 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-data-commons by spring-projects.

the class ProjectingMethodInterceptorUnitTests method appliesProjectionToNonEmptyLists.

// DATAREST-394, DATAREST-408
@Test
@SuppressWarnings("unchecked")
public void appliesProjectionToNonEmptyLists() throws Throwable {
    MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor, conversionService);
    Object result = methodInterceptor.invoke(mockInvocationOf("getHelperList", Collections.singletonList(mock(Helper.class))));
    assertThat(result).isInstanceOf(List.class);
    List<Object> projections = (List<Object>) result;
    assertThat(projections).hasSize(1);
    assertThat(projections).hasOnlyElementsOfType(HelperProjection.class);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) List(java.util.List) Test(org.junit.Test)

Aggregations

MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)57 Test (org.junit.Test)30 MethodInvocation (org.aopalliance.intercept.MethodInvocation)28 List (java.util.List)20 Method (java.lang.reflect.Method)19 ArrayList (java.util.ArrayList)18 Test (org.junit.jupiter.api.Test)15 Advice (org.aopalliance.aop.Advice)14 Map (java.util.Map)13 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)13 AopUtils (org.springframework.aop.support.AopUtils)12 TestBean (org.springframework.beans.testfixture.beans.TestBean)12 ProxyFactory (org.springframework.aop.framework.ProxyFactory)11 HashMap (java.util.HashMap)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)9 Advisor (org.springframework.aop.Advisor)9 Message (org.springframework.messaging.Message)8