Class ClassTransformer
- java.lang.Object
-
- io.quarkus.gizmo.ClassTransformer
-
public class ClassTransformer extends java.lang.ObjectTransforms an existing class by wrapping an ASMClassVisitor. TheapplyTo(ClassVisitor)method must be called before the wrappedClassVisitoris visited and the result should be used instead of the original visitor.In other words, this class acts like a builder. First, the class transformation must be described by calling
addInterface(),addMethod(),addField(),modifyMethod(),modifyField(), etc. Then, aClassVisitormust be created usingapplyTo(). Afterwards, theClassTransformerinstance should be discarded.If a modification of a class member and an addition of a class member of the same kind, with the same name and the same descriptor are configured, the modification is performed prior to the addition. (It follows that the
modify*methods may not be used to alter members created using theadd*methods.) No other ordering guarantees are made.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classClassTransformer.NamedDescriptor
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<FieldDescriptor,FieldCreatorImpl>addedFieldsprivate java.util.Set<java.lang.String>addedInterfacesprivate java.util.Map<MethodDescriptor,ClassTransfromerMethodCreatorImpl>addedMethodsprivate intaddedModifiersprivate java.lang.StringclassNameprivate java.util.Map<ClassTransformer.NamedDescriptor,FieldTransformer>modifiedFieldsprivate java.util.Map<ClassTransformer.NamedDescriptor,MethodTransformer>modifiedMethodsprivate java.util.Set<ClassTransformer.NamedDescriptor>removedFieldsprivate java.util.Set<ClassTransformer.NamedDescriptor>removedMethodsprivate intremovedModifiers
-
Constructor Summary
Constructors Constructor Description ClassTransformer(java.lang.String className)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FieldCreatoraddField(FieldDescriptor fieldDescriptor)Returns aFieldCreatorto configure a new field that will be added to this class.FieldCreatoraddField(java.lang.String name, java.lang.Object type)Returns aFieldCreatorto configure a new field that will be added to this class.voidaddInterface(java.lang.Object interfaceName)Adds giveninterfaceNameto the set of interfaces implemented by this class.MethodCreatoraddMethod(MethodDescriptor methodDescriptor)Returns aMethodCreatorto configure a new method that will be added to this class.MethodCreatoraddMethod(java.lang.String name, java.lang.Object returnType, java.lang.Object... parameters)Returns aMethodCreatorto configure a new method that will be added to this class.voidaddModifiers(int modifiers)Adds givenmodifiersto the modifiers of this class.org.objectweb.asm.ClassVisitorapplyTo(org.objectweb.asm.ClassVisitor visitor)Returns aClassVisitorthat applies the class transformation to givenvisitor.(package private) java.lang.StringgetClassName()(package private) booleanhasAddedMethod(MethodDescriptor methodDescriptor)FieldTransformermodifyField(FieldDescriptor field)Returns aFieldTransformerto configure a transformation of givenfield.FieldTransformermodifyField(java.lang.String name, java.lang.Object type)MethodTransformermodifyMethod(MethodDescriptor method)Returns aMethodTransformerto configure a transformation of givenmethod.MethodTransformermodifyMethod(java.lang.String name, java.lang.Object returnType, java.lang.Object... parameters)Returns aMethodTransformerto configure a transformation of a method with givenname,returnTypeandparameters.voidremoveField(FieldDescriptor field)Removes the given field.voidremoveField(java.lang.String name, java.lang.Object type)Removes the field of the given name and type.voidremoveMethod(MethodDescriptor method)Removes the given method.voidremoveMethod(java.lang.String name, java.lang.Object returnType, java.lang.Object... parameters)Removes the method with the given name, return type and parameters.voidremoveModifiers(int modifiers)Removes givenmodifiersfrom the modifiers of this class.
-
-
-
Field Detail
-
className
private final java.lang.String className
-
addedModifiers
private int addedModifiers
-
removedModifiers
private int removedModifiers
-
addedInterfaces
private final java.util.Set<java.lang.String> addedInterfaces
-
addedMethods
private final java.util.Map<MethodDescriptor,ClassTransfromerMethodCreatorImpl> addedMethods
-
addedFields
private final java.util.Map<FieldDescriptor,FieldCreatorImpl> addedFields
-
modifiedMethods
private final java.util.Map<ClassTransformer.NamedDescriptor,MethodTransformer> modifiedMethods
-
modifiedFields
private final java.util.Map<ClassTransformer.NamedDescriptor,FieldTransformer> modifiedFields
-
removedMethods
private final java.util.Set<ClassTransformer.NamedDescriptor> removedMethods
-
removedFields
private final java.util.Set<ClassTransformer.NamedDescriptor> removedFields
-
-
Method Detail
-
getClassName
java.lang.String getClassName()
-
hasAddedMethod
boolean hasAddedMethod(MethodDescriptor methodDescriptor)
-
addModifiers
public void addModifiers(int modifiers)
Adds givenmodifiersto the modifiers of this class. It is a responsibility of the caller to make sure the resulting set of modifiers is valid.- Parameters:
modifiers- the modifiers to add to this class
-
removeModifiers
public void removeModifiers(int modifiers)
Removes givenmodifiersfrom the modifiers of this class. It is a responsibility of the caller to make sure the resulting set of modifiers is valid.- Parameters:
modifiers- the modifiers to remove from this class
-
addInterface
public void addInterface(java.lang.Object interfaceName)
Adds giveninterfaceNameto the set of interfaces implemented by this class. It is a responsibility of the caller to make sure the class in fact implements the interface, typically by adding all the methods withaddMethod().Adding an interface that has already been added is a noop, as well as adding an interface that the original class already implements.
- Parameters:
interfaceName- the interface type to add- Throws:
java.lang.IllegalArgumentException- if theinterfaceNameis not aClassorString
-
addMethod
public MethodCreator addMethod(MethodDescriptor methodDescriptor)
Returns aMethodCreatorto configure a new method that will be added to this class.The
BytecodeCreator.createFunction(Class)method may not be used, as creating new class would be required.- Parameters:
methodDescriptor- descriptor of the new method- Returns:
- a new
MethodCreator
-
addMethod
public MethodCreator addMethod(java.lang.String name, java.lang.Object returnType, java.lang.Object... parameters)
Returns aMethodCreatorto configure a new method that will be added to this class.The
BytecodeCreator.createFunction(Class)method may not be used, as creating new class would be required.- Parameters:
name- name of the new methodreturnType- return type of the new methodparameters- parameter types of the new method- Returns:
- a new
MethodCreator - Throws:
java.lang.IllegalArgumentException- if thereturnTypeor any of theparametersis not aClassorString
-
removeMethod
public void removeMethod(java.lang.String name, java.lang.Object returnType, java.lang.Object... parameters)Removes the method with the given name, return type and parameters.It's illegal to remove a modified method.
- Parameters:
name-returnType-parameters-- Throws:
java.lang.IllegalArgumentException- if thereturnTypeor any of theparametersis not aClassorString
-
removeMethod
public void removeMethod(MethodDescriptor method)
Removes the given method.It's illegal to remove a modified method.
- Parameters:
method-
-
addField
public FieldCreator addField(FieldDescriptor fieldDescriptor)
Returns aFieldCreatorto configure a new field that will be added to this class.- Parameters:
fieldDescriptor- descriptor of the new field- Returns:
- a new
FieldCreator
-
addField
public FieldCreator addField(java.lang.String name, java.lang.Object type)
Returns aFieldCreatorto configure a new field that will be added to this class.- Parameters:
name- name of the new fieldtype- type of the new field- Returns:
- a new
FieldCreator - Throws:
java.lang.IllegalArgumentException- if thetypeis not aClassorString
-
removeField
public void removeField(java.lang.String name, java.lang.Object type)Removes the field of the given name and type.It's illegal to remove a modified field.
- Parameters:
name-type-- Throws:
java.lang.IllegalArgumentException- if thetypeis not aClassorString
-
removeField
public void removeField(FieldDescriptor field)
Removes the given field.It's illegal to remove a modified field.
- Parameters:
field-
-
modifyMethod
public MethodTransformer modifyMethod(MethodDescriptor method)
Returns aMethodTransformerto configure a transformation of givenmethod.It's illegal to modify a removed method.
- Parameters:
method- descriptor of the method to transform- Returns:
- a
MethodTransformer
-
modifyMethod
public MethodTransformer modifyMethod(java.lang.String name, java.lang.Object returnType, java.lang.Object... parameters)
Returns aMethodTransformerto configure a transformation of a method with givenname,returnTypeandparameters.It's illegal to modify a removed method.
- Parameters:
name- name of the methodreturnType- return type of the methodparameters- parameter types of the method- Returns:
- a
MethodTransformer - Throws:
java.lang.IllegalArgumentException- if thereturnTypeor any of theparametersis not aClassorString
-
modifyField
public FieldTransformer modifyField(FieldDescriptor field)
Returns aFieldTransformerto configure a transformation of givenfield.It's illegal to modify a removed field.
- Parameters:
field- descriptor of the field to transform- Returns:
- a
FieldTransformer
-
modifyField
public FieldTransformer modifyField(java.lang.String name, java.lang.Object type)
Returns aFieldTransformerto configure a transformation of a field with givennameandtype.It's illegal to modify a removed field.
- Parameters:
name- name of the fieldtype- type of the field- Returns:
- a
FieldTransformer - Throws:
java.lang.IllegalArgumentException- if thetypeis not aClassorString
-
applyTo
public org.objectweb.asm.ClassVisitor applyTo(org.objectweb.asm.ClassVisitor visitor)
Returns aClassVisitorthat applies the class transformation to givenvisitor. At the moment this method is called, novisit*method must have been called on givenvisitor. The transformation is not finished untilvisitEnd()is called on the resulting visitor.- Parameters:
visitor- theClassVisitorto which the transformation is applied- Returns:
- the transforming
ClassVisitor
-
-