public class CodeAttributeEditor extends java.lang.Object implements AttributeVisitor, InstructionVisitor, ExceptionInfoVisitor, StackMapFrameVisitor, VerificationTypeVisitor, LineNumberInfoVisitor, LocalVariableInfoVisitor, LocalVariableTypeInfoVisitor, TypeAnnotationVisitor, TargetInfoVisitor, LocalVariableTargetElementVisitor
AttributeVisitor
accumulates specified changes to code, and then applies these
accumulated changes to the code attributes that it visits.
The class also supports labels (label()
) and exception handlers (catch_(int,int,int)
) in replacement sequences. They provide local branch offsets inside the
replacement sequences (CodeAttributeEditor.Label.offset()
). For example, replacing a specified instruction in
a method by a sequence of instructions, with the help of InstructionSequenceBuilder
:
Clazz clazz = ... Method method = ... CodeAttribute codeAttribute = ... int offset = ... CodeAttributeEditor codeAttributeEditor = new CodeAttributeEditor(); InstructionSequenceBuilder builder = new InstructionSequenceBuilder(clazz); // Create labels and instructions. final CodeAttributeEditor.Label TRY_START = codeAttributeEditor.label(); final CodeAttributeEditor.Label TRY_END = codeAttributeEditor.label(); final CodeAttributeEditor.Label CATCH_END = codeAttributeEditor.label(); final CodeAttributeEditor.Label CATCH_EXCEPTION = codeAttributeEditor.catch_(TRY_START.offset(), TRY_END.offset(), constantPoolEditor.addClassConstant("java/lang/Exception", null)); Instructions[] replacementInstructions = builder .label(TRY_START) ...... .label(TRY_END) .goto_(CATCH_END.offset()) .catch_(CATCH_EXCEPTION) ...... .athrow() .label(CATCH_END) ...... .instructions(); // Prepare the editor for this code. codeAttributeEditor.reset(codeAttribute.u4codeLength); // Edit the code -- in this case, replace an instruction. codeAttributeEditor.replaceInstruction(offset, replacementInstructions); // Apply the changes. codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
Modifier and Type | Class and Description |
---|---|
static class |
CodeAttributeEditor.Label
This pseudo-instruction represents a label that marks an instruction offset, for use in the
context of the code attribute editor only.
|
Modifier and Type | Field and Description |
---|---|
boolean[] |
deleted |
Instruction[] |
postInsertions |
Instruction[] |
preInsertions |
Instruction[] |
preOffsetInsertions |
Instruction[] |
replacements |
Constructor and Description |
---|
CodeAttributeEditor()
Creates a new CodeAttributeEditor that automatically updates frame sizes and shrinks
instructions.
|
CodeAttributeEditor(boolean updateFrameSizes,
boolean shrinkInstructions)
Creates a new CodeAttributeEditor.
|
Modifier and Type | Method and Description |
---|---|
CodeAttributeEditor.Label |
catch_(int startOffset,
int endOffset,
int catchType)
Creates a new catch instance that can be used as a pseudo-instruction to mark the start of an
exception handler.
|
CodeAttributeEditor.Label |
catch_(int identifier,
int startOffset,
int endOffset,
int catchType)
Creates a new catch instance that can be used as a pseudo-instruction to mark the start of an
exception handler.
|
void |
clearModifications(int instructionOffset)
Clears all modifications of the instruction at the given offset.
|
void |
deleteInstruction(int instructionOffset)
Remembers to delete the instruction at the given offset.
|
void |
extend(int codeLength)
Extends the size of the accumulated code changes.
|
int |
getNewOffset(int oldOffset)
Get the new offset corresponding to the given old offset.
|
void |
insertAfterInstruction(int instructionOffset,
Instruction instruction)
Remembers to place the given instruction right after the instruction at the given offset.
|
void |
insertAfterInstruction(int instructionOffset,
Instruction[] instructions)
Remembers to place the given instructions right after the instruction at the given offset.
|
void |
insertBeforeInstruction(int instructionOffset,
Instruction instruction)
Remembers to place the given instruction right before the instruction at the given offset.
|
void |
insertBeforeInstruction(int instructionOffset,
Instruction[] instructions)
Remembers to place the given instructions right before the instruction at the given offset.
|
void |
insertBeforeOffset(int instructionOffset,
Instruction instruction)
Remembers to place the given instruction right before the instruction at the given offset.
|
void |
insertBeforeOffset(int instructionOffset,
Instruction[] instructions)
Remembers to place the given instructions right before the instruction at the given offset.
|
boolean |
isModified()
Returns whether the code has been modified in any way.
|
boolean |
isModified(int instructionOffset)
Returns whether the instruction at the given offset has been modified in any way.
|
CodeAttributeEditor.Label |
label()
Creates a new label that can be used as a pseudo-instruction to mark a local offset.
|
CodeAttributeEditor.Label |
label(int identifier)
Creates a new label that can be used as a pseudo-instruction to mark a local offset.
|
CodeAttributeEditor.Label |
line(int lineNumber)
Creates a new line number instance that will insert the given line number at the current
offset.
|
CodeAttributeEditor.Label |
line(int identifier,
int lineNumber,
java.lang.String source)
Creates a new line number instance that will insert the given line number at the current
offset.
|
CodeAttributeEditor.Label |
line(int lineNumber,
java.lang.String source)
Creates a new line number instance that will insert the given line number at the current
offset.
|
void |
replaceInstruction(int instructionOffset,
Instruction instruction)
Remembers to replace the instruction at the given offset by the given instruction.
|
void |
replaceInstruction(int instructionOffset,
Instruction[] instructions)
Remembers to replace the instruction at the given offset by the given instructions.
|
void |
reset(int codeLength)
Resets the accumulated code changes for a given anticipated maximum code length.
|
void |
undeleteInstruction(int instructionOffset)
Remembers not to delete the instruction at the given offset.
|
void |
visitAnyAttribute(Clazz clazz,
Attribute attribute)
Visits any Attribute instance.
|
void |
visitAnyStackMapFrame(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
StackMapFrame stackMapFrame)
Visits any StackMapFrame instance.
|
void |
visitAnyTargetInfo(Clazz clazz,
TypeAnnotation typeAnnotation,
TargetInfo targetInfo)
Visits any TargetInfo instance.
|
void |
visitAnyTypeAnnotationsAttribute(Clazz clazz,
TypeAnnotationsAttribute typeAnnotationsAttribute)
Visits any TypeAnnotationsAttribute instance.
|
void |
visitAnyVerificationType(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
VerificationType verificationType)
Visits any VerificationType instance.
|
void |
visitBranchInstruction(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
BranchInstruction branchInstruction) |
void |
visitCodeAttribute(Clazz clazz,
Method method,
CodeAttribute codeAttribute) |
void |
visitCodeAttribute0(Clazz clazz,
Method method,
CodeAttribute codeAttribute) |
void |
visitConstantInstruction(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
ConstantInstruction constantInstruction) |
void |
visitExceptionInfo(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
ExceptionInfo exceptionInfo) |
void |
visitFullFrame(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
FullFrame fullFrame) |
void |
visitLineNumberInfo(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
LineNumberInfo lineNumberInfo) |
void |
visitLineNumberTableAttribute(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
LineNumberTableAttribute lineNumberTableAttribute) |
void |
visitLocalVariableInfo(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
LocalVariableInfo localVariableInfo) |
void |
visitLocalVariableTableAttribute(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
LocalVariableTableAttribute localVariableTableAttribute) |
void |
visitLocalVariableTargetElement(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
TypeAnnotation typeAnnotation,
LocalVariableTargetInfo localVariableTargetInfo,
LocalVariableTargetElement localVariableTargetElement) |
void |
visitLocalVariableTargetInfo(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
TypeAnnotation typeAnnotation,
LocalVariableTargetInfo localVariableTargetInfo) |
void |
visitLocalVariableTypeInfo(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
LocalVariableTypeInfo localVariableTypeInfo) |
void |
visitLocalVariableTypeTableAttribute(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
LocalVariableTypeTableAttribute localVariableTypeTableAttribute) |
void |
visitLookUpSwitchInstruction(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
LookUpSwitchInstruction lookUpSwitchInstruction) |
void |
visitMoreZeroFrame(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
MoreZeroFrame moreZeroFrame) |
void |
visitOffsetTargetInfo(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
TypeAnnotation typeAnnotation,
OffsetTargetInfo offsetTargetInfo) |
void |
visitSameOneFrame(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
SameOneFrame sameOneFrame) |
void |
visitSimpleInstruction(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
SimpleInstruction simpleInstruction) |
void |
visitStackMapAttribute(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
StackMapAttribute stackMapAttribute) |
void |
visitStackMapTableAttribute(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
StackMapTableAttribute stackMapTableAttribute) |
void |
visitTableSwitchInstruction(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
TableSwitchInstruction tableSwitchInstruction) |
void |
visitTypeAnnotation(Clazz clazz,
TypeAnnotation typeAnnotation)
Visits any TypeAnnotation instance.
|
void |
visitUninitializedType(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
UninitializedType uninitializedType) |
void |
visitVariableInstruction(Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
VariableInstruction variableInstruction) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
visitAnnotationDefaultAttribute, visitAnyAnnotationsAttribute, visitAnyParameterAnnotationsAttribute, visitBootstrapMethodsAttribute, visitConstantValueAttribute, visitDeprecatedAttribute, visitDeprecatedAttribute, visitDeprecatedAttribute, visitDeprecatedAttribute, visitEnclosingMethodAttribute, visitExceptionsAttribute, visitInnerClassesAttribute, visitMethodParametersAttribute, visitModuleAttribute, visitModuleMainClassAttribute, visitModulePackagesAttribute, visitNestHostAttribute, visitNestMembersAttribute, visitPermittedSubclassesAttribute, visitRecordAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleAnnotationsAttribute, visitRuntimeInvisibleParameterAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeInvisibleTypeAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleAnnotationsAttribute, visitRuntimeVisibleParameterAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitRuntimeVisibleTypeAnnotationsAttribute, visitSignatureAttribute, visitSignatureAttribute, visitSignatureAttribute, visitSignatureAttribute, visitSignatureAttribute, visitSourceDebugExtensionAttribute, visitSourceDirAttribute, visitSourceFileAttribute, visitSyntheticAttribute, visitSyntheticAttribute, visitSyntheticAttribute, visitSyntheticAttribute, visitUnknownAttribute
visitAnyInstruction, visitAnySwitchInstruction
visitLessZeroFrame, visitSameZeroFrame
visitDoubleType, visitFloatType, visitIntegerType, visitLongType, visitNullType, visitObjectType, visitStackDoubleType, visitStackFloatType, visitStackIntegerType, visitStackLongType, visitStackNullType, visitStackObjectType, visitStackTopType, visitStackUninitializedThisType, visitStackUninitializedType, visitTopType, visitUninitializedThisType, visitVariablesDoubleType, visitVariablesFloatType, visitVariablesIntegerType, visitVariablesLongType, visitVariablesNullType, visitVariablesObjectType, visitVariablesTopType, visitVariablesUninitializedThisType, visitVariablesUninitializedType
visitTypeAnnotation, visitTypeAnnotation, visitTypeAnnotation, visitTypeAnnotation, visitTypeAnnotation
visitCatchTargetInfo, visitEmptyTargetInfo, visitEmptyTargetInfo, visitEmptyTargetInfo, visitFormalParameterTargetInfo, visitSuperTypeTargetInfo, visitThrowsTargetInfo, visitTypeArgumentTargetInfo, visitTypeParameterBoundTargetInfo, visitTypeParameterBoundTargetInfo, visitTypeParameterBoundTargetInfo, visitTypeParameterBoundTargetInfo, visitTypeParameterTargetInfo, visitTypeParameterTargetInfo
public Instruction[] preOffsetInsertions
public Instruction[] preInsertions
public Instruction[] replacements
public Instruction[] postInsertions
public boolean[] deleted
public CodeAttributeEditor()
public CodeAttributeEditor(boolean updateFrameSizes, boolean shrinkInstructions)
updateFrameSizes
- specifies whether frame sizes of edited code should be updated.shrinkInstructions
- specifies whether added instructions should automatically be shrunk
before being written.public void reset(int codeLength)
extend(int)
.codeLength
- the length of the code that will be edited next.public void extend(int codeLength)
codeLength
- the length of the code that will be edited next.public void insertBeforeOffset(int instructionOffset, Instruction instruction)
instructionOffset
- the offset of the instruction.instruction
- the new instruction.public void insertBeforeInstruction(int instructionOffset, Instruction instruction)
instructionOffset
- the offset of the instruction.instruction
- the new instruction.public void insertBeforeOffset(int instructionOffset, Instruction[] instructions)
instructionOffset
- the offset of the instruction.instructions
- the new instructions.public void insertBeforeInstruction(int instructionOffset, Instruction[] instructions)
instructionOffset
- the offset of the instruction.instructions
- the new instructions.public void replaceInstruction(int instructionOffset, Instruction instruction)
instructionOffset
- the offset of the instruction to be replaced.instruction
- the new instruction.public void replaceInstruction(int instructionOffset, Instruction[] instructions)
instructionOffset
- the offset of the instruction to be replaced.instructions
- the new instructions.public void insertAfterInstruction(int instructionOffset, Instruction instruction)
instructionOffset
- the offset of the instruction.instruction
- the new instruction.public void insertAfterInstruction(int instructionOffset, Instruction[] instructions)
instructionOffset
- the offset of the instruction.instructions
- the new instructions.public void deleteInstruction(int instructionOffset)
instructionOffset
- the offset of the instruction to be deleted.public void undeleteInstruction(int instructionOffset)
instructionOffset
- the offset of the instruction not to be deleted.public void clearModifications(int instructionOffset)
instructionOffset
- the offset of the instruction to be deleted.public boolean isModified()
public boolean isModified(int instructionOffset)
public int getNewOffset(int oldOffset)
public void visitAnyAttribute(Clazz clazz, Attribute attribute)
AttributeVisitor
visitAnyAttribute
in interface AttributeVisitor
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
visitCodeAttribute
in interface AttributeVisitor
public void visitCodeAttribute0(Clazz clazz, Method method, CodeAttribute codeAttribute)
public void visitStackMapAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapAttribute stackMapAttribute)
visitStackMapAttribute
in interface AttributeVisitor
public void visitStackMapTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapTableAttribute stackMapTableAttribute)
visitStackMapTableAttribute
in interface AttributeVisitor
public void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute)
visitLineNumberTableAttribute
in interface AttributeVisitor
public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute)
visitLocalVariableTableAttribute
in interface AttributeVisitor
public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute)
visitLocalVariableTypeTableAttribute
in interface AttributeVisitor
public void visitAnyTypeAnnotationsAttribute(Clazz clazz, TypeAnnotationsAttribute typeAnnotationsAttribute)
AttributeVisitor
visitAnyTypeAnnotationsAttribute
in interface AttributeVisitor
public void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)
visitSimpleInstruction
in interface InstructionVisitor
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
visitConstantInstruction
in interface InstructionVisitor
public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
visitVariableInstruction
in interface InstructionVisitor
public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
visitBranchInstruction
in interface InstructionVisitor
public void visitTableSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, TableSwitchInstruction tableSwitchInstruction)
visitTableSwitchInstruction
in interface InstructionVisitor
public void visitLookUpSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LookUpSwitchInstruction lookUpSwitchInstruction)
visitLookUpSwitchInstruction
in interface InstructionVisitor
public void visitExceptionInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, ExceptionInfo exceptionInfo)
visitExceptionInfo
in interface ExceptionInfoVisitor
public void visitAnyStackMapFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, StackMapFrame stackMapFrame)
StackMapFrameVisitor
visitAnyStackMapFrame
in interface StackMapFrameVisitor
public void visitSameOneFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameOneFrame sameOneFrame)
visitSameOneFrame
in interface StackMapFrameVisitor
public void visitMoreZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, MoreZeroFrame moreZeroFrame)
visitMoreZeroFrame
in interface StackMapFrameVisitor
public void visitFullFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, FullFrame fullFrame)
visitFullFrame
in interface StackMapFrameVisitor
public void visitAnyVerificationType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VerificationType verificationType)
VerificationTypeVisitor
visitAnyVerificationType
in interface VerificationTypeVisitor
public void visitUninitializedType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, UninitializedType uninitializedType)
visitUninitializedType
in interface VerificationTypeVisitor
public void visitLineNumberInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberInfo lineNumberInfo)
visitLineNumberInfo
in interface LineNumberInfoVisitor
public void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo)
visitLocalVariableInfo
in interface LocalVariableInfoVisitor
public void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo)
visitLocalVariableTypeInfo
in interface LocalVariableTypeInfoVisitor
public void visitTypeAnnotation(Clazz clazz, TypeAnnotation typeAnnotation)
TypeAnnotationVisitor
visitTypeAnnotation
in interface TypeAnnotationVisitor
public void visitAnyTargetInfo(Clazz clazz, TypeAnnotation typeAnnotation, TargetInfo targetInfo)
TargetInfoVisitor
visitAnyTargetInfo
in interface TargetInfoVisitor
public void visitLocalVariableTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, LocalVariableTargetInfo localVariableTargetInfo)
visitLocalVariableTargetInfo
in interface TargetInfoVisitor
public void visitOffsetTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, OffsetTargetInfo offsetTargetInfo)
visitOffsetTargetInfo
in interface TargetInfoVisitor
public void visitLocalVariableTargetElement(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, LocalVariableTargetInfo localVariableTargetInfo, LocalVariableTargetElement localVariableTargetElement)
visitLocalVariableTargetElement
in interface LocalVariableTargetElementVisitor
public CodeAttributeEditor.Label label()
CodeAttributeEditor.Label.offset()
).public CodeAttributeEditor.Label label(int identifier)
CodeAttributeEditor.Label.offset()
).public CodeAttributeEditor.Label catch_(int startOffset, int endOffset, int catchType)
CodeAttributeEditor.Label.offset()
).public CodeAttributeEditor.Label catch_(int identifier, int startOffset, int endOffset, int catchType)
CodeAttributeEditor.Label.offset()
).public CodeAttributeEditor.Label line(int lineNumber)
public CodeAttributeEditor.Label line(int lineNumber, java.lang.String source)
public CodeAttributeEditor.Label line(int identifier, int lineNumber, java.lang.String source)