Top Description Inners Fields Constructors Methods
javassist

public Class Loader

extends ClassLoader
Class Inheritance
Known Direct Subclasses
javassist.tools.reflect.Loader
Imports
java.io.IOException, .InputStream, java.lang.reflect.InvocationTargetException, java.security.ProtectionDomain, java.util.Arrays, .HashMap, .Vector, javassist.bytecode.ClassFile

The class loader for Javassist.

This is a sample class loader using ClassPool. Unlike a regular class loader, this class loader obtains bytecode from a ClassPool.

Note that Javassist can be used without this class loader; programmers can define their own versions of class loader. They can run a program even without any user-defined class loader if that program is statically translated with Javassist. This class loader is just provided as a utility class.

Suppose that an instance of MyTranslator implementing the interface Translator is responsible for modifying class files. The startup program of an application using MyTranslator should be something like this:

import javassist.*;

public class Main {
  public static void main(String[] args) throws Throwable {
    MyTranslator myTrans = new MyTranslator();
    ClassPool cp = ClassPool.getDefault();
    Loader cl = new Loader(cp);
    cl.addTranslator(cp, myTrans);
    cl.run("MyApp", args);
  }
}

Class MyApp is the main program of the application.

This program should be executed as follows:

% java Main arg1 arg2...

It modifies the class MyApp with a MyTranslator object before the JVM loads it. Then it calls main() in MyApp with arguments arg1, arg2, ...

This program execution is equivalent to:

% java MyApp arg1 arg2...

except that classes are translated by MyTranslator at load time.

If only a particular class must be modified when it is loaded, the startup program can be simpler; MyTranslator is unnecessary. For example, if only a class test.Rectangle is modified, the main() method above will be the following:

ClassPool cp = ClassPool.getDefault();
Loader cl = new Loader(cp);
CtClass ct = cp.get("test.Rectangle");
ct.setSuperclass(cp.get("test.Point"));
cl.run("MyApp", args);

This program changes the super class of the test.Rectangle class.

Note 1:

This class loader does not allow the users to intercept the loading of java.* and javax.* classes (and sun.*, org.xml.*, ...) unless Loader.doDelegation is false. This is because the JVM prohibits a user class loader from loading a system class. Also see Note 2. If this behavior is not appropriate, a subclass of Loader must be defined and loadClassByDelegation() must be overridden.

Note 2:

If classes are loaded with different class loaders, they belong to separate name spaces. If class C is loaded by a class loader CL, all classes that the class C refers to are also loaded by CL. However, if CL delegates the loading of the class C to CL', then those classes that the class C refers to are loaded by a parent class loader CL' instead of CL.

If an object of class C is assigned to a variable of class C belonging to a different name space, then a ClassCastException is thrown.

Because of the fact above, this loader delegates only the loading of javassist.Loader and classes included in package java.* and javax.* to the parent class loader. Other classes are directly loaded by this loader.

For example, suppose that java.lang.String would be loaded by this loader while java.io.File is loaded by the parent class loader. If the constructor of java.io.File is called with an instance of java.lang.String, then it may throw an exception since it accepts an instance of only the java.lang.String loaded by the parent class loader.

See Also
javassist.ClassPool, javassist.Translator

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Loader.Simple

A simpler class loader.

Field Summary

Modifier and TypeField and Description
public boolean
doDelegation

Specifies the algorithm of class loading.

private ProtectionDomain
private HashMap<String, ClassLoader>
private Vector<String>
private ClassPool
private Translator

Constructor Summary

AccessConstructor and Description
public
Loader()

Creates a new class loader.

public
Loader(ClassPool
the source of class files.
cp
)

Creates a new class loader.

public
Loader(ClassLoader
the parent class loader.
parent
,
ClassPool
the source of class files.
cp
)

Creates a new class loader using the specified parent class loader for delegation.

Method Summary

Modifier and TypeMethod and Description
public void
addTranslator(ClassPool
the ClassPool object for obtaining a class file.
cp
,
Translator
a translator.
t
)

Adds a translator, which is called whenever a class is loaded.

public void
delegateLoadingOf(String classname)

Records a class so that the loading of that class is delegated to the parent class loader.

protected Class<?>
protected Class<?>

Returns:

null if the specified class could not be found.
findClass
(String
The binary name of the class
name
)

Overrides java.lang.ClassLoader.findClass.

Finds the specified class using ClassPath.

private void
private boolean
protected Class<?>
loadClass(String
The binary name of the class
name
,
boolean
If true then resolve the class
resolve
)

Overrides java.lang.ClassLoader.loadClass.

Requests the class loader to load a class.

protected Class<?>
public static void
main(String[]
command line parameters.
  args[0] is the class name to be loaded.
  args[1..n] are parameters passed to the target main().
args
)

Loads a class with an instance of Loader and calls main() of that class.

private boolean
public void
run(String[]
command line parameters.
  args[0] is the class name to be loaded.
  args[1..n] are parameters passed to the target main().
args
)

Loads a class and calls main() in that class.

public void
run(String
the loaded class.
classname
,
String[]
parameters passed to main().
args
)

Loads a class and calls main() in that class.

public void
setClassPool(ClassPool cp)

Sets the soruce ClassPool.

public void
setDomain(ProtectionDomain d)

Sets the protection domain for the classes handled by this class loader.

Inherited from java.lang.ClassLoader:
clearAssertionStatusdefineClassdefineClassdefineClassdefineClassdefinePackagefindClassfindLibraryfindLoadedClassfindResourcefindResourcefindResourcesfindSystemClassgetClassLoadingLockgetDefinedPackagegetDefinedPackagesgetNamegetPackagegetPackagesgetParentgetPlatformClassLoadergetResourcegetResourceAsStreamgetResourcesgetSystemClassLoadergetSystemResourcegetSystemResourceAsStreamgetSystemResourcesgetUnnamedModuleisRegisteredAsParallelCapableloadClassregisterAsParallelCapableresolveClassresourcessetClassAssertionStatussetDefaultAssertionStatussetPackageAssertionStatussetSigners

Field Detail

doDelegationback to summary
public boolean doDelegation

Specifies the algorithm of class loading.

This class loader uses the parent class loader for java.* and javax.* classes. If this variable doDelegation is false, this class loader does not delegate those classes to the parent class loader.

The default value is true.

domainback to summary
private ProtectionDomain domain
notDefinedHereback to summary
private HashMap<String, ClassLoader> notDefinedHere
notDefinedPackagesback to summary
private Vector<String> notDefinedPackages
sourceback to summary
private ClassPool source
translatorback to summary
private Translator translator

Constructor Detail

Loaderback to summary
public Loader()

Creates a new class loader.

Loaderback to summary
public Loader(ClassPool cp)

Creates a new class loader.

Parameters
cp:ClassPool

the source of class files.

Loaderback to summary
public Loader(ClassLoader parent, ClassPool cp)

Creates a new class loader using the specified parent class loader for delegation.

Parameters
parent:ClassLoader

the parent class loader.

cp:ClassPool

the source of class files.

Method Detail

addTranslatorback to summary
public void addTranslator(ClassPool cp, Translator t) throws NotFoundException, CannotCompileException

Adds a translator, which is called whenever a class is loaded.

Parameters
cp:ClassPool

the ClassPool object for obtaining a class file.

t:Translator

a translator.

Exceptions
NotFoundException:
if t.start() throws an exception.
CannotCompileException:
if t.start() throws an exception.
delegateLoadingOfback to summary
public void delegateLoadingOf(String classname)

Records a class so that the loading of that class is delegated to the parent class loader.

If the given class name ends with . (dot), then that name is interpreted as a package name. All the classes in that package and the sub packages are delegated.

delegateToParentback to summary
protected Class<?> delegateToParent(String classname) throws ClassNotFoundException
findClassback to summary
protected Class<?> findClass(String name) throws ClassNotFoundException

Overrides java.lang.ClassLoader.findClass.

Finds the specified class using ClassPath. If the source throws an exception, this returns null.

This method can be overridden by a subclass of Loader. Note that the overridden method must not throw an exception when it just fails to find a class file.

Parameters
name:String

Doc from java.lang.ClassLoader.findClass.

The binary name of the class

Returns:Class<?>

null if the specified class could not be found.

Annotations
@Override
Exceptions
ClassNotFoundException:
if an exception is thrown while obtaining a class file.
initback to summary
private void init(ClassPool cp)
isDefinedPackageback to summary
private boolean isDefinedPackage(String name)
loadClassback to summary
protected Class<?> loadClass(String name, boolean resolve) throws ClassFormatError, ClassNotFoundException

Overrides java.lang.ClassLoader.loadClass.

Requests the class loader to load a class.

Parameters
name:String

Doc from java.lang.ClassLoader.loadClass.

The binary name of the class

resolve:boolean

Doc from java.lang.ClassLoader.loadClass.

If true then resolve the class

Returns:Class<?>

Doc from java.lang.ClassLoader.loadClass.

The resulting Class object

Annotations
@Override
Exceptions
ClassNotFoundException:

Doc from java.lang.ClassLoader.loadClass.

If the class could not be found

loadClassByDelegationback to summary
protected Class<?> loadClassByDelegation(String name) throws ClassNotFoundException
mainback to summary
public static void main(String[] args) throws Throwable

Loads a class with an instance of Loader and calls main() of that class.

This method calls run().

Parameters
args:String[]

command line parameters.
  args[0] is the class name to be loaded.
  args[1..n] are parameters passed to the target main().

See Also
javassist.Loader#run(String[])
notDelegatedback to summary
private boolean notDelegated(String name)
runback to summary
public void run(String[] args) throws Throwable

Loads a class and calls main() in that class.

Parameters
args:String[]

command line parameters.
  args[0] is the class name to be loaded.
  args[1..n] are parameters passed to the target main().

runback to summary
public void run(String classname, String[] args) throws Throwable

Loads a class and calls main() in that class.

Parameters
classname:String

the loaded class.

args:String[]

parameters passed to main().

setClassPoolback to summary
public void setClassPool(ClassPool cp)

Sets the soruce ClassPool.

setDomainback to summary
public void setDomain(ProtectionDomain d)

Sets the protection domain for the classes handled by this class loader. Without registering an appropriate protection domain, the program loaded by this loader will not work with a security manager or a signed jar file.

javassist back to summary

public Class Loader.Simple

extends ClassLoader
Class Inheritance

A simpler class loader. This is a class loader that exposes the protected defineClass() method declared in java.lang.ClassLoader. It provides a method similar to CtClass#toClass().

When loading a class, this class loader delegates the work to the parent class loader unless the loaded classes are explicitly given by invokeDefineClass(CtClass). Note that a class Foo loaded by this class loader is different from the class with the same name Foo but loaded by another class loader. This is Java's naming rule.

Since
3.24

Constructor Summary

AccessConstructor and Description
public
Simple()

Constructs a class loader.

public
Simple(ClassLoader
the parent class loader.
parent
)

Constructs a class loader.

Method Summary

Modifier and TypeMethod and Description
public Class<?>
invokeDefineClass(CtClass cc)

Invokes the protected defineClass() in ClassLoader.

Inherited from java.lang.ClassLoader:
clearAssertionStatusdefineClassdefineClassdefineClassdefineClassdefinePackagefindClassfindClassfindLibraryfindLoadedClassfindResourcefindResourcefindResourcesfindSystemClassgetClassLoadingLockgetDefinedPackagegetDefinedPackagesgetNamegetPackagegetPackagesgetParentgetPlatformClassLoadergetResourcegetResourceAsStreamgetResourcesgetSystemClassLoadergetSystemResourcegetSystemResourceAsStreamgetSystemResourcesgetUnnamedModuleisRegisteredAsParallelCapableloadClassloadClassregisterAsParallelCapableresolveClassresourcessetClassAssertionStatussetDefaultAssertionStatussetPackageAssertionStatussetSigners

Constructor Detail

Simpleback to summary
public Simple()

Constructs a class loader.

Simpleback to summary
public Simple(ClassLoader parent)

Constructs a class loader.

Parameters
parent:ClassLoader

the parent class loader.

Method Detail

invokeDefineClassback to summary
public Class<?> invokeDefineClass(CtClass cc) throws IOException, CannotCompileException

Invokes the protected defineClass() in ClassLoader. It converts the given CtClass object into a java.lang.Class object.