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.
License file (Also see the copyright notices below) | |
Tutorial | |
The Javassist jar file (class files) | |
The source files | |
The top page of the Javassist API document. | |
Sample programs |
JDK 1.4 or later is needed.
Run the sample-all task. Otherwise, follow the instructions below.
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:
-classpath ".:javassist.jar"
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:
export CLASSPATH=.:javassist.jar
or if the operating system is Windows:
set CLASSPATH=.;javassist.jar
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.
This is a very simple program using Javassist.
To run, type the commands:
% javac sample/Test.java % java sample.Test
For more details, see sample/Test.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:
% javac sample/reflect/*.java % java javassist.tools.reflect.Loader sample.reflect.Main Joe
Compare this result with that of the regular execution without reflection:
% java sample.reflect.Person Joe
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:
% java javassist.tools.reflect.Compiler sample.reflect.Person -m sample.reflect.VerboseMetaobj
Then,
% java sample.reflect.Person Joe
This is another example of reflective programming.
To run, type the commands:
% javac sample/duplicate/*.java % java sample.duplicate.Main
Compare this result with that of the regular execution without reflection:
% java sample.duplicate.Viewer
For more details, see sample/duplicate/Main.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:
% javac sample/vector/*.java % java sample.preproc.Compiler sample/vector/Test.j % javac sample/vector/Test.java % java sample.vector.Test
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
This demonstrates the javassist.rmi package.
To run, type the commands:
% javac sample/rmi/*.java % java sample.rmi.Counter 5001
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:
% java javassist.web.Viewer localhost 5001 sample.rmi.CountApplet
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:
% javac sample/evolve/*.java % java sample.evolve.DemoLoader 5003
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.)
This shows dynamic class reloading by the JPDA. It needs JDK 1.4 or later. To run, first type the following commands:
% cd sample/hotswap % javac *.java % cd logging % javac *.java % cd ..
If your Java is 1.4, then type:
% java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 Test
If you are using Java 5, then type:
% java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 Test
Note that the class path must include JAVA_HOME/lib/tools.jar
.
To know the version number, type this command:
% java -jar javassist.jar
Javassist provides a class file viewer for debugging. For more details, see javassist.Dump.
-version 3.14 on October 5, 2010
-version 3.13 on July 19, 2010
-version 3.12.1 on June 10, 2010
-version 3.12 on April 16, 2010
-version 3.11 on July 3, 2009
-version 3.10 on March 5, 2009
-version 3.9 on October 9, 2008
-version 3.8.1 on July 17, 2008
-version 3.8.0 on June 13, 2008
-version 3.7.1 on March 10, 2008
-version 3.7 on January 20, 2008
-version 3.6.0 on September 13, 2007
-version 3.6.0.CR1 on July 27, 2007
-version 3.5 on April 29, 2007
-version 3.4 on November 17, 2006
javassist.util.proxy
have been fixed
(JASSIST-28).
Now generated proxy classes are cached. To turn the caching off,
set ProxyFactory.useCache
to false
.
-version 3.3 on August 17, 2006
ProtectionDomain
(JASSIST-23).
Now ClassPool#toClass(CtClass, ClassLoader) should not be overridden. All
subclasses of ClassPool must override toClass(CtClass, ClassLoader,
ProtectionDomain).
-version 3.2 on June 21, 2006
- version 3.2.0.CR2 on May 9, 2006
- version 3.2.0.CR1 on March 18, 2006
- version 3.1 on February 23, 2006
- version 3.1 RC2 on September 7, 2005
- version 3.1 RC1 on August 29, 2005
CtClass.getAnnotations()
- version 3.0 on January 18, 2005
- version 3.0 RC1 on September 13, 2004.
.class
notation has been supported. The modified class
file needs javassist.runtime.DotClass at runtime.
CtClass.getMethods()
has been fixed.
- version 3.0 beta on May 18th, 2004.
- version 2.6 in August, 2003.
- version 2.5.1 in May, 2003.
Simple changes for integration with JBoss AOP
- version 2.5 in May, 2003.
From this version, Javassist is part of the JBoss project.
- version 2.4 in February, 2003.
- version 2.3 in December, 2002.
- version 2.2 in October, 2002.
javassist.expr
has been added.
This is replacement of classic CodeConverter
.
- version 2.1 in July, 2002.
- version 2.0 (major update) in November, 2001.
version 1.0 in July, 2001.
- version 0.8
- version 0.7
- version 0.6
- version 0.5
- version 0.4
- version 0.3
- version 0.2
-version 0.1 on April 16, 1999.
Javassist, a Java-bytecode translator toolkit.
The contents of this software, Javassist, are subject to
the Mozilla Public License Version 1.1 (the "License"); Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF The Original Code is Javassist.
The Initial Developer of the Original Code is Shigeru Chiba.
Portions created by the Initial Developer are Contributor(s): ______________________________________.
Alternatively, the contents of this software may be used under the
terms of the GNU Lesser General Public License Version 2.1 or later
(the "LGPL"), in which case the provisions of the LGPL are applicable
instead of those above. If you wish to allow use of your version of
this software only under the terms of the LGPL, and not to allow others to
use your version of this software under the terms of the MPL, indicate
your decision by deleting the provisions above and replace them with
the notice and other provisions required by the LGPL. If you do not
delete the provisions above, a recipient may use your version of this
software under the terms of either the MPL or the LGPL.
If you obtain this software as part of JBoss, the contents of this
software may be used under only the terms of the LGPL. To use them
under the MPL, you must obtain a separate package including only
Javassist but not the other part of JBoss.
All the contributors to the original source tree must agree to
the original license term described above.
The development of this software is sponsored in part by the PRESTO
and CREST programs of Japan
Science and Technology Corporation.
I'd like to thank Michiaki Tatsubori, Johan Cloetens,
Philip Tomlinson, Alex Villazon, Pascal Rapicault, Dan HE, Eric Tanter,
Michael Haupt, Toshiyuki Sasaki, Renaud Pawlak, Luc Bourlier,
Eric Bui, Lewis Stiller, Susumu Yamazaki, Rodrigo Teruo Tomita,
Marc Segura-Devillechaise, Jan Baudisch, Julien Blass, Yoshiki Sato,
Fabian Crabus, Bo Norregaard Jorgensen, Bob Lee, Bill Burke,
Remy Sanlaville, Muga Nishizawa, Alexey Loubyansky, Saori Oki,
Andreas Salathe, Dante Torres estrada, S. Pam, Nuno Santos,
Denis Taye, Colin Sampaleanu, Robert Bialek, Asato Shimotaki,
Howard Lewis Ship, Richard Jones, Marjan Sterjev,
Bruce McDonald, Mark Brennan, Vlad Skarzhevskyy,
Brett Randall, Tsuyoshi Murakami, Nathan Meyers, Yoshiyuki Usui
Yutaka Sunaga, Arjan van der Meer, Bruce Eckel, Guillaume Pothier,
Kumar Matcha, Andreas Salathe, Renat Zubairov, Armin Haaf,
Emmanuel Bernard
and all other contributors for their contributions.
Copyright notices
Copyright (C) 1999-2010 Shigeru Chiba. All Rights Reserved.
you may not use this software except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
ANY KIND, either express or implied.
See the License for the specific language governing rights and
limitations under the License.
Copyright (C) 1999-2010 Shigeru Chiba. All Rights Reserved.
Acknowledgments
Shigeru Chiba
(Email: chiba@acm.org)
Dept. of Math. and Computing Sciences,
Tokyo Institute of Technology