Package com.tonic.analysis
Class Bytecode
- java.lang.Object
-
- com.tonic.analysis.Bytecode
-
public class Bytecode extends Object
High-level bytecode manipulation API.
-
-
Constructor Summary
Constructors Constructor Description Bytecode(CodeWriter codeWriter)Constructs a Bytecode utility for the given CodeWriter.Bytecode(MethodEntry methodEntry)Constructs a Bytecode utility for the given MethodEntry.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAConstNull()Appends an ACONST_NULL instruction to push a null reference onto the stack.voidaddALoad(int index)Appends an ALOAD instruction to the end of the bytecode.voidaddAStore(int index)Appends an ASTORE instruction to the end of the bytecode.voidaddDConst(double value)Appends a DCONST instruction to push a double constant onto the stack.voidaddDLoad(int index)Appends a DLOAD instruction to load a double from a local variable.voidaddFConst(float value)Appends a FCONST instruction to push a float constant onto the stack.voidaddFLoad(int index)Appends a FLOAD instruction to load a float from a local variable.voidaddGetField(int fieldRefIndex)Appends a GETFIELD instruction to the end of the bytecode.voidaddGetStatic(int fieldRefIndex)Appends a GETSTATIC instruction to the end of the bytecode.voidaddGetStatic(String className, String fieldName, String fieldDescriptor)Inserts a GETSTATIC instruction into the bytecode.voidaddGoto(String labelName)Appends a GOTO instruction to the end of the bytecode.voidaddGotoW(String labelName)Appends a GOTO_W instruction to the end of the bytecode.voidaddIConst(int value)Appends an ICONST instruction to push an integer constant onto the stack.voidaddIInc(int varIndex, int increment)Appends an IINC instruction to the end of the bytecode.voidaddILoad(int index)Appends an ILOAD instruction to the end of the bytecode.voidaddInvokeDynamic(int cpIndex)Appends an INVOKEDYNAMIC instruction to the end of the bytecode.voidaddInvokeInterface(int interfaceMethodRefIndex, int count)Appends an INVOKEINTERFACE instruction to the end of the bytecode.voidaddInvokeSpecial(int methodRefIndex)Appends an INVOKESPECIAL instruction to the end of the bytecode.voidaddInvokeStatic(int methodRefIndex)Appends an INVOKESTATIC instruction to the end of the bytecode.voidaddInvokeStatic(String className, String methodName, String methodDescriptor)Inserts an INVOKESTATIC instruction into the bytecode.voidaddInvokeVirtual(int methodRefIndex)Appends an INVOKEVIRTUAL instruction to the end of the bytecode.voidaddInvokeVirtual(String className, String methodName, String methodDescriptor)Inserts an INVOKEVIRTUAL instruction into the bytecode.voidaddIStore(int index)Appends an ISTORE instruction to the end of the bytecode.voidaddLConst(long value)Appends a LCONST instruction to push a long constant onto the stack.voidaddLdc(String value)Inserts an LDC or LDC_W instruction to load a String constant onto the operand stack.voidaddLLoad(int index)Appends a LLOAD instruction to load a long from a local variable.voidaddLoad(int i, String desc)Appends a load instruction based on the type descriptor.voidaddLookupSwitch(int defaultOffset, Map<Integer,Integer> matchOffsets)Appends a LOOKUPSWITCH instruction to the bytecode.voidaddNew(int classRefIndex)Appends a NEW instruction to the end of the bytecode.voidaddPutField(int fieldRefIndex)Appends a PUTFIELD instruction to the end of the bytecode.voidaddPutStatic(int fieldRefIndex)Appends a PUTSTATIC instruction to the end of the bytecode.voidaddReturn(int returnOpcode)Appends a RETURN instruction to the end of the bytecode.voidaddReturn(ReturnType returnType)Appends a RETURN instruction to the end of the bytecode.voidaddTableSwitch(int low, int high, int defaultOffset, Map<Integer,Integer> jumpOffsets)Appends a TABLESWITCH instruction to the bytecode.voidcomputeFrames()Computes and updates this method's StackMapTable frames.voiddefineLabel(String labelName)Defines a label at the current end of the bytecode.booleanendsWithReturn()Checks if the bytecode ends with a return instruction.voidfinalizeBytecode()Finalizes the bytecode modifications by writing them back to the MethodEntry.voidforceComputeFrames()Forces recomputation of StackMapTable frames, even if bytecode wasn't modified.CodeWritergetCodeWriter()ConstPoolgetConstPool()intgetInsertBeforeOffset()Map<String,Integer>getLabels()booleanisInsertBefore()booleanisModified()Returns whether the bytecode has been modified since loading.booleanisStatic()voidsetInsertBefore(boolean insertBefore)Sets whether instructions should be inserted before the current position.voidsetInsertBeforeOffset(int insertBeforeOffset)Sets the bytecode offset at which insert-before emission places new instructions.
-
-
-
Constructor Detail
-
Bytecode
public Bytecode(MethodEntry methodEntry)
Constructs a Bytecode utility for the given MethodEntry.- Parameters:
methodEntry- The MethodEntry whose bytecode is to be manipulated.
-
Bytecode
public Bytecode(CodeWriter codeWriter)
Constructs a Bytecode utility for the given CodeWriter.- Parameters:
codeWriter- The CodeWriter instance to use for bytecode manipulation.
-
-
Method Detail
-
getCodeWriter
public CodeWriter getCodeWriter()
- Returns:
- the code writer
-
getConstPool
public ConstPool getConstPool()
- Returns:
- the const pool
-
isInsertBefore
public boolean isInsertBefore()
- Returns:
- whether insert before
-
getInsertBeforeOffset
public int getInsertBeforeOffset()
- Returns:
- the insert before offset
-
setInsertBeforeOffset
public void setInsertBeforeOffset(int insertBeforeOffset)
Sets the bytecode offset at which insert-before emission places new instructions.- Parameters:
insertBeforeOffset- the target bytecode offset
-
isStatic
public boolean isStatic()
- Returns:
- whether static
-
setInsertBefore
public void setInsertBefore(boolean insertBefore)
Sets whether instructions should be inserted before the current position.- Parameters:
insertBefore- True to insert before the current position, false to append.
-
defineLabel
public void defineLabel(String labelName)
Defines a label at the current end of the bytecode.- Parameters:
labelName- The name of the label.
-
addInvokeVirtual
public void addInvokeVirtual(int methodRefIndex)
Appends an INVOKEVIRTUAL instruction to the end of the bytecode.- Parameters:
methodRefIndex- The index into the constant pool for the method reference.
-
addInvokeSpecial
public void addInvokeSpecial(int methodRefIndex)
Appends an INVOKESPECIAL instruction to the end of the bytecode.- Parameters:
methodRefIndex- The index into the constant pool for the method reference.
-
addInvokeStatic
public void addInvokeStatic(int methodRefIndex)
Appends an INVOKESTATIC instruction to the end of the bytecode.- Parameters:
methodRefIndex- The index into the constant pool for the method reference.
-
addInvokeInterface
public void addInvokeInterface(int interfaceMethodRefIndex, int count)Appends an INVOKEINTERFACE instruction to the end of the bytecode.- Parameters:
interfaceMethodRefIndex- The index into the constant pool for the interface method reference.count- The count of arguments for the interface method.
-
addInvokeDynamic
public void addInvokeDynamic(int cpIndex)
Appends an INVOKEDYNAMIC instruction to the end of the bytecode.- Parameters:
cpIndex- The constant pool index to the CONSTANT_InvokeDynamic_info entry.
-
addPutStatic
public void addPutStatic(int fieldRefIndex)
Appends a PUTSTATIC instruction to the end of the bytecode.- Parameters:
fieldRefIndex- The index into the constant pool for the field reference.
-
addGetStatic
public void addGetStatic(int fieldRefIndex)
Appends a GETSTATIC instruction to the end of the bytecode.- Parameters:
fieldRefIndex- The index into the constant pool for the field reference.
-
addPutField
public void addPutField(int fieldRefIndex)
Appends a PUTFIELD instruction to the end of the bytecode.- Parameters:
fieldRefIndex- The index into the constant pool for the field reference.
-
addGetField
public void addGetField(int fieldRefIndex)
Appends a GETFIELD instruction to the end of the bytecode.- Parameters:
fieldRefIndex- The index into the constant pool for the field reference.
-
addNew
public void addNew(int classRefIndex)
Appends a NEW instruction to the end of the bytecode.- Parameters:
classRefIndex- The index into the constant pool for the class reference.
-
addALoad
public void addALoad(int index)
Appends an ALOAD instruction to the end of the bytecode.- Parameters:
index- The local variable index to load from.
-
addAStore
public void addAStore(int index)
Appends an ASTORE instruction to the end of the bytecode.- Parameters:
index- The local variable index to store into.
-
addILoad
public void addILoad(int index)
Appends an ILOAD instruction to the end of the bytecode.- Parameters:
index- The local variable index to load from.
-
addIStore
public void addIStore(int index)
Appends an ISTORE instruction to the end of the bytecode.- Parameters:
index- The local variable index to store into.
-
addIConst
public void addIConst(int value)
Appends an ICONST instruction to push an integer constant onto the stack.- Parameters:
value- The integer value to push.
-
addLConst
public void addLConst(long value)
Appends a LCONST instruction to push a long constant onto the stack.- Parameters:
value- The long value to push.
-
addFConst
public void addFConst(float value)
Appends a FCONST instruction to push a float constant onto the stack.- Parameters:
value- The float value to push.
-
addDConst
public void addDConst(double value)
Appends a DCONST instruction to push a double constant onto the stack.- Parameters:
value- The double value to push.
-
addAConstNull
public void addAConstNull()
Appends an ACONST_NULL instruction to push a null reference onto the stack.
-
addLLoad
public void addLLoad(int index)
Appends a LLOAD instruction to load a long from a local variable.- Parameters:
index- The local variable index to load from.
-
addFLoad
public void addFLoad(int index)
Appends a FLOAD instruction to load a float from a local variable.- Parameters:
index- The local variable index to load from.
-
addDLoad
public void addDLoad(int index)
Appends a DLOAD instruction to load a double from a local variable.- Parameters:
index- The local variable index to load from.
-
addGoto
public void addGoto(String labelName)
Appends a GOTO instruction to the end of the bytecode.- Parameters:
labelName- The name of the label to jump to.
-
addGotoW
public void addGotoW(String labelName)
Appends a GOTO_W instruction to the end of the bytecode.- Parameters:
labelName- The name of the label to jump to.
-
addIInc
public void addIInc(int varIndex, int increment)Appends an IINC instruction to the end of the bytecode.- Parameters:
varIndex- The local variable index to increment.increment- The constant by which to increment the variable.
-
addReturn
public void addReturn(ReturnType returnType)
Appends a RETURN instruction to the end of the bytecode.- Parameters:
returnType- The opcode of the RETURN instruction (e.g., 0xAC for IRETURN).
-
addReturn
public void addReturn(int returnOpcode)
Appends a RETURN instruction to the end of the bytecode.- Parameters:
returnOpcode- The opcode of the RETURN instruction (e.g., 0xAC for IRETURN).
-
finalizeBytecode
public void finalizeBytecode() throws IOExceptionFinalizes the bytecode modifications by writing them back to the MethodEntry.- Throws:
IOException- If an I/O error occurs during writing.
-
computeFrames
public void computeFrames()
Computes and updates this method's StackMapTable frames.
-
forceComputeFrames
public void forceComputeFrames()
Forces recomputation of StackMapTable frames, even if bytecode wasn't modified.
-
isModified
public boolean isModified()
Returns whether the bytecode has been modified since loading.- Returns:
- true if modified, false otherwise
-
addLoad
public void addLoad(int i, String desc)Appends a load instruction based on the type descriptor.- Parameters:
i- The local variable index.desc- The type descriptor (I, J, F, D, or reference type).
-
addInvokeStatic
public void addInvokeStatic(String className, String methodName, String methodDescriptor)
Inserts an INVOKESTATIC instruction into the bytecode.- Parameters:
className- The fully qualified class name (e.g., "java/lang/System").methodName- The name of the static method to invoke (e.g., "currentTimeMillis").methodDescriptor- The method descriptor (e.g., "()J").
-
endsWithReturn
public boolean endsWithReturn()
Checks if the bytecode ends with a return instruction.- Returns:
- true if the bytecode ends with a return instruction, false otherwise.
-
addLdc
public void addLdc(String value)
Inserts an LDC or LDC_W instruction to load a String constant onto the operand stack.- Parameters:
value- The String constant to load.
-
addInvokeVirtual
public void addInvokeVirtual(String className, String methodName, String methodDescriptor)
Inserts an INVOKEVIRTUAL instruction into the bytecode.- Parameters:
className- The fully qualified class name (e.g., "java/io/PrintStream").methodName- The name of the method to invoke (e.g., "println").methodDescriptor- The method descriptor (e.g., "(Ljava/lang/String;)V").
-
addGetStatic
public void addGetStatic(String className, String fieldName, String fieldDescriptor)
Inserts a GETSTATIC instruction into the bytecode.- Parameters:
className- The fully qualified class name (e.g., "java/lang/System").fieldName- The name of the static field (e.g., "out").fieldDescriptor- The field descriptor (e.g., "Ljava/io/PrintStream;").
-
addTableSwitch
public void addTableSwitch(int low, int high, int defaultOffset, Map<Integer,Integer> jumpOffsets)Appends a TABLESWITCH instruction to the bytecode.- Parameters:
low- The lowest key value.high- The highest key value.defaultOffset- The default branch offset (relative to this instruction).jumpOffsets- Map of key values to branch offsets (relative to this instruction).
-
addLookupSwitch
public void addLookupSwitch(int defaultOffset, Map<Integer,Integer> matchOffsets)Appends a LOOKUPSWITCH instruction to the bytecode.- Parameters:
defaultOffset- The default branch offset (relative to this instruction).matchOffsets- Map of case values to branch offsets (relative to this instruction).
-
-