Package org.apache.bcel.classfile
Class Code
java.lang.Object
org.apache.bcel.classfile.Attribute
org.apache.bcel.classfile.Code
This class represents a chunk of Java byte code contained in a method. It is instantiated by the
Attribute.readAttribute() method. A Code attribute contains informations about operand stack, local
variables, byte code and the exceptions handled within this method.
This attribute has attributes itself, namely LineNumberTable which is used for debugging purposes and
LocalVariableTable which contains information about the local variables.
Code_attribute { u2 attribute_name_index; u4 attribute_length; u2 max_stack; u2 max_locals; u4 code_length; u1 code[code_length]; u2 exception_table_length; { u2 start_pc; u2 end_pc; u2 handler_pc; u2 catch_type; } exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; }
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Attribute[]
private byte[]
private CodeException[]
private int
private int
Fields inherited from class org.apache.bcel.classfile.Attribute
constant_pool, EMPTY_ARRAY, length, name_index, tag
-
Constructor Summary
ConstructorsConstructorDescriptionCode
(int nameIndex, int length, int maxStack, int maxLocals, byte[] code, CodeException[] exceptionTable, Attribute[] attributes, ConstantPool constantPool) Code
(int nameIndex, int length, DataInput file, ConstantPool constantPool) Initialize from another object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.private int
copy
(ConstantPool constantPool) void
dump
(DataOutputStream file) Dump code attribute to file stream in binary format.byte[]
getCode()
private int
int
int
Finds the attribute ofStackMap
instance.void
setAttributes
(Attribute[] attributes) void
setCode
(byte[] code) void
setExceptionTable
(CodeException[] exceptionTable) void
setMaxLocals
(int maxLocals) void
setMaxStack
(int maxStack) toString()
toString
(boolean verbose) Converts this object to a String.Methods inherited from class org.apache.bcel.classfile.Attribute
addAttributeReader, addAttributeReader, clone, getConstantPool, getLength, getName, getNameIndex, getTag, println, readAttribute, readAttribute, removeAttributeReader, setConstantPool, setLength, setNameIndex
-
Field Details
-
maxStack
private int maxStack -
maxLocals
private int maxLocals -
code
private byte[] code -
exceptionTable
-
attributes
-
-
Constructor Details
-
Code
Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical copy.- Parameters:
code
- The source Code.
-
Code
Code(int nameIndex, int length, DataInput file, ConstantPool constantPool) throws IOException - Parameters:
nameIndex
- Index pointing to the name Codelength
- Content length in bytesfile
- Input streamconstantPool
- Array of constants- Throws:
IOException
-
Code
public Code(int nameIndex, int length, int maxStack, int maxLocals, byte[] code, CodeException[] exceptionTable, Attribute[] attributes, ConstantPool constantPool) - Parameters:
nameIndex
- Index pointing to the name Codelength
- Content length in bytesmaxStack
- Maximum size of stackmaxLocals
- Number of local variablescode
- Actual byte codeexceptionTable
- of handled exceptionsattributes
- Attributes of code: LineNumber or LocalVariableconstantPool
- Array of constants
-
-
Method Details
-
accept
Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. -
calculateLength
private int calculateLength()- Returns:
- the full size of this code attribute, minus its first 6 bytes, including the size of all its contained attributes
-
copy
-
dump
Dump code attribute to file stream in binary format.- Overrides:
dump
in classAttribute
- Parameters:
file
- Output file stream- Throws:
IOException
- if an I/O error occurs.
-
getAttributes
- Returns:
- Collection of code attributes.
- See Also:
-
getCode
public byte[] getCode()- Returns:
- Actual byte code of the method.
-
getExceptionTable
- Returns:
- Table of handled exceptions.
- See Also:
-
getInternalLength
private int getInternalLength()- Returns:
- the internal length of this code attribute (minus the first 6 bytes) and excluding all its attributes
-
getLineNumberTable
- Returns:
- LineNumberTable of Code, if it has one
-
getLocalVariableTable
- Returns:
- LocalVariableTable of Code, if it has one
-
getMaxLocals
public int getMaxLocals()- Returns:
- Number of local variables.
-
getMaxStack
public int getMaxStack()- Returns:
- Maximum size of stack used by this method.
-
getStackMap
Finds the attribute ofStackMap
instance.- Returns:
- StackMap of Code, if it has one, else null.
- Since:
- 6.8.0
-
setAttributes
- Parameters:
attributes
- the attributes to set for this Code
-
setCode
public void setCode(byte[] code) - Parameters:
code
- byte code
-
setExceptionTable
- Parameters:
exceptionTable
- exception table
-
setMaxLocals
public void setMaxLocals(int maxLocals) - Parameters:
maxLocals
- maximum number of local variables
-
setMaxStack
public void setMaxStack(int maxStack) - Parameters:
maxStack
- maximum stack size
-
toString
-
toString
Converts this object to a String.- Parameters:
verbose
- Provides verbose output when true.- Returns:
- String representation of code chunk.
-