As for your last comment: I am not discouraging you from anything but bad design, I was trying to encourage you to do the right thing: refactor and not make life harder on yourself than necessary. You do not even know the very AspectJ syntax basics and yet you already want to implement an over-complex scenario with tons of classes, which is a maintenance nightmare. I am trying to help by motivating you not to make short-sighted decisions. Believe me, I have been using AspectJ for years in what you call real-life projects with lots of legacy code. Avoiding even the cheapest bit of refactoring is much more expensive than doing smart refactoring - not too much, but enough according to the boy scout rule: leave the camp ground behind cleaner than you found it. It pays off, trust me.
Anyway, talking about your code snippet:
execution(*.*(..))
is syntactically wrong because you do not specify a return type (or placeholder for it) for the methods to be matched. You want to useexecution(* *.*(..))
or the shorthand versionexecution(* *(..))
.- The error "incompatible number of arguments to pointcut, expected 1 found 0" does not come from your pointcut but from your advice which you have not even bothered to post. You must have written something like
@Before("mycut()")
, but correct would be@Before("mycut(jp)")
.
Having said that, here is a simple, fully self-contained and compileable example:
Driver application:
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
System.out.println(multiply(3, add(4, 5)));
}
public static int multiply(int i, int j) { return i * j; }
public static int add(int i, int j) { return i + j; }
}
Aspect:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TraceAspect {
@Pointcut("execution(* *(..)) && if()")
public static boolean hasMatchingSignature(JoinPoint thisJoinPoint) {
return !thisJoinPoint.getSignature().getName().equals("main");
}
@Before("hasMatchingSignature(thisJoinPoint)")
public void myAdvice(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
Sample output:
execution(int de.scrum_master.app.Application.add(int, int))
execution(int de.scrum_master.app.Application.multiply(int, int))
27
If your if()
pointcut would just return true
, the output would also show the execution of main
.