Class ClassTransformer

java.lang.Object
io.quarkus.gizmo.ClassTransformer

public class ClassTransformer extends Object
Transforms an existing class by wrapping an ASM ClassVisitor. The applyTo(ClassVisitor) method must be called before the wrapped ClassVisitor is 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, a ClassVisitor must be created using applyTo(). Afterwards, the ClassTransformer instance 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 the add* methods.) No other ordering guarantees are made.

  • Field Details

  • Constructor Details

    • ClassTransformer

      public ClassTransformer(String className)
      Parameters:
      className - the name of the transformed class
  • Method Details

    • getClassName

      String getClassName()
    • hasAddedMethod

      boolean hasAddedMethod(MethodDescriptor methodDescriptor)
    • addModifiers

      public void addModifiers(int modifiers)
      Adds given modifiers to 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 given modifiers from 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(Object interfaceName)
      Adds given interfaceName to 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 with addMethod().

      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:
      IllegalArgumentException - if the interfaceName is not a Class or String
    • addMethod

      public MethodCreator addMethod(MethodDescriptor methodDescriptor)
      Returns a MethodCreator to 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(String name, Object returnType, Object... parameters)
      Returns a MethodCreator to 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 method
      returnType - return type of the new method
      parameters - parameter types of the new method
      Returns:
      a new MethodCreator
      Throws:
      IllegalArgumentException - if the returnType or any of the parameters is not a Class or String
    • removeMethod

      public void removeMethod(String name, Object returnType, 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:
      IllegalArgumentException - if the returnType or any of the parameters is not a Class or String
    • 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 a FieldCreator to 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(String name, Object type)
      Returns a FieldCreator to configure a new field that will be added to this class.
      Parameters:
      name - name of the new field
      type - type of the new field
      Returns:
      a new FieldCreator
      Throws:
      IllegalArgumentException - if the type is not a Class or String
    • removeField

      public void removeField(String name, Object type)
      Removes the field of the given name and type.

      It's illegal to remove a modified field.

      Parameters:
      name -
      type -
      Throws:
      IllegalArgumentException - if the type is not a Class or String
    • 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 a MethodTransformer to configure a transformation of given method.

      It's illegal to modify a removed method.

      Parameters:
      method - descriptor of the method to transform
      Returns:
      a MethodTransformer
    • modifyMethod

      public MethodTransformer modifyMethod(String name, Object returnType, Object... parameters)
      Returns a MethodTransformer to configure a transformation of a method with given name, returnType and parameters.

      It's illegal to modify a removed method.

      Parameters:
      name - name of the method
      returnType - return type of the method
      parameters - parameter types of the method
      Returns:
      a MethodTransformer
      Throws:
      IllegalArgumentException - if the returnType or any of the parameters is not a Class or String
    • modifyField

      public FieldTransformer modifyField(FieldDescriptor field)
      Returns a FieldTransformer to configure a transformation of given field.

      It's illegal to modify a removed field.

      Parameters:
      field - descriptor of the field to transform
      Returns:
      a FieldTransformer
    • modifyField

      public FieldTransformer modifyField(String name, Object type)
      Returns a FieldTransformer to configure a transformation of a field with given name and type.

      It's illegal to modify a removed field.

      Parameters:
      name - name of the field
      type - type of the field
      Returns:
      a FieldTransformer
      Throws:
      IllegalArgumentException - if the type is not a Class or String
    • applyTo

      public org.objectweb.asm.ClassVisitor applyTo(org.objectweb.asm.ClassVisitor visitor)
      Returns a ClassVisitor that applies the class transformation to given visitor. At the moment this method is called, no visit* method must have been called on given visitor. The transformation is not finished until visitEnd() is called on the resulting visitor.
      Parameters:
      visitor - the ClassVisitor to which the transformation is applied
      Returns:
      the transforming ClassVisitor