public class ClassLoaderTransform extends Object
ClassLoaderTransform
provides an alternative to using a java agent to
perform byte-code manipulation by providing a classloader that will
automatically transform classes as they are loaded.Modifier and Type | Field and Description |
---|---|
static String |
MAIN_CLASS
Manifest property key for specifying the actual main class when using the
loading technique provided by
main(String[]) . |
static String |
TRANSFORMERS
Manifest property key for specifying the
ClassTransformer classes
when using the loading technique provided by main(String[]) . |
static String |
TRANSFORMERS_SEPARATOR
Separator character for separating multiple
ClassTransformer s
when using the loading technique provided by main(String[]) . |
Constructor and Description |
---|
ClassLoaderTransform() |
Modifier and Type | Method and Description |
---|---|
static String |
getMainClass(File jarFile)
Get the main class from the manifest of the given jar file
|
static void |
main(String[] args)
Main method for an alternative in-place loading strategy.
|
static boolean |
run(Class<?> clz,
String[] args,
ClassTransformer transformer,
ClassTransformer... transformers)
If the the given class has not already been loaded in by
Loader ,
load it in a newly created Loader that is configured to apply the
given transform(s), run the main method and return true. |
protected static javassist.Loader |
run(ClassLoader parent,
javassist.ClassPool pool,
MultiTransformClassFileTransformer tf,
String mainClass,
String[] args) |
static javassist.Loader |
run(javassist.ClassPool pool,
MultiTransformClassFileTransformer tf,
File jarFile,
String[] args)
Run the main method of the given jar file, passing all classes as they
are loaded through the transformer to modify the bytecode.
|
static javassist.Loader |
run(javassist.ClassPool pool,
MultiTransformClassFileTransformer tf,
String classpath,
String mainClass,
String[] args)
Run the main method of the given class, transforming any classes found on
the given classpath as they are loaded.
|
static void |
run(MultiTransformClassFileTransformer transformer,
Class<?> clz,
String[] args)
Re-load the given class in a newly created
Loader that is
configured to apply the given transform(s), and then run the main method. |
public static final String MAIN_CLASS
main(String[])
.public static final String TRANSFORMERS
ClassTransformer
classes
when using the loading technique provided by main(String[])
.public static final String TRANSFORMERS_SEPARATOR
ClassTransformer
s
when using the loading technique provided by main(String[])
.public ClassLoaderTransform()
public static javassist.Loader run(javassist.ClassPool pool, MultiTransformClassFileTransformer tf, File jarFile, String[] args) throws Throwable
pool
- the classpool to hold classes as they are loadedtf
- the transform(s) to applyjarFile
- the jar file to runargs
- the arguments to pass to the main methodThrowable
- if an error occurspublic static String getMainClass(File jarFile) throws IOException
jarFile
- the jar fileIOException
- if an error occurs when readingpublic static javassist.Loader run(javassist.ClassPool pool, MultiTransformClassFileTransformer tf, String classpath, String mainClass, String[] args) throws Throwable
pool
- the classpool to hold classes as they are loadedtf
- the transform(s) to applyclasspath
- the classpath on which to search for classesmainClass
- the main class with the main(String[])
methodargs
- the arguments to pass to the main methodThrowable
- if an error occurspublic static void run(MultiTransformClassFileTransformer transformer, Class<?> clz, String[] args) throws Throwable
Loader
that is
configured to apply the given transform(s), and then run the main method.transformer
- the transformerclz
- the classargs
- the argumentsThrowable
- if an error occursprotected static javassist.Loader run(ClassLoader parent, javassist.ClassPool pool, MultiTransformClassFileTransformer tf, String mainClass, String[] args) throws Throwable
Throwable
public static boolean run(Class<?> clz, String[] args, ClassTransformer transformer, ClassTransformer... transformers) throws Throwable
Loader
,
load it in a newly created Loader
that is configured to apply the
given transform(s), run the main method and return true. If the class's
classload is a Loader
then return false.clz
- the class to load, transform and runargs
- the arguments to the main methodtransformer
- the first transformertransformers
- any additional transformersThrowable
- if an error occurs.public static void main(String[] args) throws Throwable
ClassTransformer
s
on itself.
Modify the assembled jar file so that the Main-Class is set as this class
(org.openimaj.aop.classloader.ClassLoaderTransform
) and add
additional keys to the manifest to point at the actual class main (
MAIN_CLASS
) and the transformer classes (TRANSFORMERS
,
separated by TRANSFORMERS_SEPARATOR
).
The specified transformer classes must all be instances of
ClassTransformer
and also have a no-args constructor.
args
- the arguments to pass to the program being runThrowable
- if an error occurs