I created simple aspect to count how many times specific method is executed. I have to tell that I'm doing it first time so probably it's not really pretty.
First, I created something like that:
@Aspect
@Component
public class ItemAspect {
private Map<String, Integer> functionsCalls = new HashMap<>();
public ItemAspect(){
this.functionsCalls.put("ItemApiController.addItem()", 0);
this.functionsCalls.put("ItemApiController.getItems()", 0);
}
@Pointcut("execution(* io.swagger.api.ItemApiController.getItems(..))")
private void itemApiControllerEx(){ }
@After("itemApiControllerEx()")
public void doAfterItemApiControllerEx (JoinPoint joinPoint) {
this.functionsCalls.put(joinPoint.getSignature().toString(), this.functionsCalls.get(joinPoint.getSignature().toString())+1);
}
public Map<String, Integer> getFunctionsCalls () {
return functionsCalls; }
}
and it counted how many times "getItems()" was executed correctly. (Yes, for "addItem()" it work too.) But I want to count both methods, so I converted code to:
@Aspect
@Component
public class ItemAspect {
private Map<String, Integer> functionsCalls = new HashMap<>();
public ItemAspect(){
this.functionsCalls.put("ItemApiController.addItem()", 0);
this.functionsCalls.put("ItemApiController.getItems()", 0);
}
@Pointcut("execution(* io.swagger.api.ItemApiController.*(..))")
private void itemApiControllerEx(){ }
@After("itemApiControllerEx()")
public void doAfterItemApiControllerEx (JoinPoint joinPoint) {
this.functionsCalls.put(joinPoint.getSignature().toString(), this.functionsCalls.get(joinPoint.getSignature().toString())+1);
}
public Map<String, Integer> getFunctionsCalls () {
return functionsCalls; }
}
and now I'm getting NullPointerException when I try to get signature. Can someone tell me why this is happening and how to fix it?