0

In Spring we can share common pointcut definitions like below

@Aspect
public class SystemArchitecture {

  /**
   * A join point is in the web layer if the method is defined
   * in a type in the com.xyz.someapp.web package or any sub-package
   * under that.
   */
  @Pointcut("within(com.xyz.someapp.web..*)")
  public void inWebLayer() {}

}

And the above can be used like below

@Aspect
public class MyAspect {


    @AfterThrowing(pointcut = "inWebLayer()  ")
    public void processError(JoinPoint jp) {
        logger.info("Enter:processError");

    }

}

Is it possible to pass the jointpoint to the share point cut definition .

Something like below where myCustomCheck is another shared point cut definition which checks something based on the jointpoint passed to it.

 @Aspect
    public class MyAspect {


        @AfterThrowing(pointcut = "inWebLayer() && myCustomCheck(jp) ")
        public void processError(JoinPoint jp) {
            logger.info("Enter:processError");

        }

    }

Is this feasible ?

Thanks

Lives.

lives
  • 1,243
  • 5
  • 25
  • 61

1 Answers1

0

This is what you can do in code style:

public static boolean checkLine(JoinPoint tjp, int l) {
    return tjp.getSourceLocation().getLine()==l;
}

before(): execution(* just*(..)) && if(checkLine(thisJoinPoint,16)) {
    System.out.println("Execution!");
}

Not exactly passing the join point to another pointcut, instead passing it to a helper method.

I think a similar thing is possible in annotation style syntax but am not quite so quick at crafting that syntax, using the if() model it supports.

Andy Clement
  • 2,510
  • 16
  • 11
  • what is thisJoinPoint ? Can we get a reference to joinpoint if we access like this ? – lives Jul 17 '14 at 11:49
  • thisJoinPoint is a well known variable that you can use in code style pointcuts/advice that is of type JoinPoint object. For annotation style aspects like you are using, you mention it explicitly like you have done in your example: public void processError(JoinPoint jp) { I'm just not 100% how you tie that and the if() together if using that syntax. It would take some experimenting with the annotation style if() syntax which is slightly different. – Andy Clement Jul 18 '14 at 20:31