Class MethodEditor

  • All Implemented Interfaces:
    Opcode

    public class MethodEditor
    extends java.lang.Object
    implements Opcode
    MethodEditor provides a means to edit a method of a class. A MethodEditor gathers information from a MethodInfo object. It then goes through the bytecodes of the method and extracts information about the method. Along the way it creates an array of Instruction and Label objects that represent the code. Additionally, it models the try-catch blocks in the method and their associated exception handlers.
    See Also:
    MethodInfo, Label, Instruction
    • Field Detail

      • PRESERVE_DEBUG

        public static boolean PRESERVE_DEBUG
      • UNIQUE_HANDLERS

        public static boolean UNIQUE_HANDLERS
      • OPT_STACK_2

        public static boolean OPT_STACK_2
    • Constructor Detail

      • MethodEditor

        public MethodEditor​(ClassEditor editor,
                            int modifiers,
                            java.lang.Class returnType,
                            java.lang.String methodName,
                            java.lang.Class[] paramTypes,
                            java.lang.Class[] exceptionTypes)
      • MethodEditor

        public MethodEditor​(ClassEditor editor,
                            int modifiers,
                            Type returnType,
                            java.lang.String methodName,
                            Type[] paramTypes,
                            Type[] exceptionTypes)
        Creates a new MethodEditor for editing a method in a given class with the given modifiers, return type, name, parameter types, and exception types.
        Parameters:
        modifiers - The modifiers for the new method
        returnType - The return type of the method. If, returnType is null, the return type is assumed to be void.
        methodName - The name of the method
        paramTypes - The types of the parameters to the new method. If paramTypes is null, then we assume that there are no arguments.
        exceptionTypes - The types of exceptions that may be thrown by the new method. If exceptionTypes is null, then we assume that no exceptions are declared.
      • MethodEditor

        public MethodEditor​(ClassEditor editor,
                            MethodInfo methodInfo)
        Constructor.
        Parameters:
        editor - The class containing the method.
        methodInfo - The method to edit.
        See Also:
        ClassEditor, MethodInfo
    • Method Detail

      • exceptions

        public Type[] exceptions()
        Returns the Types of exceptions that this method may throw.
      • isDirty

        public boolean isDirty()
        Returns true if this method has been modified.
      • setDirty

        public void setDirty​(boolean dirty)
        Sets the dirty flag of this method. The dirty flag is true if the method has been modified.
      • delete

        public void delete()
        Marks this method for deletion. Once a method has been marked for deletion all attempts to change it will throw an IllegalStateException.
      • paramTypes

        public Type[] paramTypes()
        Returns an array of Types representing the types of the parameters of this method. It's really used to figure out the type of the local variables that hold the parameters. So, wide data is succeeded by an empty slot. Also, for virtual methods, the first element in the array is the receiver.
      • paramAt

        public LocalVariable paramAt​(int index)
        Get the LocalVariable for the parameter at the given index.
        Parameters:
        index - The index into the params (0 is the this pointer or the first argument, if static).
        Returns:
        The LocalVariable for the parameter at the given index.
      • methodInfo

        public MethodInfo methodInfo()
        Returns the raw MethodInfo of the method being edited.
      • declaringClass

        public ClassEditor declaringClass()
        Returns the class which declared the method.
      • maxLocals

        public int maxLocals()
        Returns the maximum number of locals used by the method.
      • isPublic

        public boolean isPublic()
      • isPrivate

        public boolean isPrivate()
      • isProtected

        public boolean isProtected()
      • isPackage

        public boolean isPackage()
        Returns true is the method has package level visibility
      • isStatic

        public boolean isStatic()
      • isFinal

        public boolean isFinal()
      • isSynchronized

        public boolean isSynchronized()
      • isNative

        public boolean isNative()
      • isAbstract

        public boolean isAbstract()
      • isInterface

        public boolean isInterface()
        Returns true if this method's class is an interface.
      • setPublic

        public void setPublic​(boolean flag)
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • setPrivate

        public void setPrivate​(boolean flag)
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • setProtected

        public void setProtected​(boolean flag)
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • setStatic

        public void setStatic​(boolean flag)
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • setFinal

        public void setFinal​(boolean flag)
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • setSynchronized

        public void setSynchronized​(boolean flag)
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • setNative

        public void setNative​(boolean flag)
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • setAbstract

        public void setAbstract​(boolean flag)
      • clearCode

        public void clearCode()
        Remove all the instructions in preparation for the instructions being added back after a control flow graph edit.
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • clearCode2

        public void clearCode2()
        Like clear code, but doesn't reset the maxLocals. I'm not really sure why this works, but it stops certain parts of code that is generated and then re-cfg'd from being eliminated as dead
      • name

        public java.lang.String name()
        Returns the name of the method.
      • isConstructor

        public boolean isConstructor()
        Returns true if the method being edited is a constructor.
      • type

        public Type type()
        Returns the type of the method.
      • nameAndType

        public NameAndType nameAndType()
        Returns the NameAndType of the method.
      • memberRef

        public MemberRef memberRef()
        Returns a MemberRef for the method.
      • codeLength

        public int codeLength()
        Get the length of the code array.
        Returns:
        The length of the code array.
      • setCode

        public void setCode​(java.util.List v)
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • code

        public java.util.List code()
        Returns the code (Instructions and Labels) in the method.
      • firstBlock

        public Label firstBlock()
        Get the label of the first block.
      • nextBlock

        public Label nextBlock​(Label label)
        Get the label of the next block after the parameter.
        Parameters:
        label - The label at which to begin.
        Returns:
        The label.
      • removeCodeAt

        public void removeCodeAt​(int i)
        Removes a Label or Instruction from the code array.
        Parameters:
        i - The index of the element to remove.
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • insertCodeAt

        public void insertCodeAt​(java.lang.Object obj,
                                 int i)
        Inserts a Label or Instruction into the code array.
        Parameters:
        i - The index of the element to insert before.
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • replaceCodeAt

        public void replaceCodeAt​(java.lang.Object obj,
                                  int i)
        Replace a Label or Instruction in the code array.
        Parameters:
        obj - The new element.
        i - The index of the element to replace
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • codeElementAt

        public java.lang.Object codeElementAt​(int i)
        Returns a Label or Instruction in the code array.
        Parameters:
        i - The index into the code array.
        Returns:
        The element at the index.
      • addLineNumberEntry

        public void addLineNumberEntry​(Label label,
                                       int lineNumber)
        Add a line number entry.
        Parameters:
        label - The label beginning the range of instructions for this line number.
        lineNumber - The line number.
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • numTryCatches

        public int numTryCatches()
        Returns the number of exception handlers in the method.
      • tryCatches

        public java.util.Collection tryCatches()
        Returns the exception handlers (TryCatch) in the method.
      • addTryCatch

        public void addTryCatch​(TryCatch tryCatch)
        Add an exception handler.
        Parameters:
        tryCatch - An exception handler.
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • newLocal

        public LocalVariable newLocal​(Type type)
        Creates a new local variable.
      • newLocal

        public LocalVariable newLocal​(boolean isWide)
        Creates a new local variable of an undertermined type.
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • localAt

        public LocalVariable localAt​(int index)
        Returns the LocalVariable with the given index. If there is no local variable at that index, a new one is created at that index. We assume that this variable is not wide.
      • addInstruction

        public void addInstruction​(int opcodeClass)
        Add an instruction.
        Parameters:
        opcodeClass - The instruction to add.
      • addInstruction

        public void addInstruction​(int opcodeClass,
                                   java.lang.Object operand)
        Add an instruction.
        Parameters:
        opcodeClass - The instruction to add.
      • addInstruction

        public void addInstruction​(Instruction inst)
        Add an instruction to the end of the code array.
        Parameters:
        inst - The instruction to add.
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • newLabel

        public Label newLabel()
        Get the next available label. That is the Label after the final Instruction in the code array.
        Returns:
        A new label.
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • newLabelTrue

        public Label newLabelTrue()
      • addLabel

        public void addLabel​(Label label)
        Add a label to the code array to the end of the code array.
        Parameters:
        label - The label to add.
        Throws:
        java.lang.IllegalStateException - This field has been marked for deletion
      • commit

        public void commit()
        Commits changes made to this MethodEditor back to the MethodInfo on which it is based. Note that committal will take place regardless of whether or not the method is dirty.
      • print

        public void print​(java.io.PrintStream out)
        Print the method.
        Parameters:
        out - Stream to which to print.
      • equals

        public boolean equals​(java.lang.Object o)
        Two MethodEditors are equal if they edit the same method in the same class.
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        A MethodEditor's hash code is based on the hash codes for its class, name, and type.
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • rememberDef

        public void rememberDef​(LocalExpr e)