10

I need to intrecept some methods and their attributes by using annotations as point cuts, but how can I access those method attributes. I have following code that succesfully can run code before method is run, but I just don't know how I can access those attrbiutes.

package my.package;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyAspect {

 @Pointcut(value="execution(public * *(..))")
 public void anyPublicMethod() {
 }

 @Around("anyPublicMethod() && @annotation(myAnnotation )")
 public Object myAspect(ProceedingJoinPoint pjp, MyAnnotation myAnnotation)
    throws Throwable {

  // how can I access method attributes here ?
  System.out.println("hello aspect!");
  return pjp.proceed();
 }
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
newbie
  • 24,286
  • 80
  • 201
  • 301

2 Answers2

14

You can get them from the ProceedingJoinPoint object:

@Around("anyPublicMethod() && @annotation(myAnnotation )")
public Object myAspect(final ProceedingJoinPoint pjp,
    final MyAnnotation myAnnotation) throws Throwable{

    // retrieve the methods parameter types (static):
    final Signature signature = pjp.getStaticPart().getSignature();
    if(signature instanceof MethodSignature){
        final MethodSignature ms = (MethodSignature) signature;
        final Class<?>[] parameterTypes = ms.getParameterTypes();
        for(final Class<?> pt : parameterTypes){
            System.out.println("Parameter type:" + pt);
        }
    }

    // retrieve the runtime method arguments (dynamic)
    for(final Object argument : pjp.getArgs()){
        System.out.println("Parameter value:" + argument);
    }

    return pjp.proceed();
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Ok now Im getting it, but it returns odd parameters. org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper@8c666a org.springframework.security.web.context.HttpSessionSecurityContextRepository$SaveToSessionResponseWrapper@197d20c . My method is Spring controller with @RequestMapping and my own annotation. Why its giving me some Spring Security objects here when it should give HttpServletRequest object as params? – newbie Oct 27 '10 at 09:16
  • Those are spring proxies around the original http request objects. See my updated answer: you can get both the defined parameter types (static analysis) and the actual runtime arguments (dynamic analysis) – Sean Patrick Floyd Oct 27 '10 at 09:32
  • How can I get static parameter values, in this case HttpServletRequest object ? – newbie Oct 27 '10 at 10:02
  • I showed you how to get the static type in my updated answer. But I don't think that's what you mean. Don't worry: SecurityContextHolderAwareRequestWrapper *is* actually a HttpServletRequest implementation (see http://static.springsource.org/spring-security/site/apidocs/org/springframework/security/web/servletapi/SecurityContextHolderAwareRequestWrapper.html) that delegates functionality to the original request underneath. – Sean Patrick Floyd Oct 27 '10 at 10:10
5

ProceedingJoinPoint has pjp.getArgs(), which returns all the parameters of the method.

(but these are called parameters / arguments, not attributes)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140