Class ClassTransformer


  • public class ClassTransformer
    extends java.lang.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.

    • Constructor Detail

      • ClassTransformer

        public ClassTransformer​(java.lang.String className)
        Parameters:
        className - the name of the transformed class
    • Method Detail

      • getClassName

        java.lang.String getClassName()
      • 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​(java.lang.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:
        java.lang.IllegalArgumentException - if the interfaceName is not a Class or String
      • addMethod

        public MethodCreator addMethod​(java.lang.String name,
                                       java.lang.Object returnType,
                                       java.lang.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:
        java.lang.IllegalArgumentException - if the returnType or any of the parameters is not a Class or String
      • 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 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​(java.lang.String name,
                                     java.lang.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:
        java.lang.IllegalArgumentException - if the type is not a Class or String
      • 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 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​(java.lang.String name,
                                              java.lang.Object returnType,
                                              java.lang.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:
        java.lang.IllegalArgumentException - if the returnType or any of the parameters is not a Class or String
      • modifyField

        public FieldTransformer modifyField​(java.lang.String name,
                                            java.lang.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:
        java.lang.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