Take the 2-minute tour ×

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.


share|improve this question

3 Answers 3

up vote 2 down vote accepted

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)

share|improve this answer
Thanks! I think I'll try AspectJ. –  Rafael Regis Jul 25 '12 at 16:34

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.

share|improve this answer
Thanks! I will try aspectJ. –  Rafael Regis Jul 25 '12 at 16:40

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.

share|improve this answer
Nice paper and tools. The project is a academic tool based on a new test model. In our context, it's better to develop a new one. But thanks! –  Rafael Regis Jul 25 '12 at 16:39

Your Answer


