Top Description Fields Constructors Methods
javassist.tools.reflect

public Class Loader

extends Loader
Class Inheritance
Imports
javassist.CannotCompileException, .ClassPool, .NotFoundException

A class loader for reflection.

To run a program, say MyApp, including a reflective class, you must write a start-up program as follows:

public class Main {
  public static void main(String[] args) throws Throwable {
    javassist.tools.reflect.Loader cl
        = (javassist.tools.reflect.Loader)Main.class.getClassLoader();
    cl.makeReflective("Person", "MyMetaobject",
                      "javassist.tools.reflect.ClassMetaobject");
    cl.run("MyApp", args);
  }
}

Then run this program as follows:

% java javassist.tools.reflect.Loader Main arg1, ...

This command runs Main.main() with arg1, ... and Main.main() runs MyApp.main() with arg1, ... The Person class is modified to be a reflective class. Method calls on a Person object are intercepted by an instance of MyMetaobject.

Also, you can run MyApp in a slightly different way:

public class Main2 {
  public static void main(String[] args) throws Throwable {
    javassist.tools.reflect.Loader cl = new javassist.tools.reflect.Loader();
    cl.makeReflective("Person", "MyMetaobject",
                      "javassist.tools.reflect.ClassMetaobject");
    cl.run("MyApp", args);
  }
}

This program is run as follows:

% java Main2 arg1, ...

The difference from the former one is that the class Main is loaded by javassist.tools.reflect.Loader whereas the class Main2 is not. Thus, Main belongs to the same name space (security domain) as MyApp whereas Main2 does not; Main2 belongs to the same name space as javassist.tools.reflect.Loader. For more details, see the notes in the manual page of javassist.Loader.

The class Main2 is equivalent to this class:

public class Main3 {
  public static void main(String[] args) throws Throwable {
    Reflection reflection = new Reflection();
    javassist.Loader cl
        = new javassist.Loader(ClassPool.getDefault(reflection));
    reflection.makeReflective("Person", "MyMetaobject",
                              "javassist.tools.reflect.ClassMetaobject");
    cl.run("MyApp", args);
  }
}

Note:

javassist.tools.reflect.Loader does not make a class reflective if that class is in a java.* or javax.* pacakge because of the specifications on the class loading algorithm of Java. The JVM does not allow to load such a system class with a user class loader.

To avoid this limitation, those classes should be statically modified with javassist.tools.reflect.Compiler and the original class files should be replaced.

See Also
javassist.tools.reflect.Reflection, javassist.tools.reflect.Compiler, javassist.Loader

Field Summary

Modifier and TypeField and Description
protected Reflection
Inherited from javassist.Loader:
doDelegation

Constructor Summary

AccessConstructor and Description
public
Loader()

Constructs a new class loader.

Method Summary

Modifier and TypeMethod and Description
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
)

Hides javassist.Loader.main.

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

public boolean

Returns:

false if the class is already reflective.
makeReflective
(String
the reflective class.
clazz
,
String
the class of metaobjects. It must be a subclass of Metaobject.
metaobject
,
String
the class of the class metaobject. It must be a subclass of ClassMetaobject.
metaclass
)

Produces a reflective class.

Inherited from javassist.Loader:
addTranslatordelegateLoadingOfdelegateToParentfindClassloadClassloadClassByDelegationrunrunsetClassPoolsetDomain

Field Detail

reflectionback to summary
protected Reflection reflection

Constructor Detail

Loaderback to summary
public Loader() throws CannotCompileException, NotFoundException

Constructs a new class loader.

Method Detail

mainback to summary
public static void main(String[] args) throws Throwable

Hides javassist.Loader.main.

Loads a class with an instance of Loader 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().

makeReflectiveback to summary
public boolean makeReflective(String clazz, String metaobject, String metaclass) throws CannotCompileException, NotFoundException

Produces a reflective class. If the super class is also made reflective, it must be done before the sub class.

Parameters
clazz:String

the reflective class.

metaobject:String

the class of metaobjects. It must be a subclass of Metaobject.

metaclass:String

the class of the class metaobject. It must be a subclass of ClassMetaobject.

Returns:boolean

false if the class is already reflective.

See Also
javassist.tools.reflect.Metaobject, javassist.tools.reflect.ClassMetaobject