Many types of applications rely on the ability to alter Java bytecode prior to loading classes into the JVM. Code verifiers, coverage analyzers, O/R mapping tools, distributed object caches, or profilers are just a few examples of applications that need the ability to enhance Java bytecode.
Several open-source tools have been providing programmatic access to byte codes, such as ASM and Javassist. In a recent blog post, Jason Green, a lead developer on JBoss Cache, describes in a recent blog post, Javassist now provides data-flow analysis API , why he needed to add dataflow analysis capabilities to Javassist:
A lot of information is lost once a Java program is compiled, since it is not really needed when the program is executed, and leaving it out helps keep class files small...
For security reasons, the JVM actually does its own data-flow analysis to verify that a class does not violate type rules before it can be ran. This poses an interesting challenge to any application that manipulates bytecode, since any change that affects the possible type-state can lead to a verify error and the JVM throwing out the class...
To get around that problem, Greene added the ability to perform such dataflow analysis from within Javassist:
The framework allows an application to determine, by inference, the type-state of the local variable table and stack frame at the start of every bytecode instruction...
The analysis framework provides this by modeling the effect of every instruction, until it can eventually infer the type information. This process does not use any debugging information, since there is no guarantee it is available. Instead, it extrapolates it by tracking all possible type states, as every branch is evaluated, until the type information is reduced to the most specific type state available...
What do you think of Javassist and other bytecode manipulation libraries?
> <p>What do you think of Javassist and other bytecode > manipulation libraries?</p>
yeah, well. i've been burned by having one bytecode-hackery-thing get all fouled up with another bytecode-hackery-thing. so each worked OK individually, but could not work together. which completely turned me off of the idea of every using anything 3rd party that does bytecode hackery. of course, this was a while ago, so maybe things suck less now, but since i'm a natural pessimist i suspect they still kinda suck. or that at the very least there can easily be come gotchya waiting to jump out of the bushes at you.