最好的选择?在编译之前编辑字节码(asm)或编辑java文件

问题描述:


  • 检测变量之间的比较和变量之间的比较


通用目的:计算执行某些参数后进行比较的数量

General purpose: count the amount of comparisons and copies made after execution with certain parameters

注意:我始终有一个.java文件以

Note: I always have a .java file to begin with

1)编辑java文件

1) Edit java file

找到与regex的比较,并注入代码行附近
然后编译类(我的应用程序使用JavaCompiler)

Find comparisons with regex and inject pieces of code near the line And then compile the class (My application uses JavaCompiler)

2)使用ASM字节码工程

2)Use ASM Bytecode engineering

同时检测事件的位置, bytecode
然后使用(已编译但已修改的)类

Also detecting where the events i want to track and inject pieces into the bytecode And then use the (already compiled but modified) class

最好的/最干净的方式?有更好的方法吗?

What is the best/cleanest way? Is there a better way to do this?

如果你去Java路由,你不想使用regex - 你想要一个真正的java解析器。这可能会影响你的决定。注意,Oracle JVM包括一个,作为实现Java编译器的内部私有类的一部分,所以如果你不想要,你实际上不必自己写一个。但解码Oracle AST不是一个5分钟的任务。并且,当然,如果这很重要,使用那不是可移植的。

If you go for the Java route, you don't want to use regexes -- you want a real java parser. So that may influence your decision. Mind, the Oracle JVM includes one, as part of their internal private classes that implement the java compiler, so you don't actually have to write one yourself if you don't want to. But decoding the Oracle AST is not a 5 minute task either. And, of course, using that is not portable if that's important.

如果你去ASM路由,字节码最初将更容易分析,因为语义是很简单。分析的简单性是否超过了不熟悉程度在解决方案的净时间方面是未知的。最后,在生成代码方面,两者都不是更好。

If you go the ASM route, the bytecode will initially be easier to analyze, since the semantics are a lot simpler. Whether the simplicity of analyses outweighs the unfamiliarity is unknown in terms of net time to your solution. In the end, in terms of generated code, neither is "better".

有一个明显的简单性只是看看生成的java源代码和知道你看到的是什么你做的原始转储类文件调试和等等,但所有显而易见的简单性是因为你已经存在的舒适性与Java lanaguage。一旦你花了一些时间挖掘通过字节码,也,将变得舒适。只是一个问题,是否值得你到达那里的时间。

There is an apparent simplicity of just looking at generated java source code and "knowing" that What You See Is What You Get vs doing primitive dumps of class files for debugging and etc., but all that apparently simplicity is there because of your already existing comfortability with the Java lanaguage. Once you spend some time dredging through byte code that, too, will become comfortable. Just a question whether it's worth the time to you to get there in the first place.