views:

244

answers:

5

What legitimate uses are there for bytecode manipulation and how people implement those bytecode manipulation based solutions in practice?

Update: I should have made it more clear that this question really is about what patterns and techniques people use to make their code fly with the help of bytecode manipulation.

Something like aspect oriented programming that was already mentioned or building proxy objects on the fly and similar techniques.

+4  A: 

Bytecode manipulation lets you implement arbitrarily complex (and interesting) program transformations, such as:

  • entry/exit logging code for selected functions
  • security transformations that stub out access to certain API's
  • API substitution for, e.g., running code in a test harness.

The scope is endless; this is just a small sampling.

As for how this is typically done, start here.

Marcelo Cantos
Maybe I should have been more clear on this. I already know those libraries and I know how Java works on the bytecode level. What I really want to know is what cool techniques people invented to do things you couldn't do otherwise so I have those solutions in mind whenever I stumble over such problems.
ahe
+1  A: 

So, one can read bytecode to implement an interpreter / JVM. One can write / generate bytecode when implementing a Java compiler or a compiler for another language that will target the JVM (e.g. Scala and Jython). You might perform bytecode manipulation to optimize bytecode (if you want to produce and market a bytecode optimizer or you need it as an internal tool to give your company's code an edge over the competition). In a similar vein, you might manipulate bytecode in order to obfuscate it prior to distribution. You might also perform bytecode manipulation for aspect-oriented programming; for example, you might want to insert hooks (maybe for timing or logging purposes or for some other reason), but it would be less expensive to manipulate the bytecode than it would be to edit all the source files, etc.

You can manipulate bytecode yourself, although there are many existing open source libraries and frameworks to do it, including BCEL and ASM to name just a couple.

Michael Aaron Safyan
+2  A: 

One use for bytecode manipulation is in aspect oriented programming. In Java, you can use AspectJ for this.

Jesper
+1  A: 

There are papers Patterns of Aspect-Oriented Design (PDF) and Aspect-Oriented Design Principles: Lessons from Object-Oriented Design (PDF) which describe some patterns for AOP/bytecode manipulation.

Personally I have used bytecode manipulation with ASM in one framework to generate some boilerplate code for classes which use that framework. The framework requires custom equals() and hashCode() methods for client code, so I generate those by hooking in a Java Agent which modifies the bytecode as the ClassLoader loads the classes. I have also many times used CGLIB to produce dynamic proxies (if that counts as AOP).

Esko Luontola
A: 

Some frameworks such as BEA KODO (Implementation of the Java Data Objects specification) use bytecode manipulation to "enhance" Plain Old Java Objects and add the persistence logic, based on an XML description.

Thus, database mapping information is then automatically generated on the bytecode.

Mario Ortegón