Example usage for org.aspectj.lang.reflect MethodSignature getExceptionTypes

List of usage examples for org.aspectj.lang.reflect MethodSignature getExceptionTypes

Introduction

In this page you can find the example usage for org.aspectj.lang.reflect MethodSignature getExceptionTypes.

Prototype

Class[] getExceptionTypes();

Source Link

Usage

From source file:org.springframework.aop.aspectj.MethodInvocationProceedingJoinPointTests.java

License:Apache License

@Test
public void testCanGetMethodSignatureFromJoinPoint() {
    final Object raw = new TestBean();
    // Will be set by advice during a method call
    final int newAge = 23;

    ProxyFactory pf = new ProxyFactory(raw);
    pf.setExposeProxy(true);/*from   ww w. j a v a  2  s.c o  m*/
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice(new MethodBeforeAdvice() {
        private int depth;

        @Override
        public void before(Method method, Object[] args, Object target) throws Throwable {
            JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
            assertTrue("Method named in toString", jp.toString().contains(method.getName()));
            // Ensure that these don't cause problems
            jp.toShortString();
            jp.toLongString();

            assertSame(target, AbstractAspectJAdvice.currentJoinPoint().getTarget());
            assertFalse(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getTarget()));

            ITestBean thisProxy = (ITestBean) AbstractAspectJAdvice.currentJoinPoint().getThis();
            assertTrue(AopUtils.isAopProxy(AbstractAspectJAdvice.currentJoinPoint().getThis()));

            assertNotSame(target, thisProxy);

            // Check getting again doesn't cause a problem
            assertSame(thisProxy, AbstractAspectJAdvice.currentJoinPoint().getThis());

            // Try reentrant call--will go through this advice.
            // Be sure to increment depth to avoid infinite recursion
            if (depth++ == 0) {
                // Check that toString doesn't cause a problem
                thisProxy.toString();
                // Change age, so this will be returned by invocation
                thisProxy.setAge(newAge);
                assertEquals(newAge, thisProxy.getAge());
            }

            assertSame(AopContext.currentProxy(), thisProxy);
            assertSame(target, raw);

            assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName());
            assertEquals(method.getModifiers(),
                    AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers());

            MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature();
            assertSame("Return same MethodSignature repeatedly", msig,
                    AbstractAspectJAdvice.currentJoinPoint().getSignature());
            assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(),
                    AbstractAspectJAdvice.currentJoinPoint());
            assertEquals(method.getDeclaringClass(), msig.getDeclaringType());
            assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes()));
            assertEquals(method.getReturnType(), msig.getReturnType());
            assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes()));
            msig.toLongString();
            msig.toShortString();
        }
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    assertEquals("Advice reentrantly set age", newAge, itb.getAge());
}