Class Loader
- java.lang.Object
-
- java.lang.ClassLoader
-
- org.hotswap.agent.javassist.Loader
-
- org.hotswap.agent.javassist.tools.reflect.Loader
-
public class Loader extends 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.tools.reflect.Loader cl = (javassist.tools.reflect.Loader)Main.class.getClassLoader(); cl.makeReflective("Person", "MyMetaobject", "org.hotswap.agent.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()witharg1, ... andMain.main()runsMyApp.main()witharg1, ... ThePersonclass is modified to be a reflective class. Method calls on aPersonobject are intercepted by an instance ofMyMetaobject.Also, you can run
MyAppin 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", "org.hotswap.agent.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
Mainis loaded byjavassist.tools.reflect.Loaderwhereas the classMain2is not. Thus,Mainbelongs to the same name space (security domain) asMyAppwhereasMain2does not;Main2belongs to the same name space asjavassist.tools.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", "org.hotswap.agent.javassist.tools.reflect.ClassMetaobject"); cl.run("MyApp", args); } }Note:
javassist.tools.reflect.Loaderdoes not make a class reflective if that class is in ajava.*orjavax.*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.Compilerand the original class files should be replaced.- See Also:
javassist.tools.reflect.Reflection,javassist.tools.reflect.Compiler,javassist.Loader
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.hotswap.agent.javassist.Loader
Loader.Simple
-
-
Field Summary
Fields Modifier and Type Field Description protected Reflectionreflection-
Fields inherited from class org.hotswap.agent.javassist.Loader
doDelegation
-
-
Constructor Summary
Constructors Constructor Description Loader()Constructs a new class loader.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidmain(String[] args)Loads a class with an instance ofLoaderand callsmain()in that class.booleanmakeReflective(String clazz, String metaobject, String metaclass)Produces a reflective class.-
Methods inherited from class org.hotswap.agent.javassist.Loader
addTranslator, delegateLoadingOf, delegateToParent, findClass, loadClass, loadClassByDelegation, run, run, setClassPool, setDomain
-
Methods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResource, findResources, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
-
-
-
Field Detail
-
reflection
protected Reflection reflection
-
-
Constructor Detail
-
Loader
public Loader() throws CannotCompileException, NotFoundExceptionConstructs a new class loader.
-
-
Method Detail
-
main
public static void main(String[] args) throws Throwable
Loads a class with an instance ofLoaderand callsmain()in that class.- Parameters:
args- command line parameters.
args[0]is the class name to be loaded.
args[1..n]are parameters passed to the targetmain().- Throws:
Throwable
-
makeReflective
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- the reflective class.metaobject- the class of metaobjects. It must be a subclass ofMetaobject.metaclass- the class of the class metaobject. It must be a subclass ofClassMetaobject.- Returns:
falseif the class is already reflective.- Throws:
CannotCompileExceptionNotFoundException- See Also:
javassist.tools.reflect.Metaobject,javassist.tools.reflect.ClassMetaobject
-
-