JAVASSIST (Java programming Assistant) is a utility that allows one to inspect, edit and create Java binary classes. It enables Java programs to define a new class at runtime and to modify a given class file when the JVM loads it. Its best feature is its ease of use since it allows developers to fully exploit byte code manipulation with little or no knowledge of byte code, giving them a degree of fine-grained control.
Javassist provides two levels of API: Source level and Byte code level. If the users use source level API, they can edit a class file without knowledge of the specifications of the Java byte code. The whole API is designed with only the vocabulary of the Java language. You can even specify inserted byte code in the form of source text; Javassist compiles it on the fly. On the other hand, the byte code level API allows the users to directly edit a class file as other editors.
Javassist uses the javassist.ClassPool class to track and control the classes being manipulated. Classes loaded in a class pool are represented by javassist.CtClass instances where as fields, methods and constructors are represented by javassist.CtField, javassist.CtMethod and javassist.CtConstructor instances, respectively.
An Example of Javassist
The below example depicts the usage of few of the Javassist basic APIs. All that is required is to just download ‘javassist.jar’ and add the same to the class path.