Search in sources :

Example 1 with AspectJAroundAdvice

use of org.springframework.aop.aspectj.AspectJAroundAdvice in project uPortal by Jasig.

the class AspectJAroundAdviceFactory method createInstance.

@Override
protected AspectJAroundAdvice createInstance() throws Exception {
    final Class<? extends Object> aspectType = this.aspect.getClass();
    final Method method = ReflectionUtils.findMethod(aspectType, this.method, this.args);
    final SingletonAspectInstanceFactory aif = new SingletonAspectInstanceFactory(this.aspect);
    return new AspectJAroundAdvice(method, pointcut, aif);
}
Also used : AspectJAroundAdvice(org.springframework.aop.aspectj.AspectJAroundAdvice) SingletonAspectInstanceFactory(org.springframework.aop.aspectj.SingletonAspectInstanceFactory) Method(java.lang.reflect.Method)

Example 2 with AspectJAroundAdvice

use of org.springframework.aop.aspectj.AspectJAroundAdvice in project spring-framework by spring-projects.

the class ReflectiveAspectJAdvisorFactory method getAdvice.

@Override
public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut expressionPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {
    Class<?> candidateAspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
    validate(candidateAspectClass);
    AspectJAnnotation<?> aspectJAnnotation = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
    if (aspectJAnnotation == null) {
        return null;
    }
    // Check that it's an AspectJ-annotated class
    if (!isAspect(candidateAspectClass)) {
        throw new AopConfigException("Advice must be declared inside an aspect type: " + "Offending method '" + candidateAdviceMethod + "' in class [" + candidateAspectClass.getName() + "]");
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Found AspectJ method: " + candidateAdviceMethod);
    }
    AbstractAspectJAdvice springAdvice;
    switch(aspectJAnnotation.getAnnotationType()) {
        case AtBefore:
            springAdvice = new AspectJMethodBeforeAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            break;
        case AtAfter:
            springAdvice = new AspectJAfterAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            break;
        case AtAfterReturning:
            springAdvice = new AspectJAfterReturningAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation();
            if (StringUtils.hasText(afterReturningAnnotation.returning())) {
                springAdvice.setReturningName(afterReturningAnnotation.returning());
            }
            break;
        case AtAfterThrowing:
            springAdvice = new AspectJAfterThrowingAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation();
            if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
                springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
            }
            break;
        case AtAround:
            springAdvice = new AspectJAroundAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            break;
        case AtPointcut:
            if (logger.isDebugEnabled()) {
                logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
            }
            return null;
        default:
            throw new UnsupportedOperationException("Unsupported advice type on method: " + candidateAdviceMethod);
    }
    // Now to configure the advice...
    springAdvice.setAspectName(aspectName);
    springAdvice.setDeclarationOrder(declarationOrder);
    String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod);
    if (argNames != null) {
        springAdvice.setArgumentNamesFromStringArray(argNames);
    }
    springAdvice.calculateArgumentBindings();
    return springAdvice;
}
Also used : AopConfigException(org.springframework.aop.framework.AopConfigException) AspectJAfterThrowingAdvice(org.springframework.aop.aspectj.AspectJAfterThrowingAdvice) AspectJAroundAdvice(org.springframework.aop.aspectj.AspectJAroundAdvice) AspectJMethodBeforeAdvice(org.springframework.aop.aspectj.AspectJMethodBeforeAdvice) AspectJAfterAdvice(org.springframework.aop.aspectj.AspectJAfterAdvice) AbstractAspectJAdvice(org.springframework.aop.aspectj.AbstractAspectJAdvice) AspectJAfterReturningAdvice(org.springframework.aop.aspectj.AspectJAfterReturningAdvice) AfterReturning(org.aspectj.lang.annotation.AfterReturning) AfterThrowing(org.aspectj.lang.annotation.AfterThrowing)

Example 3 with AspectJAroundAdvice

use of org.springframework.aop.aspectj.AspectJAroundAdvice in project uPortal by Jasig.

the class AspectJExpressionTest method testProgramaticPointcut.

@Test
public void testProgramaticPointcut() {
    final RepositoryPointcutInterface targetPointcutInterface = new RepositoryPointcutInterfaceImpl();
    final AspectJProxyFactory portletPreferencesProxyFactory = new AspectJProxyFactory(targetPointcutInterface);
    final Method interceptorMethod = ReflectionUtils.findMethod(CountingMethodInterceptor.class, "countInvocation", ProceedingJoinPoint.class);
    final AspectJAroundAdvice aspectJAroundAdvice = new AspectJAroundAdvice(interceptorMethod, repositoryPointcutInterfaceExecutionPointcut, new SingletonAspectInstanceFactory(this.countingMethodInterceptor));
    portletPreferencesProxyFactory.addAdvice(aspectJAroundAdvice);
    final RepositoryPointcutInterface proxiedPointcutInterface = (RepositoryPointcutInterface) portletPreferencesProxyFactory.getProxy();
    assertEquals(0, countingMethodInterceptor.getCount());
    proxiedPointcutInterface.methodOne("test");
    assertEquals(1, countingMethodInterceptor.getCount());
    proxiedPointcutInterface.methodOne("test");
    assertEquals(2, countingMethodInterceptor.getCount());
}
Also used : AspectJAroundAdvice(org.springframework.aop.aspectj.AspectJAroundAdvice) AspectJProxyFactory(org.springframework.aop.aspectj.annotation.AspectJProxyFactory) SingletonAspectInstanceFactory(org.springframework.aop.aspectj.SingletonAspectInstanceFactory) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

AspectJAroundAdvice (org.springframework.aop.aspectj.AspectJAroundAdvice)3 Method (java.lang.reflect.Method)2 SingletonAspectInstanceFactory (org.springframework.aop.aspectj.SingletonAspectInstanceFactory)2 AfterReturning (org.aspectj.lang.annotation.AfterReturning)1 AfterThrowing (org.aspectj.lang.annotation.AfterThrowing)1 Test (org.junit.Test)1 AbstractAspectJAdvice (org.springframework.aop.aspectj.AbstractAspectJAdvice)1 AspectJAfterAdvice (org.springframework.aop.aspectj.AspectJAfterAdvice)1 AspectJAfterReturningAdvice (org.springframework.aop.aspectj.AspectJAfterReturningAdvice)1 AspectJAfterThrowingAdvice (org.springframework.aop.aspectj.AspectJAfterThrowingAdvice)1 AspectJMethodBeforeAdvice (org.springframework.aop.aspectj.AspectJMethodBeforeAdvice)1 AspectJProxyFactory (org.springframework.aop.aspectj.annotation.AspectJProxyFactory)1 AopConfigException (org.springframework.aop.framework.AopConfigException)1