rb8680
rb8680

Reputation: 279

Aspectj @Around pointcut all methods in Java

i am writing a simple timer aspect to instrument all the methods in all the packages that belong my project. But, then the return types of various methods in those classes are different and I am getting this following error:

It only works for setter but not for getter...

Error: applying to joinpoint that doesn't return void

and here is my timeraspect...

@Around("execution(* com.myproject..*(..))")
public void log(ProceedingJoinPoint pjp) throws Throwable{


    LOG.info("TimerAspect");
    String name = pjp.getSignature().getName();
    Monitor mon = MonitorFactory.start(name);
    pjp.proceed();
    mon.stop();

    LOG.info("TimerAspect Mon" + mon);

    String printStr = mon.getLabel()+","+mon.getUnits()+","+mon.getLastValue()+","+mon.getHits()+","+mon.getAvg()+","+mon.getTotal()+","+mon.getMin()+","+mon.getMax()+","+mon.getFirstAccess()+","+mon.getLastAccess();

    File f = new File("target/stats.csv");
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(f, true));
    bufferedWriter.write(printStr);
    bufferedWriter.newLine();
    bufferedWriter.flush();
    bufferedWriter.close();


}

Any clue to resolve this is greatly appreciated.

Thanks

Upvotes: 10

Views: 15236

Answers (1)

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

You should capture the output from your adviced call and return that from your around advice along these lines:

@Around("execution(* com.myproject..*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable{

....
Object result = pjp.proceed();
......
return result;
}

This will take care of all your calls

Upvotes: 23

Related Questions