Package EDU.purdue.cs.bloat.editor
Class MethodEditor
- java.lang.Object
-
- EDU.purdue.cs.bloat.editor.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 Summary
Fields Modifier and Type Field Description static booleanOPT_STACK_2static booleanPRESERVE_DEBUGUseMapuMapstatic booleanUNIQUE_HANDLERS-
Fields inherited from interface EDU.purdue.cs.bloat.editor.Opcode
opc_aaload, opc_aastore, opc_aconst_null, opc_aload, opc_aload_0, opc_aload_1, opc_aload_2, opc_aload_3, opc_anewarray, opc_areturn, opc_arraylength, opc_astore, opc_astore_0, opc_astore_1, opc_astore_2, opc_astore_3, opc_aswizzle, opc_aswrange, opc_athrow, opc_aupdate, opc_baload, opc_bastore, opc_bipush, opc_breakpoint, opc_caload, opc_castore, opc_checkcast, opc_d2f, opc_d2i, opc_d2l, opc_dadd, opc_daload, opc_dastore, opc_dcmpg, opc_dcmpl, opc_dconst_0, opc_dconst_1, opc_ddiv, opc_dload, opc_dload_0, opc_dload_1, opc_dload_2, opc_dload_3, opc_dmul, opc_dneg, opc_drem, opc_dreturn, opc_dstore, opc_dstore_0, opc_dstore_1, opc_dstore_2, opc_dstore_3, opc_dsub, opc_dup, opc_dup_x1, opc_dup_x2, opc_dup2, opc_dup2_x1, opc_dup2_x2, opc_f2d, opc_f2i, opc_f2l, opc_fadd, opc_faload, opc_fastore, opc_fcmpg, opc_fcmpl, opc_fconst_0, opc_fconst_1, opc_fconst_2, opc_fdiv, opc_fload, opc_fload_0, opc_fload_1, opc_fload_2, opc_fload_3, opc_fmul, opc_fneg, opc_frem, opc_freturn, opc_fstore, opc_fstore_0, opc_fstore_1, opc_fstore_2, opc_fstore_3, opc_fsub, opc_getfield, opc_getstatic, opc_goto, opc_goto_w, opc_i2b, opc_i2c, opc_i2d, opc_i2f, opc_i2l, opc_i2s, opc_iadd, opc_iaload, opc_iand, opc_iastore, opc_iconst_0, opc_iconst_1, opc_iconst_2, opc_iconst_3, opc_iconst_4, opc_iconst_5, opc_iconst_m1, opc_idiv, opc_if_acmpeq, opc_if_acmpne, opc_if_icmpeq, opc_if_icmpge, opc_if_icmpgt, opc_if_icmple, opc_if_icmplt, opc_if_icmpne, opc_ifeq, opc_ifge, opc_ifgt, opc_ifle, opc_iflt, opc_ifne, opc_ifnonnull, opc_ifnull, opc_iinc, opc_iload, opc_iload_0, opc_iload_1, opc_iload_2, opc_iload_3, opc_imul, opc_ineg, opc_instanceof, opc_invokeinterface, opc_invokespecial, opc_invokestatic, opc_invokevirtual, opc_ior, opc_irem, opc_ireturn, opc_ishl, opc_ishr, opc_istore, opc_istore_0, opc_istore_1, opc_istore_2, opc_istore_3, opc_isub, opc_iushr, opc_ixor, opc_jsr, opc_jsr_w, opc_l2d, opc_l2f, opc_l2i, opc_ladd, opc_laload, opc_land, opc_lastore, opc_lcmp, opc_lconst_0, opc_lconst_1, opc_ldc, opc_ldc_w, opc_ldc2_w, opc_ldiv, opc_lload, opc_lload_0, opc_lload_1, opc_lload_2, opc_lload_3, opc_lmul, opc_lneg, opc_lookupswitch, opc_lor, opc_lrem, opc_lreturn, opc_lshl, opc_lshr, opc_lstore, opc_lstore_0, opc_lstore_1, opc_lstore_2, opc_lstore_3, opc_lsub, opc_lushr, opc_lxor, opc_monitorenter, opc_monitorexit, opc_multianewarray, opc_new, opc_newarray, opc_nop, opc_pop, opc_pop2, opc_putfield, opc_putfield_nowb, opc_putstatic, opc_putstatic_nowb, opc_rc, opc_ret, opc_return, opc_saload, opc_sastore, opc_sipush, opc_supdate, opc_swap, opc_tableswitch, opc_wide, opc_xxxunusedxxx, opcNames, opcSize, opcx_aaload, opcx_aastore, opcx_aload, opcx_areturn, opcx_arraylength, opcx_astore, opcx_aswizzle, opcx_aswrange, opcx_athrow, opcx_aupdate, opcx_baload, opcx_bastore, opcx_caload, opcx_castore, opcx_checkcast, opcx_d2f, opcx_d2i, opcx_d2l, opcx_dadd, opcx_daload, opcx_dastore, opcx_dcmpg, opcx_dcmpl, opcx_ddiv, opcx_dload, opcx_dmul, opcx_dneg, opcx_drem, opcx_dreturn, opcx_dstore, opcx_dsub, opcx_dup, opcx_dup_x1, opcx_dup_x2, opcx_dup2, opcx_dup2_x1, opcx_dup2_x2, opcx_f2d, opcx_f2i, opcx_f2l, opcx_fadd, opcx_faload, opcx_fastore, opcx_fcmpg, opcx_fcmpl, opcx_fdiv, opcx_fload, opcx_fmul, opcx_fneg, opcx_frem, opcx_freturn, opcx_fstore, opcx_fsub, opcx_getfield, opcx_getstatic, opcx_goto, opcx_i2b, opcx_i2c, opcx_i2d, opcx_i2f, opcx_i2l, opcx_i2s, opcx_iadd, opcx_iaload, opcx_iand, opcx_iastore, opcx_idiv, opcx_if_acmpeq, opcx_if_acmpne, opcx_if_icmpeq, opcx_if_icmpge, opcx_if_icmpgt, opcx_if_icmple, opcx_if_icmplt, opcx_if_icmpne, opcx_ifeq, opcx_ifge, opcx_ifgt, opcx_ifle, opcx_iflt, opcx_ifne, opcx_ifnonnull, opcx_ifnull, opcx_iinc, opcx_iload, opcx_imul, opcx_ineg, opcx_instanceof, opcx_invokeinterface, opcx_invokespecial, opcx_invokestatic, opcx_invokevirtual, opcx_ior, opcx_irem, opcx_ireturn, opcx_ishl, opcx_ishr, opcx_istore, opcx_isub, opcx_iushr, opcx_ixor, opcx_jsr, opcx_l2d, opcx_l2f, opcx_l2i, opcx_ladd, opcx_laload, opcx_land, opcx_lastore, opcx_lcmp, opcx_ldc, opcx_ldiv, opcx_lload, opcx_lmul, opcx_lneg, opcx_lor, opcx_lrem, opcx_lreturn, opcx_lshl, opcx_lshr, opcx_lstore, opcx_lsub, opcx_lushr, opcx_lxor, opcx_monitorenter, opcx_monitorexit, opcx_multianewarray, opcx_new, opcx_newarray, opcx_nop, opcx_pop, opcx_pop2, opcx_putfield, opcx_putfield_nowb, opcx_putstatic, opcx_putstatic_nowb, opcx_rc, opcx_ret, opcx_return, opcx_saload, opcx_sastore, opcx_supdate, opcx_swap, opcx_switch, opcXMap, VARIABLE
-
-
Constructor Summary
Constructors Constructor Description MethodEditor(ClassEditor editor, int modifiers, Type returnType, java.lang.String methodName, Type[] paramTypes, Type[] exceptionTypes)Creates a newMethodEditorfor editing a method in a given class with the given modifiers, return type, name, parameter types, and exception types.MethodEditor(ClassEditor editor, int modifiers, java.lang.Class returnType, java.lang.String methodName, java.lang.Class[] paramTypes, java.lang.Class[] exceptionTypes)MethodEditor(ClassEditor editor, MethodInfo methodInfo)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddInstruction(int opcodeClass)Add an instruction.voidaddInstruction(int opcodeClass, java.lang.Object operand)Add an instruction.voidaddInstruction(Instruction inst)Add an instruction to the end of the code array.voidaddLabel(Label label)Add a label to the code array to the end of the code array.voidaddLineNumberEntry(Label label, int lineNumber)Add a line number entry.voidaddTryCatch(TryCatch tryCatch)Add an exception handler.voidclearCode()Remove all the instructions in preparation for the instructions being added back after a control flow graph edit.voidclearCode2()Like clear code, but doesn't reset the maxLocals.java.util.Listcode()Returns the code (Instructions and Labels) in the method.java.lang.ObjectcodeElementAt(int i)Returns a Label or Instruction in the code array.intcodeLength()Get the length of the code array.voidcommit()Commits changes made to this MethodEditor back to the MethodInfo on which it is based.ClassEditordeclaringClass()Returns the class which declared the method.voiddelete()Marks this method for deletion.booleanequals(java.lang.Object o)Two MethodEditors are equal if they edit the same method in the same class.Type[]exceptions()Returns the Types of exceptions that this method may throw.LabelfirstBlock()Get the label of the first block.inthashCode()A MethodEditor's hash code is based on the hash codes for its class, name, and type.voidinsertCodeAt(java.lang.Object obj, int i)Inserts a Label or Instruction into the code array.booleanisAbstract()booleanisConstructor()Returns true if the method being edited is a constructor.booleanisDirty()Returns true if this method has been modified.booleanisFinal()booleanisInterface()Returns true if this method's class is an interface.booleanisNative()booleanisPackage()Returns true is the method has package level visibilitybooleanisPrivate()booleanisProtected()booleanisPublic()booleanisStatic()booleanisSynchronized()LocalVariablelocalAt(int index)Returns the LocalVariable with the given index.intmaxLocals()Returns the maximum number of locals used by the method.MemberRefmemberRef()Returns a MemberRef for the method.MethodInfomethodInfo()Returns the raw MethodInfo of the method being edited.java.lang.Stringname()Returns the name of the method.NameAndTypenameAndType()Returns the NameAndType of the method.LabelnewLabel()Get the next available label.LabelnewLabelTrue()LocalVariablenewLocal(boolean isWide)Creates a new local variable of an undertermined type.LocalVariablenewLocal(Type type)Creates a new local variable.LabelnextBlock(Label label)Get the label of the next block after the parameter.intnumTryCatches()Returns the number of exception handlers in the method.LocalVariableparamAt(int index)Get the LocalVariable for the parameter at the given index.Type[]paramTypes()Returns an array of Types representing the types of the parameters of this method.voidprint(java.io.PrintStream out)Print the method.voidrememberDef(LocalExpr e)voidremoveCodeAt(int i)Removes a Label or Instruction from the code array.voidreplaceCodeAt(java.lang.Object obj, int i)Replace a Label or Instruction in the code array.voidsetAbstract(boolean flag)voidsetCode(java.util.List v)voidsetDirty(boolean dirty)Sets the dirty flag of this method.voidsetFinal(boolean flag)voidsetNative(boolean flag)voidsetPrivate(boolean flag)voidsetProtected(boolean flag)voidsetPublic(boolean flag)voidsetStatic(boolean flag)voidsetSynchronized(boolean flag)java.lang.StringtoString()java.util.CollectiontryCatches()Returns the exception handlers (TryCatch) in the method.Typetype()Returns the type of the method.UseMapuMap()
-
-
-
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
-
uMap
public UseMap uMap
-
-
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 newMethodEditorfor editing a method in a given class with the given modifiers, return type, name, parameter types, and exception types.- Parameters:
modifiers- Themodifiersfor the new methodreturnType- The return type of the method. If,returnTypeis null, the return type is assumed to bevoid.methodName- The name of the methodparamTypes- The types of the parameters to the new method. IfparamTypesisnull, then we assume that there are no arguments.exceptionTypes- The types of exceptions that may be thrown by the new method. IfexceptionTypesisnull, 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 anIllegalStateException.
-
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:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
A MethodEditor's hash code is based on the hash codes for its class, name, and type.- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
uMap
public UseMap uMap()
-
rememberDef
public void rememberDef(LocalExpr e)
-
-