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.
|
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.
|
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, waitvisitAnnotationDefaultAttribute, 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, visitUnknownAttributevisitAnyInstruction, visitAnySwitchInstructionvisitLessZeroFrame, visitSameZeroFramevisitDoubleType, visitFloatType, visitIntegerType, visitLongType, visitNullType, visitObjectType, visitStackDoubleType, visitStackFloatType, visitStackIntegerType, visitStackLongType, visitStackNullType, visitStackObjectType, visitStackTopType, visitStackUninitializedThisType, visitStackUninitializedType, visitTopType, visitUninitializedThisType, visitVariablesDoubleType, visitVariablesFloatType, visitVariablesIntegerType, visitVariablesLongType, visitVariablesNullType, visitVariablesObjectType, visitVariablesTopType, visitVariablesUninitializedThisType, visitVariablesUninitializedTypevisitTypeAnnotation, visitTypeAnnotation, visitTypeAnnotation, visitTypeAnnotation, visitTypeAnnotationvisitCatchTargetInfo, visitEmptyTargetInfo, visitEmptyTargetInfo, visitEmptyTargetInfo, visitFormalParameterTargetInfo, visitSuperTypeTargetInfo, visitThrowsTargetInfo, visitTypeArgumentTargetInfo, visitTypeParameterBoundTargetInfo, visitTypeParameterBoundTargetInfo, visitTypeParameterBoundTargetInfo, visitTypeParameterBoundTargetInfo, visitTypeParameterTargetInfo, visitTypeParameterTargetInfopublic 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 void visitAnyAttribute(Clazz clazz, Attribute attribute)
AttributeVisitorvisitAnyAttribute in interface AttributeVisitorpublic void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
visitCodeAttribute in interface AttributeVisitorpublic void visitCodeAttribute0(Clazz clazz, Method method, CodeAttribute codeAttribute)
public void visitStackMapAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapAttribute stackMapAttribute)
visitStackMapAttribute in interface AttributeVisitorpublic void visitStackMapTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, StackMapTableAttribute stackMapTableAttribute)
visitStackMapTableAttribute in interface AttributeVisitorpublic void visitLineNumberTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberTableAttribute lineNumberTableAttribute)
visitLineNumberTableAttribute in interface AttributeVisitorpublic void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute)
visitLocalVariableTableAttribute in interface AttributeVisitorpublic void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute)
visitLocalVariableTypeTableAttribute in interface AttributeVisitorpublic void visitAnyTypeAnnotationsAttribute(Clazz clazz, TypeAnnotationsAttribute typeAnnotationsAttribute)
AttributeVisitorvisitAnyTypeAnnotationsAttribute in interface AttributeVisitorpublic void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)
visitSimpleInstruction in interface InstructionVisitorpublic void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
visitConstantInstruction in interface InstructionVisitorpublic void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
visitVariableInstruction in interface InstructionVisitorpublic void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
visitBranchInstruction in interface InstructionVisitorpublic void visitTableSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, TableSwitchInstruction tableSwitchInstruction)
visitTableSwitchInstruction in interface InstructionVisitorpublic void visitLookUpSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LookUpSwitchInstruction lookUpSwitchInstruction)
visitLookUpSwitchInstruction in interface InstructionVisitorpublic void visitExceptionInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, ExceptionInfo exceptionInfo)
visitExceptionInfo in interface ExceptionInfoVisitorpublic void visitAnyStackMapFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, StackMapFrame stackMapFrame)
StackMapFrameVisitorvisitAnyStackMapFrame in interface StackMapFrameVisitorpublic void visitSameOneFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SameOneFrame sameOneFrame)
visitSameOneFrame in interface StackMapFrameVisitorpublic void visitMoreZeroFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, MoreZeroFrame moreZeroFrame)
visitMoreZeroFrame in interface StackMapFrameVisitorpublic void visitFullFrame(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, FullFrame fullFrame)
visitFullFrame in interface StackMapFrameVisitorpublic void visitAnyVerificationType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VerificationType verificationType)
VerificationTypeVisitorvisitAnyVerificationType in interface VerificationTypeVisitorpublic void visitUninitializedType(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, UninitializedType uninitializedType)
visitUninitializedType in interface VerificationTypeVisitorpublic void visitLineNumberInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LineNumberInfo lineNumberInfo)
visitLineNumberInfo in interface LineNumberInfoVisitorpublic void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo)
visitLocalVariableInfo in interface LocalVariableInfoVisitorpublic void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo)
visitLocalVariableTypeInfo in interface LocalVariableTypeInfoVisitorpublic void visitTypeAnnotation(Clazz clazz, TypeAnnotation typeAnnotation)
TypeAnnotationVisitorvisitTypeAnnotation in interface TypeAnnotationVisitorpublic void visitAnyTargetInfo(Clazz clazz, TypeAnnotation typeAnnotation, TargetInfo targetInfo)
TargetInfoVisitorvisitAnyTargetInfo in interface TargetInfoVisitorpublic void visitLocalVariableTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, LocalVariableTargetInfo localVariableTargetInfo)
visitLocalVariableTargetInfo in interface TargetInfoVisitorpublic void visitOffsetTargetInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, OffsetTargetInfo offsetTargetInfo)
visitOffsetTargetInfo in interface TargetInfoVisitorpublic void visitLocalVariableTargetElement(Clazz clazz, Method method, CodeAttribute codeAttribute, TypeAnnotation typeAnnotation, LocalVariableTargetInfo localVariableTargetInfo, LocalVariableTargetElement localVariableTargetElement)
visitLocalVariableTargetElement in interface LocalVariableTargetElementVisitorpublic 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()).