简体   繁体   中英

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). 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.

My question is: It's the only way to instrument Java applications? 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). The main library is AspectJ which also mostly defines the area.

The third option that might be interesting (since you are just starting out with the program) is using 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.

In terms of complexity I would probably rate it:

  • spring (easiest)
  • 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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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