简体   繁体   English

Java字节码检测的替代方案

[英]Alternatives to Java bytecode instrumentation

I'm starting a project that will have to instrument java applications for coverage purposes (definition-usage of variables, etc). 我正在开始一个项目,必须为了覆盖目的(变量的定义 - 使用等)来检测java应用程序。 It has to add trace statements and some logic to the application and maybe remove statements. 它必须向应用程序添加跟踪语句和一些逻辑,并可能删除语句。

I have searched for ways of instrument Java code and what I always find is about bytecode instrumentation. 我已经搜索了仪器Java代码的方法,我总是找到关于字节码检测的方法。

My question is: It's the only way to instrument Java applications? 我的问题是:这是测试Java应用程序的唯一方法吗? There is any other way to do that? 有没有其他方法可以做到这一点? What are the advantages of bytecode instrumentation over the others? 字节码检测相对于其他检测有什么优势?

I'll probably use the bytecode solution, but I want to know what are the problems with the other approaches (if any) to decide precisely. 我可能会使用字节码解决方案,但我想知道其他方法(如果有的话)有什么问题需要精确决定。

Thanks! 谢谢!

The other method close to changing bytecode is using AOP (Aspect Oriented Programming). 接近改变字节码的另一种方法是使用AOP(面向方面​​编程)。 The main library is AspectJ which also mostly defines the area. 主库是AspectJ ,它主要定义了该区域。

The third option that might be interesting (since you are just starting out with the program) is using Spring . 可能有趣的第三个选项(因为你刚开始使用该程序)是使用Spring It means you will have to learn a bit about IOC (inversion of control) but it basically means that instead of creating your objects yourself you let spring do it for you, and it has it advantages because when spring is incharge of the creation it can add all sorts of things in the creation process without you having to really declare it all yourself in aspectj. 这意味着你将需要学习一些关于IOC(控制反转)的内容,但它基本上意味着不是自己创建你的对象,而是让spring为你做,它有它的优点,因为当spring充满创造时它可以在创建过程中添加各种各样的东西,而不必在aspectj中自己声明它。

In terms of complexity I would probably rate it: 就复杂性而言,我可能会评价它:

  • spring (easiest) 春天(最简单)
  • aspectj AspectJ的
  • bytecode instrumentation (hardest) 字节码检测(最难)

but it's exactly the other way around when talking about capabilities (power). 但谈到能力(力量)时,恰恰相反。 for example doing something like substracting code is only possible using the last one (I think) 例如做一些像减去代码的东西只能使用最后一个(我认为)

You should definitely check out AspectJ 你一定要看看AspectJ

From what you describe you will be able to do what you want with it. 根据你的描述,你将能够用它做你想做的事。

Doing bytecode instrumentation yourself is absolutely possible but it much more complicated. 自己做字节码检测是绝对可能的,但它要复杂得多。

I think you should check out AsepctJ first and got back to do bytecode instrumentation yourself as last resort. 我想你应该先检查一下AsepctJ,然后自己回去做字节码检测。

See my paper on building coverage tools using program transformation engines. 请参阅我使用程序转换引擎构建覆盖率工具的论文。 This approach has the advantage that it can be used on arbitrary programming languages. 这种方法的优点是它可以用于任意编程语言。 In addition, it sees the source code the way the programmer sees it, not as compiled byte codes (as generics get more complex, and get ground into finer byte codes, it gets harder to understand that source code by inspecting the byte code). 此外,它以程序员看到的方式看待源代码,而不是编译的字节代码(因为泛型变得更复杂,并且变得更精细的字节代码,通过检查字节代码更难理解源代码)。

It is perhaps worth noting that program transformation generalizes aspect-oriented programming. 值得注意的是,程序转换概括了面向方面的编程。

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

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