34

I know that Javassist is a Java library providing a means to manipulate the Java bytecode of an application.

Ok, but why we need manipulate bytecode?

Any real example? Any real app, where javassist used?

4 Answers 4

39

A common application is to generate proxy classes at runtime, i.e. to create a subclass at runtime that intercepts all method invocations. Examples:

Hibernate uses Proxies to intercept method invocations on entities to implement lazy loading, i.e. fetching the object from the database when it is first accessed.

The Spring Framework uses Proxies to implement its AOP support, which among other things powers its support for declarative transactions. It also uses proxies to enforce proper scoping.

EJB uses proxies to implement container managed transactions, authorization checking, and to apply user-defined interceptors.

CDI implementations must also proxy the managed beans to ensure proper scoping. I suspect they use a byte code engineering library, too.

I recently used Javassist to implement a transparent cache for method return values, by intercepting all method invocations and only delegating to the super implementation on the first invocation.

Note that java.lang.reflect.Proxy can generate proxy classes at runtime, but can only implement interfaces, not extend a class. All of the above use cases require the proxying of classes.

16

Bytecode manipulation is useful and necessary, especially when you do not have source code for certain projects. Say you only have the bytecode (like a jar file) for some project, but you want somehow change the behavior of the code, the bytecode manipulation library can help in such cases. The advantage of bytecode manipulation is that you don't need to recompile your code and can directly execute it after manipulation.

I have used bytecode manipulation to do some program analysis. Given a library, I want to know during the runtime what methods in the library have been invoked. I can use bytecode manipulation to insert a System.out.println("method_name"); statement in the beginning of a method. So during the runtime, it will print out what methods have been invoked.

Some bytecode manipulation libraries are:

1
  • 1
    About real projects: I know, that playFramework used Javaassist. In this case, we have source code... And i, propesed, that Hibernate use javassist too.
    – user471011
    Sep 4, 2011 at 5:49
1

To extend Meriton answer and to provide a real example of use :

Hibernate-core (5.2.8.Final) use javaassit (3.20.0-GA):

https://mvnrepository.com/artifact/org.hibernate/hibernate-core/5.2.8.Final

0

Users page of the ASM project lists several dozen widely used Java projects and frameworks using ASM for bytecode analysis and manipulation. http://asm.ow2.org/users.html

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.