Class CodeContext
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
private static class
Representation of an entry in the "exception_table" of a "Code" attribute (see JVMS 4.7.3).static interface
A throw-in interface that marksCodeContext.Offset
s as "fix-ups": During the execution offixUp()
, all "fix-ups" are invoked and can do last touches to the code attribute.private final class
class
A class that implements an insertion point into a "Code" attribute.class
AnCodeContext.Offset
who's sole purpose is to later create a 'LineNumberTable' attribute.(package private) static class
class
A class that represents an offset within a "Code" attribute.private class
private class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<Java.LocalVariableSlot>
All the local variables that are allocated in any block in thisCodeContext
.private final CodeContext.Offset
private final ClassFile
private byte[]
private CodeContext.Inserter
private CodeContext.LocalScope
Each List of Java.LocalVariableSlot is the local variables allocated for a block.private final CodeContext.Inserter
private final List<CodeContext.ExceptionTableEntry>
private static final int
private short
private int
private short
private final List<CodeContext.Relocatable>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addExceptionTableEntry
(CodeContext.Offset startPc, CodeContext.Offset endPc, CodeContext.Offset handlerPc, String catchTypeFd) Adds another entry to the "exception_table" of this code attribute (see JVMS 4.7.3).void
addLineNumberOffset
(int lineNumber) short
allocateLocalVariable
(short size) Allocates space for a local variable of the given size (1 or 2) on the local variable array.allocateLocalVariable
(short size, String name, IType type) Allocates space for a local variable of the given size (1 or 2) on the local variable array.private void
fixUp()
Fixes up all offsets.void
Fixes up all of the offsets and relocate() all relocatables.TheClassFile
this context is related to.private static int
invertBranchOpcode
(int branchOpcode) E.g.int
makeSpace
(int size) Inserts size NUL bytes at the current inserter's offset, advances the current inserter's offset by size, createsCodeContext.LineNumberOffset
s as necessary, and returns the current inserter's original offset (the offset of the first NUL byte that was inserted).private void
Grow the code if relocatables are required to.private static final StackMap
mergeStackMaps
(StackMap sm1, StackMap sm2) newCodeAttribute
(boolean debugLines, boolean debugVars) Allocates anCodeContext.Inserter
, set it to the current offset, and inserts it before the current offset.protected ClassFile.LocalVariableTableAttribute
Creates and inserts anCodeContext.Offset
at the current inserter's current position.private ClassFile.StackMapTableAttribute
boolean
boolean
void
Replaces the currentCodeContext.Inserter
with the remembered one (seepushInserter(CodeContext.Inserter)
).void
Asserts that the top operand is aninteger_variable_info
and pops it.Asserts that the top operand is anint_variable_info
orlong_variable_info
, then pops and returns it.void
Asserts that the top operand is along_variable_info
and pops it.Asserts that the top operand is anobject_variable_info
, and pops it.Asserts that the top operand is anobject_variable_info
,uninitialized_variable_info
oruninitializedThis_variable_info
, and pops it.Pops one entry from the current inserter's operand stack.void
popOperand
(String expectedFd) Pops the top operand, asserts that it is aninteger_variable_info
,long_variable_info
,float_variable_info
,double_variable_info
orvariable_object_info
, and asserts that it matches the given field descriptor.void
Pops the top entry from the operand stack and assert that it equals expected.void
popOperandAssignableTo
(String declaredFd) void
Asserts that the top operand is anobject_variable_info
or anull_variable_info
and pops it.void
Asserts that the top operand is anuninitializedThis_variable_info
and pops it.void
Asserts that the top operand is anuninitialized_variable_info
and pops it.void
void
void
Remembers the currentCodeContext.Inserter
, then replaces it with the new one.void
void
void
void
pushObjectOperand
(String fieldDescriptor) void
pushOperand
(String fieldDescriptor) Pushes oneobject_variable_info
,integer_variable_info
,double_variable_info
,float_variable_info
orlong_variable_info
entry onto the current inserter's operand stack.void
void
void
void
private static IClass
private void
relocate()
Relocates all relocatables.void
removeCode
(CodeContext.Offset from, CodeContext.Offset to) Removes all code between from and to.void
Restores the previous size of the local variables array.Remembers the current size of the local variables array.toString()
void
write
(byte b1) Inserts a byte at the current insertion position.void
write
(byte[] b) Inserts a sequence of bytes at the current insertion position.void
write
(byte b1, byte b2) Inserts bytes at the current insertion position.void
write
(byte b1, byte b2, byte b3) Inserts bytes at the current insertion position.void
write
(byte b1, byte b2, byte b3, byte b4) Inserts bytes at the current insertion position.void
writeBranch
(int opcode, CodeContext.Offset dst) Generates a "branch" instruction.void
writeOffset
(CodeContext.Offset src, CodeContext.Offset dst) Writes a four-byte offset (as it is used in TABLESWITCH and LOOKUPSWITCH) into this code context.void
writeShort
(int v)
-
Field Details
-
INITIAL_SIZE
private static final int INITIAL_SIZE- See Also:
-
classFile
-
maxStack
private int maxStack -
maxLocals
private short maxLocals -
code
private byte[] code -
beginning
-
end
-
currentInserter
-
exceptionTableEntries
-
allLocalVars
All the local variables that are allocated in any block in thisCodeContext
. -
currentLocalScope
Each List of Java.LocalVariableSlot is the local variables allocated for a block. They are pushed and popped onto the list together to make allocation of the next local variable slot easy. -
nextLocalVariableSlot
private short nextLocalVariableSlot -
relocatables
-
BRANCH_OPCODE_INVERSION
-
-
Constructor Details
-
CodeContext
Creates an empty "Code" attribute.
-
-
Method Details
-
getClassFile
TheClassFile
this context is related to. -
allocateLocalVariable
public short allocateLocalVariable(short size) Allocates space for a local variable of the given size (1 or 2) on the local variable array.As a side effect, the "max_locals" field of the "Code" attribute is updated.
The only way to deallocate local variables is to
saveLocalVariables()
and laterrestoreLocalVariables()
.- Parameters:
size
- The number of slots to allocate (1 or 2)- Returns:
- The slot index of the allocated variable
-
allocateLocalVariable
public Java.LocalVariableSlot allocateLocalVariable(short size, @Nullable String name, @Nullable IType type) Allocates space for a local variable of the given size (1 or 2) on the local variable array. As a side effect, the "max_locals" field of the "Code" attribute is updated.The only way to deallocate local variables is to
saveLocalVariables()
and laterrestoreLocalVariables()
.- Parameters:
size
- Number of slots to use (1 or 2)name
- The variable name; ifnull
, then the variable won't be written to the LocalVariableTabletype
- The variable type; if the name is not null, then the type is needed to write to the LocalVariableTable
-
saveLocalVariables
Remembers the current size of the local variables array. -
restoreLocalVariables
public void restoreLocalVariables()Restores the previous size of the local variables array. This MUST to be called for every call to saveLocalVariables as it closes the variable extent for all the active local variables in the current block. -
newCodeAttribute
-
newLineNumberTableAttribute
-
newStackMapTableAttribute
-
rawTypeOf
-
newLocalVariableTableAttribute
- Returns:
- A
ClassFile.LocalVariableTableAttribute
for thisCodeContext
-
fixUpAndRelocate
public void fixUpAndRelocate()Fixes up all of the offsets and relocate() all relocatables. -
maybeGrow
private void maybeGrow()Grow the code if relocatables are required to. -
fixUp
private void fixUp()Fixes up all offsets. -
relocate
private void relocate()Relocates all relocatables. -
write
public void write(byte[] b) Inserts a sequence of bytes at the current insertion position. CreatesCodeContext.LineNumberOffset
s as necessary. -
write
public void write(byte b1) Inserts a byte at the current insertion position. CreatesCodeContext.LineNumberOffset
s as necessary.This method is an optimization to avoid allocating small byte[] and ease GC load.
-
write
public void write(byte b1, byte b2) Inserts bytes at the current insertion position. CreatesCodeContext.LineNumberOffset
s as necessary.This method is an optimization to avoid allocating small byte[] and ease GC load.
-
write
public void write(byte b1, byte b2, byte b3) Inserts bytes at the current insertion position. CreatesCodeContext.LineNumberOffset
s as necessary.This method is an optimization to avoid allocating small byte[] and ease GC load.
-
write
public void write(byte b1, byte b2, byte b3, byte b4) Inserts bytes at the current insertion position. CreatesCodeContext.LineNumberOffset
s as necessary.This method is an optimization to avoid allocating small byte[] and ease GC load.
-
addLineNumberOffset
public void addLineNumberOffset(int lineNumber) -
makeSpace
public int makeSpace(int size) Inserts size NUL bytes at the current inserter's offset, advances the current inserter's offset by size, createsCodeContext.LineNumberOffset
s as necessary, and returns the current inserter's original offset (the offset of the first NUL byte that was inserted).Because the class file format does not support line numbers greater than 65535, these are treated as 65535.
- Parameters:
size
- The number of NUL bytes to inject- Returns:
- The offset of the first inserted byte
-
writeShort
public void writeShort(int v) -
writeBranch
Generates a "branch" instruction.- Parameters:
opcode
- One ofOpcode.GOTO
,Opcode.JSR
andOpcode.IF*
dst
- Where to branch
-
invertBranchOpcode
private static int invertBranchOpcode(int branchOpcode) E.g.Opcode.IFLT
("less than") inverts toOpcode.IFGE
("greater than or equal to"). -
createBranchOpcodeInversion
-
writeOffset
Writes a four-byte offset (as it is used in TABLESWITCH and LOOKUPSWITCH) into this code context. -
newOffset
Creates and inserts anCodeContext.Offset
at the current inserter's current position. -
newInserter
Allocates anCodeContext.Inserter
, set it to the current offset, and inserts it before the current offset.In clear text, this means that you can continue writing to the "Code" attribute, then
pushInserter(CodeContext.Inserter)
theCodeContext.Inserter
, then write again (which inserts bytes into the "Code" attribute at the previously remembered position), and thenpopInserter()
. -
currentInserter
- Returns:
- The current inserter
-
pushInserter
Remembers the currentCodeContext.Inserter
, then replaces it with the new one. -
popInserter
public void popInserter()Replaces the currentCodeContext.Inserter
with the remembered one (seepushInserter(CodeContext.Inserter)
). -
mergeStackMaps
-
addExceptionTableEntry
public void addExceptionTableEntry(CodeContext.Offset startPc, CodeContext.Offset endPc, CodeContext.Offset handlerPc, @Nullable String catchTypeFd) Adds another entry to the "exception_table" of this code attribute (see JVMS 4.7.3).- Parameters:
catchTypeFd
-null
meansfinally
clause
-
getAllLocalVars
- Returns:
- All the local variables that are allocated in any block in this
CodeContext
-
removeCode
Removes all code between from and to. Also removes anyCodeContext.Relocatable
s existing in that range. -
toString
-
pushOperand
Pushes oneobject_variable_info
,integer_variable_info
,double_variable_info
,float_variable_info
orlong_variable_info
entry onto the current inserter's operand stack. -
pushTopOperand
public void pushTopOperand() -
pushIntOperand
public void pushIntOperand() -
pushLongOperand
public void pushLongOperand() -
pushFloatOperand
public void pushFloatOperand() -
pushDoubleOperand
public void pushDoubleOperand() -
pushNullOperand
public void pushNullOperand() -
pushUninitializedThisOperand
public void pushUninitializedThisOperand() -
pushUninitializedOperand
public void pushUninitializedOperand() -
pushObjectOperand
-
pushOperand
-
peekNullOperand
public boolean peekNullOperand()- Returns:
- Whether the top operand is a
null_variable_info
-
peekObjectOperand
public boolean peekObjectOperand()- Returns:
- Whether the top operand is a
object_variable_info
-
peekOperand
- Returns:
- The verification type of the top operand
-
popOperand
Pops one entry from the current inserter's operand stack. -
popOperand
Pops the top entry from the operand stack and assert that it equals expected. -
popOperand
Pops the top operand, asserts that it is aninteger_variable_info
,long_variable_info
,float_variable_info
,double_variable_info
orvariable_object_info
, and asserts that it matches the given field descriptor. -
popOperandAssignableTo
-
popIntOperand
public void popIntOperand()Asserts that the top operand is aninteger_variable_info
and pops it. -
popLongOperand
public void popLongOperand()Asserts that the top operand is along_variable_info
and pops it. -
popUninitializedThisOperand
public void popUninitializedThisOperand()Asserts that the top operand is anuninitializedThis_variable_info
and pops it. -
popUninitializedVariableOperand
public void popUninitializedVariableOperand()Asserts that the top operand is anuninitialized_variable_info
and pops it. -
popReferenceOperand
public void popReferenceOperand()Asserts that the top operand is anobject_variable_info
or anull_variable_info
and pops it. -
popObjectOperand
Asserts that the top operand is anobject_variable_info
, and pops it.- Returns:
- The field descriptor of the popped object operand
-
popObjectOrUninitializedOrUninitializedThisOperand
public ClassFile.StackMapTableAttribute.VerificationTypeInfo popObjectOrUninitializedOrUninitializedThisOperand()Asserts that the top operand is anobject_variable_info
,uninitialized_variable_info
oruninitializedThis_variable_info
, and pops it. -
popIntOrLongOperand
Asserts that the top operand is anint_variable_info
orlong_variable_info
, then pops and returns it.
-