简体   繁体   English

AspectJ-循环迭代次数

[英]AspectJ - count of loop iterations

Can I get a number of loop iterations of any loop with AspectJ, or any aop approach, or any external approach to the class? 我可以使用AspectJ,任何aop方法或任何外部方法对类进行多次循环迭代吗? Imagine loop with just incrementation of local variable(not method argument), or something like that, what you can't get through joinpoint. 想象一下,仅增加局部变量(而不是方法参数)的循环,或类似的事情,您无法通过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;
}

I think that its not possible with aspectJ. 我认为,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. 还有其他方法,例如,用于测试中的cobertura报告的库为您提供了报告,该报告计算程序经过每一行的次数。 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. 最相关的cobertura库可能是三叶草,emma或cobertura,您可以尝试查找有关该库使用的技术的一些信息,并尝试执行类似的操作。

Not an easy task. 绝非易事。

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 ++ . 您也许可以使用cflow样式的切入点,但是它将绑定到每个指令执行,而不会选择++ if you must use aspectj, then a better approach would be to use a Counter class with an increment method. 如果必须使用Aspectj,则更好的方法是将Counter类与增量方法一起使用。 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. 或者,如果您未绑定到Aspectj,则使用ASM进行静态分析,您应该能够挑选出我认为通常用于for循环的字节码iinc ,并插入自定义字节码来调用并告知您的自定义方面增量。

bytecode definitions for java Java的字节码定义

asm library for java Java的asm库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM