# Javassist version 2

### in August, 2003.
Copyright (C) 2000-2003 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

`License.html` | License file (Also see the copyright notices below)
`tutorial/tutorial.html` | Tutorial
`./javassist.jar` | The Javassist jar file (class files)
`./src/main` | The source files
`html/index.html`
| The top page of the Javassist API document.
`./sample/` | Sample programs

To extract source files from the archive, use the jar command:

    % jar xvf javassist-src.zip

## How to run sample programs

JDK 1.2.2 or later is needed.

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

In the description below, we assume that the platform is JDK 1.2 (or later) for Solaris. If the platform is JDK 1.2 (or later) for Windows, the class-path option is:

    -classpath ".;javassist.jar"

If you don't want to put the class-path option, copy `./javassist.jar` to

<java-home>`/jre/lib/ext/`.

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

If you don't use javassist.jar, first compile Javassist:

    % cd src
    % javac -d .. javassist/*.java javassist/*/*.java
    % cd ..

Then you can compile and run the sample programs without the class-path option.

### 2. sample/Test.java

This is a very simple program using Javassist.

To run, type the commands:

    % javac -classpath ".:javassist.jar" sample/Test.java
    % java -cp ".:javassist.jar" sample.Test

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:

    % javac -classpath ".:javassist.jar" sample/reflect/*.java
    % java -cp ".:javassist.jar" javassist.reflect.Loader sample.reflect.Main Joe

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

    % java -cp ".:javassist.jar" 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 -cp ".:javassist.jar" javassist.reflect.Compiler sample.reflect.Person -m sample.reflect.VerboseMetaobj

Then,

    % java -cp ".:javassist.jar" sample.reflect.Person Joe

### 4. sample/duplicate/*.java

This is another example of reflective programming.

To run, type the commands:

    % javac -classpath ".:javassist.jar" sample/duplicate/*.java
    % java -cp ".:javassist.jar" 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

### 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. This is a demonstration of the use of `javassist.preproc` package.

To run, type the commands:

    % javac -classpath ".:javassist.jar" sample/vector/*.java
    % java -cp ".:javassist.jar" javassist.preproc.Compiler sample/vector/Test.j
    % javac sample/vector/Test.java
    % java sample.vector.Test

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:

    % javac -classpath ".:javassist.jar" sample/rmi/*.java
    % java -cp ".:javassist.jar" 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 -cp ".:javassist.jar" javassist.web.Viewer localhost 5001 sample.rmi.CountApplet

### 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:

    % javac -classpath ".:javassist.jar" sample/evolve/*.java
    % java -cp ".:javassist.jar" 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.)

### 8. Hints

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

## Changes

- version 2.6 in August, 2003.

- The behavior of CtClass.setSuperclass() was changed. To obtain the previous behavior, call CtClass.replaceClassName().
- CtConstructor.setBody() now works for class initializers.
- CtNewMethod.delegator() now works for static methods.
- javassist.expr.Expr.indexOfBytecode() has been added.
- javassist.Loader has been modified so that getPackage() returns a package object.
- Now, the compiler can correctly compile a try statement and an infinite while-loop.

- version 2.5.1 in May, 2003.
Simple changes for integration with JBoss AOP

- Made ClassPool.get0 protected so that subclasses of ClassPool can call it.
- Moved all access to the class cache (the field ClassPool.classes) to a method called getCached(String classname). This is so subclasses of ClassPool can override this behavior.

- version 2.5 in May, 2003.
From this version, Javassist is part of the JBoss project.

- The license was changed from MPL to MPL/LGPL dual.
- ClassPool.removeClassPath() and ClassPath.close() have been added.
- ClassPool.makeClass(InputStream) has been added.
- CtClass.makeClassInitializer() has been added.
- javassist.expr.Expr has been changed to a public class.
- javassist.expr.Handler has been added.
- javassist.expr.MethodCall.isSuper() has been added.
- CtMethod.isEmpty() and CtConstructor.isEmpty() have been added.
- LoaderClassPath has been implemented.

- version 2.4 in February, 2003.

- The compiler included in Javassist did not correctly work with interface methods. This bug was fixed.
- Now javassist.bytecode.Bytecode allows more than 255 local variables in the same method.
- javassist.expr.Instanceof and Cast have been added.
- javassist.expr.{MethodCall,NewExpr,FieldAccess,Instanceof,Cast}.where() have been added. They return the caller-side method surrounding the expression.
- javassist.expr.{MethodCall,NewExpr,FieldAccess,Instanceof,Cast}.mayThrow() have been added.
- $class has been introduced.
- The parameters to replaceFieldRead(), replaceFieldWrite(), and redirectFieldAccess() in javassist.CodeConverter are changed.
- The compiler could not correctly handle a try-catch statement. This bug has been fixed.

- version 2.3 in December, 2002.

- The tutorial has been revised a bit.
- SerialVersionUID class was donated by Bob Lee. Thanks.
- CtMethod.setBody() and CtConstructor.setBody() have been added.
- javassist.reflect.ClassMetaobject.useContextClassLoader has been added. If true, the reflection package does not use Class.forName() but uses a context class loader specified by the user.
- $sig and $type are now available.
- Bugs in Bytecode.write() and read() have been fixed.

- version 2.2 in October, 2002.

- The tutorial has been revised.
- A new package `javassist.expr` has been added. This is replacement of classic `CodeConverter`.
- javassist.ConstParameter was changed into javassist.CtMethod.ConstParameter.
- javassist.FieldInitializer was renamed into javassist.CtField.Initializer.
- A bug in javassist.bytecode.Bytecode.addInvokeinterface() has been fixed.
- In javassist.bytecode.Bytecode, addGetfield(), addGetstatic(), addInvokespecial(), addInvokestatic(), addInvokevirtual(), and addInvokeinterface() have been modified to update the current statck depth.

- version 2.1 in July, 2002.

- javassist.CtMember and javassist.CtBehavior have been added.
- javassist.CtClass.toBytecode() has been added.
- javassist.CtClass.toClass() and javassist.ClassPool.writeAsClass() has been added.
- javassist.ByteArrayClassPath has been added.
- javassist.bytecode.Mnemonic has been added.
- Several bugs have been fixed.

- version 2.0 (major update) in November, 2001.

- The javassist.bytecode package has been provided. It is a lower-level API for directly modifying a class file although the users must have detailed knowledge of the Java bytecode.
- The mechanism for creating CtClass objects have been changed.
- javassist.tool.Dump moves to the javassist package.

version 1.0 in July, 2001.

- javassist.reflect.Metaobject and ClassMetaobject was changed. Now they throw the same exception that they receive from a base-level object.

- version 0.8

- javassist.tool.Dump was added. It is a class file viewer.
- javassist.FiledInitializer.byNewArray() was added. It is for initializing a field with an array object.
- javassist.CodeConverter.redirectMethodCall() was added.
- javassist.Run was added.

- version 0.7

- javassit.Loader was largely modified. javassist.UserLoader was deleted. Instead, Codebase was renamed to ClassPath and UserClassPath was added. Now programmers who want to customize Loader must write a class implementing UserClassPath instead of UserLoader. This change is for sharing class search paths between Loader and CtClass.CtClass(String).
- CtClass.addField(), addMethod(), addConstructor(), addWrapper() were also largely modified so that it receives CtNewMethod, CtNewConstructor, or CtNewField. The static methods for creating these objects were added to the API.
- Constructors are now represented by CtConstructor objects. CtConstructor is a subclass of CtMethod.
- CtClass.getUserAttribute() was removed. Use CtClass.getAttribute().
- javassist.rmi.RmiLoader was added.
- javassist.reflect.Metalevel._setMetaobject() was added. Now metaobjects can be replaced at runtime.

- version 0.6

- Javassist was modified to correctly deal with array types appearing in signatures.
- A bug crashed resulting bytecode if a class includes a private static filed. It has been fixed.
- javassist.CtNewInterface was added.
- javassist.Loader.recordClass() was renamed into makeClass().
- javassist.UserLoader.loadClass() was changed to take the second parameter.

- version 0.5

- a bug-fix version.

- version 0.4

- Major update again. Many classes and methods were changed. Most of methods taking java.lang.Class have been changed to take javassist.CtClass.

- version 0.3

- Major update. Many classes and methods were changed.

- version 0.2

- Jar/zip files are supported.

-version 0.1 in April, 1999.

- The first release.

## Bug reports etc.

Bug reports:
  Post your reports at Forums or directly send an email to:
  `chiba@acm.org` or `chiba@is.titech.ac.jp`

The home page of Javassist:
  Visit `www.javassist.org` and `www.jboss.org`

## Copyright notices

Javassist, a Java-bytecode translator toolkit.
Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.

The contents of this software, Javassist, are subject to the Mozilla Public License Version 1.1 (the "License");
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/

Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
ANY KIND, either express or implied. See the License for the specific language governing rights and
limitations under the License.

The Original Code is Javassist.

The Initial Developer of the Original Code is Shigeru Chiba. Portions created by the Initial Developer are
Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.

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.

## Acknowledgments

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, and Howard Lewis Ship for their contributions.

Shigeru Chiba (Email: `chiba@acm.org`)
Dept. of Math. and Computing Sciences, Tokyo Institute of Technology