Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointcut matching methods with annotated parameters

I need to create an aspect with a pointcut matching a method if:

  • Is public
  • Its class is annotated with @Controller (Finally does not)
  • One of its parameters (can have many) is annotated with @MyParamAnnotation.

I think the first two conditions are easy, but I don't know if its possible to accomplish the third with Spring. If it is not, maybe I can change it into:

  • One of its parameters is an instance of type com.me.MyType (or implements some interface)

Do you think it's possible to achieve this? And will performance be good?

Thanks

EDIT: One example of a matching method. As you can see, MyMethod is not annotated (but it can be).

@Controller
public class MyClass {
    public void MyMethod (String arg0, @MyParamAnnotation Object arg1, Long arg3) {
        ...
    }
}

EDIT: The solution I finally used, based on @Espen answers. As you can see, I changed my conditions a little: class doesn't actually need to be a @Controller.

@Around("execution(public * * (.., @SessionInject (*), ..))")
public void methodAround(JoinPoint joinPoint) throws Exception {
    ...
}
like image 642
sinuhepop Avatar asked May 04 '10 15:05

sinuhepop


People also ask

What methods does this pointcut expression reference?

This pointcut matches any method that starts with find and has only one parameter of type Long. If we want to match a method with any number of parameters, but still having the fist parameter of type Long, we can use the following expression: @Pointcut("execution(* *.. find*(Long,..))")

Which AOP advice can access and modify arguments of a JoinPoint?

You can use Spring AOP, create point cut using @Around . Then you can use the below code to change the arguments of the method, based on the condition. int index = 0; Object[] modifiedArgs = proceedingJoinPoint.

What is pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

What is pointcut in AOP?

Pointcut is a set of one or more JoinPoint where an advice should be executed. You can specify Pointcuts using expressions or patterns as we will see in our AOP examples. In Spring, Pointcut helps to use specific JoinPoints to apply the advice.


1 Answers

It was an interesting problem, so I created a little sample application to solve the case! (And improved it with Sinuhe's feedback afterwards.)

I have created a DemoController class that should work as an example for the aspect:

@Controller
public class DemoController {

    public void soSomething(String s, @MyParamAnnotation Double d, Integer i) {
    }

    public void doSomething(String s, long l, @MyParamAnnotation int i) {
    }

    public void doSomething(@MyParamAnnotation String s) {
    }

    public void doSomething(long l) {
    }
}

The aspect that will add a join point on the first three methods, but not the last method where the parameter isn't annotated with @MyParamAnnotation:

@Aspect
public class ParameterAspect {

    @Pointcut("within(@org.springframework.stereotype.Controller *)")
    public void beanAnnotatedWithAtController() {
    }

    @Pointcut("execution(public * *(.., @aspects.MyParamAnnotation (*), ..))")
    public void methodWithAnnotationOnAtLeastOneParameter() {
    }

    @Before("beanAnnotatedWithAtController() " 
            + "&& methodWithAnnotationOnAtLeastOneParameter()")
    public void beforeMethod() {    
        System.out.println("At least one of the parameters are " 
                  + "annotated with @MyParamAnnotation");
    }
}

The first pointcut will create a joinpoint on all methods inside classes marked with @Controller.

The second pointcut will add a joinpoint when the following conditions are met:

  • public method
  • first * is a wildcard for every return type.
  • second * is a wildcard for all methods in all classes.
  • (.., matches zero to many parameters of any type before the annotated parameter.
  • @aspects.MyParamAnnotation (*), matches a parameter annotated with the given annotation.
  • ..) matches zero to many parameters of any type after the annotated parameter.

Finally, the @Before advice advises all methods where all conditions in both pointcuts are satisfied.

The pointcut works with both AspectJ and Spring AOP!

When it comes to performance. The overhead is small, especially with AspectJ that does the weaving on compile-time or load-time.

like image 165
11 revs, 2 users 93% Avatar answered Sep 25 '22 02:09

11 revs, 2 users 93%