Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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), and if it were simpler or less expensive to manipulate the bytecode than to edit all the source files (such as might be the case if the source code is unavailable or from many different sources, not all of which may be under one's control or for which it might be expensive and time-consuming to convince those teams to add such hooks), this might be a case where it would make sense to insert the modifications to the final bytecode output rather than to attempt to modify the original code (which might require upstreaming or maintaining a separate fork, or purchasing the source code from a third party that supplies only the bytecode).

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.

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top