Javassist version 3

Copyright (C) 1999-2018 by Shigeru Chiba, All rights reserved.


Javassist (JAVA programming ASSISTant) makes Java bytecode manipulation simple. It is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file when the JVM loads it. Unlike other similar bytecode editors, Javassist provides two levels of API: source level and bytecode level. If the users use the source-level API, they can edit a class file without knowledge of the specifications of the Java bytecode. The whole API is designed with only the vocabulary of the Java language. You can even specify inserted bytecode in the form of source text; Javassist compiles it on the fly. On the other hand, the bytecode-level API allows the users to directly edit a class file as other editors.


Files


How to run sample programs

JDK 1.4 or later is needed.

0. If you have Apache Ant

Run the sample-all task. Otherwise, follow the instructions below.

1. Move to the directory where this Readme.html file is located.

In the following instructions, we assume that the javassist.jar file is included in the class path. For example, the javac and java commands must receive the following classpath option:

If the operating system is Windows, the path separator must be not : (colon) but ; (semicolon). The java command can receive the -cp option as well as -classpath.

If you don't want to use the class-path option, you can make javassist.jar included in the CLASSPATH environment:

or if the operating system is Windows:

Otherwise, you can copy javassist.jar to the directory

<java-home> depends on the system. It is usually /usr/local/java, c:\j2sdk1.4\, etc.

2. sample/Test.java

This is a very simple program using Javassist.

To run, type the commands:

For more details, see sample/Test.java

3. sample/reflect/*.java

This is the "verbose metaobject" example well known in reflective programming. This program dynamically attaches a metaobject to a Person object. The metaobject prints a message if a method is called on the Person object.

To run, type the commands:

Compare this result with that of the regular execution without reflection:

For more details, see sample/reflect/Main.java

Furthermore, the Person class can be statically modified so that all the Person objects become reflective without sample.reflect.Main. To do this, type the commands:

Then,

4. sample/duplicate/*.java

This is another example of reflective programming.

To run, type the commands:

Compare this result with that of the regular execution without reflection:

For more details, see sample/duplicate/Main.java

5. sample/vector/*.java

This example shows the use of Javassit for producing a class representing a vector of a given type at compile time.

To run, type the commands:

Note: javassist.jar is unnecessary to compile and execute sample/vector/Test.java. For more details, see sample/vector/Test.j and sample/vector/VectorAssistant.java

6. sample/rmi/*.java

This demonstrates the javassist.rmi package.

To run, type the commands:

The second line starts a web server listening to port 5001.

Then, open sample/rmi/webdemo.html with a web browser running on the local host. (webdemo.html trys to fetch an applet from http://localhost:5001/, which is the web server we started above.)

Otherwise, run sample.rmi.CountApplet as an application:

7. sample/evolve/*.java

This is a demonstration of the class evolution mechanism implemented with Javassist. This mechanism enables a Java program to reload an existing class file under some restriction.

To run, type the commands:

The second line starts a class loader DemoLoader, which runs a web server DemoServer listening to port 5003.

Then, open http://localhost:5003/demo.html with a web browser running on the local host. (Or, see sample/evolve/start.html.)

8. sample/hotswap/*.java

This shows dynamic class reloading by the JPDA. It needs JDK 1.4 or later. To run, first type the following commands:

If your Java is 1.4, then type:

If you are using Java 5, then type:

Note that the class path must include JAVA_HOME/lib/tools.jar.

Hints

To know the version number, type this command:

Javassist provides a class file viewer for debugging. For more details, see javassist.Dump.


Changes

-version 3.24.1 on December 9, 2018