public class CodeContext extends Object
| Modifier and Type | Class and Description |
|---|---|
class |
CodeContext.BasicBlock
This
CodeContext.Offset marks the first byte of a "basic block" in the sense of JLS 17 "4.10.1 Verification by Type
Checking":
The intent is that a stack map frame must appear at the beginning of each basic block in a method. |
static interface |
CodeContext.FixUp
A throw-in interface that marks
CodeContext.Offsets as "fix-ups": During the execution of fixUp(), all "fix-ups" are invoked and can do last touches to the code attribute. |
class |
CodeContext.Inserter
A class that implements an insertion point into a "Code" attribute.
|
class |
CodeContext.LineNumberOffset
An
CodeContext.Offset who's sole purpose is to later create a 'LineNumberTable' attribute. |
class |
CodeContext.Offset
A class that represents an offset within a "Code" attribute.
|
| Constructor and Description |
|---|
CodeContext(ClassFile classFile)
Creates an empty "Code" attribute.
|
| Modifier and Type | Method and Description |
|---|---|
void |
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.
|
Java.LocalVariableSlot |
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.
|
CodeContext.Inserter |
currentInserter() |
void |
fixUpAndRelocate()
Fixes up all of the offsets and relocate() all relocatables.
|
List<Java.LocalVariableSlot> |
getAllLocalVars() |
ClassFile |
getClassFile()
The
ClassFile this context is related to. |
int |
makeSpace(int size)
Inserts size NUL bytes at the current inserter's offset, advances the current inserter's offset by
size, creates
CodeContext.LineNumberOffsets as necessary, and returns the current inserter's original
offset (the offset of the first NUL byte that was inserted). |
CodeContext.Offset |
newBasicBlock() |
ClassFile.CodeAttribute |
newCodeAttribute(int initialLocalsCount,
boolean debugLines,
boolean debugVars) |
CodeContext.Inserter |
newInserter()
Allocates an
CodeContext.Inserter, set it to the current offset, and inserts it before the current offset. |
protected ClassFile.LocalVariableTableAttribute |
newLocalVariableTableAttribute() |
CodeContext.Offset |
newOffset()
Creates and inserts an
CodeContext.Offset at the current inserter's current position. |
boolean |
peekNullOperand() |
boolean |
peekObjectOperand() |
ClassFile.StackMapTableAttribute.VerificationTypeInfo |
peekOperand() |
void |
popInserter()
Replaces the current
CodeContext.Inserter with the remembered one (see pushInserter(CodeContext.Inserter)). |
void |
popIntOperand()
Asserts that the top operand is an
integer_variable_info and pops it. |
ClassFile.StackMapTableAttribute.VerificationTypeInfo |
popIntOrLongOperand()
Asserts that the top operand is an
int_variable_info or long_variable_info, then pops and
returns it. |
void |
popLongOperand()
Asserts that the top operand is a
long_variable_info and pops it. |
String |
popObjectOperand()
Asserts that the top operand is an
object_variable_info, and pops it. |
ClassFile.StackMapTableAttribute.VerificationTypeInfo |
popObjectOrUninitializedOrUninitializedThisOperand()
Asserts that the top operand is an
object_variable_info, uninitialized_variable_info or
uninitializedThis_variable_info, and pops it. |
ClassFile.StackMapTableAttribute.VerificationTypeInfo |
popOperand()
Pops one entry from the current inserter's operand stack.
|
void |
popOperand(ClassFile.StackMapTableAttribute.VerificationTypeInfo expected)
Pops the top entry from the operand stack and assert that it equals expected.
|
void |
popOperand(String expectedFd)
Pops the top operand, asserts that it is an
integer_variable_info, long_variable_info, float_variable_info, double_variable_info or variable_object_info, and asserts that it matches
the given field descriptor. |
void |
popOperandAssignableTo(String declaredFd) |
void |
popReferenceOperand()
Asserts that the top operand is an
object_variable_info or a null_variable_info and pops it. |
void |
popUninitializedThisOperand()
Asserts that the top operand is an
uninitializedThis_variable_info and pops it. |
void |
popUninitializedVariableOperand()
Asserts that the top operand is an
uninitialized_variable_info and pops it. |
void |
pushDoubleOperand() |
void |
pushFloatOperand() |
void |
pushInserter(CodeContext.Inserter ins)
Remembers the current
CodeContext.Inserter, then replaces it with the new one. |
void |
pushIntOperand() |
void |
pushLongOperand() |
void |
pushNullOperand() |
void |
pushObjectOperand(String fieldDescriptor) |
void |
pushOperand(ClassFile.StackMapTableAttribute.VerificationTypeInfo topOperand) |
void |
pushOperand(String fieldDescriptor)
Pushes one
object_variable_info, integer_variable_info, double_variable_info, float_variable_info or long_variable_info entry onto the current inserter's operand stack. |
void |
pushTopOperand() |
void |
pushUninitializedOperand() |
void |
pushUninitializedThisOperand() |
void |
removeCode(CodeContext.Offset from,
CodeContext.Offset to)
Removes all code between from and to.
|
void |
restoreLocalVariables()
Restores the previous size of the local variables array.
|
List<Java.LocalVariableSlot> |
saveLocalVariables()
Remembers the current size of the local variables array.
|
String |
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 |
write(byte b1,
byte b2,
byte b3,
byte b4,
byte b5)
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) |
public CodeContext(ClassFile classFile)
public short allocateLocalVariable(short size)
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 later restoreLocalVariables().
size - The number of slots to allocate (1 or 2)public Java.LocalVariableSlot allocateLocalVariable(short size, @Nullable String name, @Nullable IType type)
The only way to deallocate local variables is to saveLocalVariables() and later restoreLocalVariables().
size - Number of slots to use (1 or 2)name - The variable name; if null, 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
LocalVariableTablepublic List<Java.LocalVariableSlot> saveLocalVariables()
public void restoreLocalVariables()
public ClassFile.CodeAttribute newCodeAttribute(int initialLocalsCount, boolean debugLines, boolean debugVars)
initialLocalsCount - The number of parameters, plus one iff there is a "this" parameter@Nullable protected ClassFile.LocalVariableTableAttribute newLocalVariableTableAttribute()
ClassFile.LocalVariableTableAttribute for this CodeContextpublic void fixUpAndRelocate()
public void write(byte[] b)
CodeContext.LineNumberOffsets as necessary.public void write(byte b1)
CodeContext.LineNumberOffsets as necessary.
This method is an optimization to avoid allocating small byte[] and ease GC load.
public void write(byte b1,
byte b2)
CodeContext.LineNumberOffsets as necessary.
This method is an optimization to avoid allocating small byte[] and ease GC load.
public void write(byte b1,
byte b2,
byte b3)
CodeContext.LineNumberOffsets as necessary.
This method is an optimization to avoid allocating small byte[] and ease GC load.
public void write(byte b1,
byte b2,
byte b3,
byte b4)
CodeContext.LineNumberOffsets as necessary.
This method is an optimization to avoid allocating small byte[] and ease GC load.
public void write(byte b1,
byte b2,
byte b3,
byte b4,
byte b5)
CodeContext.LineNumberOffsets as necessary.
This method is an optimization to avoid allocating small byte[] and ease GC load.
public void addLineNumberOffset(int lineNumber)
public int makeSpace(int size)
CodeContext.LineNumberOffsets 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.
size - The number of NUL bytes to injectpublic void writeShort(int v)
public void writeBranch(int opcode,
CodeContext.Offset dst)
opcode - One of Opcode.GOTO, Opcode.JSR and Opcode.IF*dst - Where to branchpublic void writeOffset(CodeContext.Offset src, CodeContext.Offset dst)
public CodeContext.Offset newOffset()
CodeContext.Offset at the current inserter's current position.public CodeContext.Offset newBasicBlock()
public CodeContext.Inserter newInserter()
CodeContext.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) the CodeContext.Inserter, then write again (which inserts bytes into the
"Code" attribute at the previously remembered position), and then popInserter().
public CodeContext.Inserter currentInserter()
public void pushInserter(CodeContext.Inserter ins)
CodeContext.Inserter, then replaces it with the new one.public void popInserter()
CodeContext.Inserter with the remembered one (see pushInserter(CodeContext.Inserter)).public void addExceptionTableEntry(CodeContext.Offset startPc, CodeContext.Offset endPc, CodeContext.Offset handlerPc, @Nullable String catchTypeFd)
catchTypeFd - null means finally clausepublic List<Java.LocalVariableSlot> getAllLocalVars()
CodeContextpublic void removeCode(CodeContext.Offset from, CodeContext.Offset to)
CodeContext.Relocatables
existing in that range.public void pushOperand(String fieldDescriptor)
object_variable_info, integer_variable_info, double_variable_info, float_variable_info or long_variable_info entry onto the current inserter's operand stack.public void pushTopOperand()
public void pushIntOperand()
public void pushLongOperand()
public void pushFloatOperand()
public void pushDoubleOperand()
public void pushNullOperand()
public void pushUninitializedThisOperand()
public void pushUninitializedOperand()
public void pushObjectOperand(String fieldDescriptor)
public void pushOperand(ClassFile.StackMapTableAttribute.VerificationTypeInfo topOperand)
public boolean peekNullOperand()
null_variable_infopublic boolean peekObjectOperand()
object_variable_infopublic ClassFile.StackMapTableAttribute.VerificationTypeInfo peekOperand()
public ClassFile.StackMapTableAttribute.VerificationTypeInfo popOperand()
public void popOperand(ClassFile.StackMapTableAttribute.VerificationTypeInfo expected)
public void popOperand(String expectedFd)
integer_variable_info, long_variable_info, float_variable_info, double_variable_info or variable_object_info, and asserts that it matches
the given field descriptor.public void popOperandAssignableTo(String declaredFd)
public void popIntOperand()
integer_variable_info and pops it.public void popLongOperand()
long_variable_info and pops it.public void popUninitializedThisOperand()
uninitializedThis_variable_info and pops it.public void popUninitializedVariableOperand()
uninitialized_variable_info and pops it.public void popReferenceOperand()
object_variable_info or a null_variable_info and pops it.public String popObjectOperand()
object_variable_info, and pops it.public ClassFile.StackMapTableAttribute.VerificationTypeInfo popObjectOrUninitializedOrUninitializedThisOperand()
object_variable_info, uninitialized_variable_info or
uninitializedThis_variable_info, and pops it.public ClassFile.StackMapTableAttribute.VerificationTypeInfo popIntOrLongOperand()
int_variable_info or long_variable_info, then pops and
returns it.Copyright © 2022. All rights reserved.