|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.ClassLoader
classUtils.javassist.Loader
classUtils.javassist.reflect.Loader
public class Loader
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.reflect.Loader cl
= (javassist.reflect.Loader)Main.class.getClassLoader();
cl.makeReflective("Person", "MyMetaobject",
"javassist.reflect.ClassMetaobject");
cl.run("MyApp", args);
}
}
Then run this program as follows:
% java javassist.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.reflect.Loader cl = new javassist.reflect.Loader();
cl.makeReflective("Person", "MyMetaobject",
"javassist.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
Mainis loaded byjavassist.reflect.Loaderwhereas the classMain2is not. Thus,Mainbelongs to the same name space (security domain) asMyAppwhereasMain2does not;Main2belongs to the same name space asjavassist.reflect.Loader. For more details, see the notes in the manual page ofjavassist.Loader.The class
Main2is 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.reflect.ClassMetaobject");
cl.run("MyApp", args);
}
}
Note:
javassist.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.reflect.Compiler and the original
class files should be replaced.
Reflection,
Compiler,
Loader| Field Summary |
|---|
| Fields inherited from class classUtils.javassist.Loader |
|---|
doDelegation |
| Constructor Summary | |
|---|---|
Loader()
Constructs a new class loader. |
|
| Method Summary | |
|---|---|
static void |
main(java.lang.String[] args)
Loads a class with an instance of Loader
and calls main() in that class. |
boolean |
makeReflective(java.lang.String clazz,
java.lang.String metaobject,
java.lang.String metaclass)
Produces a reflective class. |
| Methods inherited from class classUtils.javassist.Loader |
|---|
delegateLoadingOf, run, run, setClassPool |
| Methods inherited from class java.lang.ClassLoader |
|---|
clearAssertionStatus, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Loader()
| Method Detail |
|---|
public static void main(java.lang.String[] args)
throws java.lang.Throwable
Loader
and calls main() in that class.
args[0] class name to be loaded.
args[1-n] parameters passed to main().
java.lang.Throwable
public boolean makeReflective(java.lang.String clazz,
java.lang.String metaobject,
java.lang.String metaclass)
throws CannotCompileException,
NotFoundException
clazz - the reflective class.metaobject - the class of metaobjects.
It must be a subclass of
Metaobject.metaclass - the class of the class metaobject.
It must be a subclass of
ClassMetaobject.
false if the class is already reflective.
CannotCompileException
NotFoundExceptionMetaobject,
ClassMetaobject
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||