@ParametersAreNonnullByDefault
public class TransformerManager
extends java.lang.Object
implements java.lang.instrument.ClassFileTransformer
ClassFileTransformer so it can be used with an Instrumentation agent.| Constructor and Description |
|---|
TransformerManager(IClassProvider classProvider) |
TransformerManager(IClassProvider classProvider,
AMapper mapper) |
| Modifier and Type | Method and Description |
|---|---|
void |
addBytecodeTransformer(IBytecodeTransformer bytecodeTransformer)
Add a bytecode transformer to the transformer list.
Bytecode transformer are the ClassTransform implementation of ClassFileTransformer . |
void |
addClassFileTransformer(java.lang.ClassLoader classLoader,
java.lang.instrument.ClassFileTransformer classFileTransformer)
Add a
ClassFileTransformer to the transformer list.The classLoader is directly passed to the transformer. |
void |
addCoprocessor(java.util.function.Supplier<? extends IAnnotationCoprocessor> coprocessorSupplier)
Add a new annotation handler coprocessor.
A new coprocessor instance will be created for each annotation handler. |
void |
addCustomAnnotationHandler(AnnotationHandler transformer,
HandlerPosition handlerPosition)
Add a custom annotation handler into the handler chain.
|
void |
addInjectionTarget(java.lang.String name,
IInjectionTarget target)
Add a new injection target for use with the
CTarget annotation. |
void |
addPostTransformConsumer(IPostTransformer postTransformer)
Add a post transformer to handle the raw bytecode after all transformer have been applied.
Useful for dumping transformed classes to disk. |
void |
addRawTransformer(java.lang.String className,
IRawTransformer rawTransformer)
Add a raw class transformer to the transformer list.
Raw class transformer are similar to bytecode transformer but only for the specified class. A ClassNode is passed to the transformer instead of the bytecode array. |
java.util.Set<java.lang.String> |
addTransformer(org.objectweb.asm.tree.ClassNode classNode)
Add a
ClassNode directly to the transformer list.The class must still be annotated with CTransformer. |
java.util.Set<java.lang.String> |
addTransformer(org.objectweb.asm.tree.ClassNode classNode,
boolean requireAnnotation)
Add a
ClassNode directly to the transformer list.The class must still be annotated with CTransformer. |
java.util.Set<java.lang.String> |
addTransformer(org.objectweb.asm.tree.ClassNode classNode,
boolean requireAnnotation,
boolean retransformClasses)
Add a
ClassNode directly to the transformer list.The class must still be annotated with CTransformer. |
void |
addTransformer(java.lang.String transformer)
Add a transformer class to the transformer list.
Use the direct class name for a single transformer (e.g. |
void |
addTransformerPreprocessor(IAnnotationHandlerPreprocessor annotationHandlerPreprocessor)
Add an annotation handler preprocessor to the preprocessor list.
You can modify class transform annotations before they get parsed. |
IClassProvider |
getClassProvider() |
ClassTree |
getClassTree() |
AnnotationCoprocessorList |
getCoprocessors()
Create and get a new array of all coprocessors.
|
TransformerDebugger |
getDebugger() |
FailStrategy |
getFailStrategy() |
java.util.Optional<IInjectionTarget> |
getInjectionTarget(java.lang.String name)
Get an injection target by its name.
The name is case-insensitive. |
java.util.Map<java.lang.String,IInjectionTarget> |
getInjectionTargets() |
java.lang.instrument.Instrumentation |
getInstrumentation() |
AMapper |
getMapper() |
java.util.Set<java.lang.String> |
getRegisteredTransformer() |
java.util.Set<java.lang.String> |
getTransformedClasses() |
void |
hookInstrumentation(java.lang.instrument.Instrumentation instrumentation)
Hook an
Instrumentation instance to allow for transformation using it.This allows to transform classes already loaded by the JVM. You have to be careful with re-transforming classes since you can't modify the class structure (e.g. |
void |
hookInstrumentation(java.lang.instrument.Instrumentation instrumentation,
boolean hotswappable)
Hook an
Instrumentation instance to allow for transformation using it.This allows to transform classes already loaded by the JVM. You have to be careful with re-transforming classes since you can't modify the class structure (e.g. |
void |
setFailStrategy(FailStrategy failStrategy)
Set the fail strategy used when a transformer fails.
|
byte[] |
transform(java.lang.ClassLoader loader,
java.lang.String className,
java.lang.Class<?> classBeingRedefined,
java.security.ProtectionDomain protectionDomain,
byte[] classfileBuffer)
Support method for hooking an instrumentation instance.
You can simply add the TransformerManager as a transformer using Instrumentation.addTransformer(ClassFileTransformer) or call hookInstrumentation(Instrumentation). |
byte[] |
transform(java.lang.String name,
byte[] bytecode)
Transform the bytecode of the given class.
The name must be in the class format (e.g. |
byte[] |
transform(java.lang.String name,
byte[] bytecode,
boolean calculateStackMapFrames)
Transform the bytecode of the given class.
The name must be in the class format (e.g. |
public TransformerManager(IClassProvider classProvider)
classProvider - The class provider used to get the class bytecodepublic TransformerManager(IClassProvider classProvider, AMapper mapper)
classProvider - The class provider used to get the class bytecodemapper - The mapper used to remap ClassTransform annotation targets and transformers if enabledpublic ClassTree getClassTree()
public IClassProvider getClassProvider()
public AMapper getMapper()
public java.util.Set<java.lang.String> getRegisteredTransformer()
public java.util.Set<java.lang.String> getTransformedClasses()
public void addCoprocessor(java.util.function.Supplier<? extends IAnnotationCoprocessor> coprocessorSupplier)
coprocessorSupplier - The coprocessor supplierpublic AnnotationCoprocessorList getCoprocessors()
public java.util.Map<java.lang.String,IInjectionTarget> getInjectionTargets()
public java.util.Optional<IInjectionTarget> getInjectionTarget(java.lang.String name)
name - The name of the injection targetpublic TransformerDebugger getDebugger()
public void setFailStrategy(FailStrategy failStrategy)
failStrategy - The fail strategy to usepublic FailStrategy getFailStrategy()
@Nullable public java.lang.instrument.Instrumentation getInstrumentation()
public void addTransformerPreprocessor(IAnnotationHandlerPreprocessor annotationHandlerPreprocessor)
annotationHandlerPreprocessor - The annotation handler preprocessor to addpublic void addBytecodeTransformer(IBytecodeTransformer bytecodeTransformer)
ClassFileTransformer .bytecodeTransformer - The bytecode transformer to addpublic void addClassFileTransformer(java.lang.ClassLoader classLoader,
java.lang.instrument.ClassFileTransformer classFileTransformer)
ClassFileTransformer to the transformer list.classLoader is directly passed to the transformer.classLoader - The class loader of the classclassFileTransformer - The class file transformer to addpublic void addRawTransformer(java.lang.String className,
IRawTransformer rawTransformer)
ClassNode is passed to the transformer instead of the bytecode array.className - The name of the class to transformrawTransformer - The raw transformer to addpublic void addTransformer(java.lang.String transformer)
CTransformer annotation.transformer - The name of transformer class to addjava.lang.IllegalStateException - If the class is specified directly and is missing the CTransformer annotationTransformerLoadException - If the transformer could not be loadedjava.lang.RuntimeException - If the class bytecode could not be parsed using ASMpublic java.util.Set<java.lang.String> addTransformer(org.objectweb.asm.tree.ClassNode classNode)
ClassNode directly to the transformer list.CTransformer.classNode - The class node to addjava.lang.IllegalStateException - If the class is missing the CTransformer annotationpublic java.util.Set<java.lang.String> addTransformer(org.objectweb.asm.tree.ClassNode classNode,
boolean requireAnnotation)
ClassNode directly to the transformer list.CTransformer.classNode - The class node to addrequireAnnotation - If an exception should be thrown if the class is missing the CTransformer annotationjava.lang.IllegalStateException - If the class is missing the CTransformer annotation and requireAnnotation is truepublic java.util.Set<java.lang.String> addTransformer(org.objectweb.asm.tree.ClassNode classNode,
boolean requireAnnotation,
boolean retransformClasses)
ClassNode directly to the transformer list.CTransformer.classNode - The class node to addrequireAnnotation - If an exception should be thrown if the class is missing the CTransformer annotationretransformClasses - Whether to retransform classes after adding the transformer (if instrumentation is hooked)java.lang.IllegalStateException - If the class is missing the CTransformer annotation and requireAnnotation is truepublic void addPostTransformConsumer(IPostTransformer postTransformer)
postTransformer - The BiConsumer instancepublic void addCustomAnnotationHandler(AnnotationHandler transformer, HandlerPosition handlerPosition)
transformer - The annotation handler to addhandlerPosition - The position where to add the handlerpublic void addInjectionTarget(java.lang.String name,
IInjectionTarget target)
CTarget annotation.name - The name of the injection targettarget - The injection target@Nullable
public byte[] transform(java.lang.String name,
byte[] bytecode)
java.lang.String).name - The name of the classbytecode - The bytecode of the class@Nullable
public byte[] transform(java.lang.String name,
byte[] bytecode,
boolean calculateStackMapFrames)
java.lang.String).name - The name of the classbytecode - The bytecode of the classcalculateStackMapFrames - If the stack map frames should be calculatedpublic void hookInstrumentation(java.lang.instrument.Instrumentation instrumentation)
Instrumentation instance to allow for transformation using it.CInline annotation to prevent adding methods to a loaded class.instrumentation - The instrumentation instance to hookpublic void hookInstrumentation(java.lang.instrument.Instrumentation instrumentation,
boolean hotswappable)
Instrumentation instance to allow for transformation using it.CInline annotation to prevent adding methods to a loaded class.instrumentation - The instrumentation instance to hookhotswappable - Whether to enable transformer hotswapping@Nullable
public byte[] transform(@Nullable
java.lang.ClassLoader loader,
@Nullable
java.lang.String className,
@Nullable
java.lang.Class<?> classBeingRedefined,
@Nullable
java.security.ProtectionDomain protectionDomain,
byte[] classfileBuffer)
Instrumentation.addTransformer(ClassFileTransformer) or call hookInstrumentation(Instrumentation).transform in interface java.lang.instrument.ClassFileTransformer