Question

ASM and Javassist seem to have different advantages and shortcomings for generating/transforming Java bytecode. For example, the byte code generation facility of Javassist is really easy to use since it is as simple as generating your program in a string and compile it on the fly. However, sometimes the visitor approach of ASM seems more convenient for other kind of tasks (e.g., such as translating a Java routine to another language).

Is it possible to use both libraries in the same Java project so I could profit from the most convenient library according to the requirements of each different scenario ?. Any special consideration to keep in mind ?. I have the intuition that I cannot use both libraries for manipulating the byte code of the same class, but maybe even that is possible ?.

Was it helpful?

Solution

From the common sense, if you troubled yourself to learn ASM and low-level Java bytecode, the Javassist will be somewhat redundant. But from practical point of view nothing stops you from using both. If I am not mistaken, Hibernate does exactly that.

As for using ASM and Javassist on the same class, without going into specifics of each framework API, if you work just with byte array, you can just load and transform with one tool, get the result byte array and use another framework to process that byte array.

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