Question

Can I get a number of loop iterations of any loop with AspectJ, or any aop approach, or any external approach to the class? Imagine loop with just incrementation of local variable(not method argument), or something like that, what you can't get through joinpoint. I know that isn't possible set joinpoint to local variable.

public int iterationsOfLoop() {
    int count = 0;

    for(int i = 0, i<10; i++) {
        count++;
    }

    return count;
}
Was it helpful?

Solution

Short answer, no.

most pointcut definitions can only be bound to method invocations, which sadly ++ is not. you may be able to use a cflow style pointcut, but that would bind to every instruction execution, and would not pick out the ++. if you must use aspectj, then a better approach would be to use a Counter class with an increment method. or if you are not bound to aspectj, then using ASM, for static analysis, you should be able to pick out the bytecode iinc which i think is usually used in for loops, and insert custom bytecode to call and inform your custom aspect of the increment.

bytecode definitions for java

asm library for java

OTHER TIPS

I think that its not possible with aspectJ.

There are other approaches, for example the libraries used for cobertura reporting in testing present you reports that counts the number of times the program goes through each line. These libraries uses some form or code instrumentation.

The most relevant cobertura libraries are probably clover, emma or cobertura, you can try to find some information about the techniques that this libraries uses and try to do something similar.

Not an easy task.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top