Class Code
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];
}
The layout above uses the standard Code header. This class also supports compact Code headers found in historical class file versions 45.0 through 45.2.
Here, "compact" describes the narrower encoding of three fields: max_stack and max_locals each use u1, and code_length
uses u2, instead of u2, u2, and u4, respectively. The notation u1, u2, and u4 denotes unsigned
one-, two-, and four-byte values. These three fields therefore occupy 4 bytes instead of 8, excluding the common six-byte attribute prefix.
The layout of that prefix and the remaining Code fields is unchanged; the bytecode itself is not compressed.
isCompactCodeHeader() identifies this narrower layout.
Class file versions 45.0 through 45.2 also occur with standard Code headers accepted by modern JVMs, so the version alone does not identify the layout.
ClassParser uses standard headers by default; ClassParser.setCompactCodeHeaderEnabled(boolean) explicitly selects compact headers
for those versions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Attribute[]private byte[]private booleanWhethermax_stack,max_locals, andcode_lengthuseu1/u1/u2(4 bytes) instead ofu2/u2/u4(8 bytes).private CodeException[]private intprivate intFields inherited from class org.apache.bcel.classfile.Attribute
constant_pool, EMPTY_ARRAY, length, name_index, tag -
Constructor Summary
ConstructorsModifierConstructorDescriptionCode(int nameIndex, int length, int maxStack, int maxLocals, byte[] code, CodeException[] exceptionTable, Attribute[] attributes, ConstantPool constantPool) Constructs a Code attribute using a standard eight-byte header.privateCode(int nameIndex, int length, int maxStack, int maxLocals, byte[] code, CodeException[] exceptionTable, Attribute[] attributes, ConstantPool constantPool, boolean compactCodeHeader) (package private)Code(int nameIndex, int length, DataInput dataInput, ConstantPool constantPool) Constructs a Code attribute object from a DataInput.(package private)Code(int nameIndex, int length, DataInput dataInput, ConstantPool constantPool, boolean compactCodeHeader) Reads a Code attribute using the selected header layout.Initialize from another object. -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.private intcopy(ConstantPool constantPool) Creates a deep copy of this attribute.voiddump(DataOutputStream file) Dumps code attribute to file stream in binary format.Gets the collection of code attributes.byte[]getCode()Gets the actual byte code of the method.Gets the table of handled exceptions.private intGets the internal length of this code attribute (minus the first 6 bytes) and excluding all its attributes.Gets the LineNumberTable of Code, if it has one.Gets the LocalVariableTable of Code, if it has one.Gets the local variable type table attributeLocalVariableTypeTable.intGets the number of local variables.intGets the maximum size of stack used by this method.Gets theStackMapattribute.booleanGets whether this attribute uses the compact four-byte Code header.voidsetAttributes(Attribute[] attributes) Sets the attributes for this Code.voidsetCode(byte[] code) Sets the byte code.voidsetCompactCodeHeader(boolean compactCodeHeader) Sets the Code header layout and recalculates the attribute length.voidsetExceptionTable(CodeException[] exceptionTable) Sets the exception table.voidsetMaxLocals(int maxLocals) Sets the maximum number of local variables.voidsetMaxStack(int maxStack) Sets the maximum stack size.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, readAttribute, removeAttributeReader, setConstantPool, setLength, setNameIndex
-
Field Details
-
compactCodeHeader
private boolean compactCodeHeaderWhethermax_stack,max_locals, andcode_lengthuseu1/u1/u2(4 bytes) instead ofu2/u2/u4(8 bytes). Compact describes these field widths, not compression of the bytecode or the rest of the attribute. -
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 dataInput, ConstantPool constantPool) throws IOException Constructs a Code attribute object from a DataInput.- Parameters:
nameIndex- Index pointing to the name Code.length- Content length in bytes.dataInput- Data input.constantPool- Array of constants.- Throws:
ClassFormatException- Thrown if the code array read fromfileis greater thanConst.MAX_CODE_SIZE.IOException
-
Code
Code(int nameIndex, int length, DataInput dataInput, ConstantPool constantPool, boolean compactCodeHeader) throws IOException Reads a Code attribute using the selected header layout.- Throws:
IOException
-
Code
public Code(int nameIndex, int length, int maxStack, int maxLocals, byte[] code, CodeException[] exceptionTable, Attribute[] attributes, ConstantPool constantPool) Constructs a Code attribute using a standard eight-byte header.Use
setCompactCodeHeader(boolean)to select the compact four-byte header after construction.- Parameters:
nameIndex- Index pointing to the name Code.length- Content length in bytes.maxStack- Maximum size of stack.maxLocals- Number of local variables.code- Actual byte code.exceptionTable- of handled exceptions.attributes- Attributes of code: LineNumber or LocalVariable.constantPool- Array of constants.- Throws:
ClassFormatException- Thrown if the code array is greater thanConst.MAX_CODE_SIZE.
-
Code
private Code(int nameIndex, int length, int maxStack, int maxLocals, byte[] code, CodeException[] exceptionTable, Attribute[] attributes, ConstantPool constantPool, boolean compactCodeHeader)
-
-
Method Details
-
accept
Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. That is, the hierarchy of methods, fields, attributes, and other elements forms 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
Creates a deep copy of this attribute. -
dump
Dumps code attribute to file stream in binary format.- Overrides:
dumpin classAttribute- Parameters:
file- Output file stream.- Throws:
IOException- Thrown if an I/O error occurs.ClassFormatException- Thrown if a compact header'smax_stackormax_localsis outside 0 through 255, or its code length exceeds 65535 bytes.
-
getAttributes
Gets the collection of code attributes.- Returns:
- Collection of code attributes.
- See Also:
-
getCode
public byte[] getCode()Gets the actual byte code of the method.- Returns:
- Actual byte code of the method.
-
getExceptionTable
Gets the table of handled exceptions.- Returns:
- Table of handled exceptions.
- See Also:
-
getInternalLength
private int getInternalLength()Gets the internal length of this code attribute (minus the first 6 bytes) and excluding all its attributes.- Returns:
- The internal length of this code attribute (minus the first 6 bytes) and excluding all its attributes.
-
getLineNumberTable
Gets the LineNumberTable of Code, if it has one.- Returns:
- LineNumberTable of Code, if it has one.
-
getLocalVariableTable
Gets the LocalVariableTable of Code, if it has one.- Returns:
- LocalVariableTable of Code, if it has one.
-
getLocalVariableTypeTable
Gets the local variable type table attributeLocalVariableTypeTable.- Returns:
- LocalVariableTypeTable of Code, if it has one, null otherwise.
- Since:
- 6.10.0
-
getMaxLocals
public int getMaxLocals()Gets the number of local variables.- Returns:
- Number of local variables.
-
getMaxStack
public int getMaxStack()Gets the maximum size of stack used by this method.- Returns:
- Maximum size of stack used by this method.
-
getStackMap
Gets theStackMapattribute.- Returns:
- StackMap of Code, if it has one, else null.
- Since:
- 6.8.0
-
isCompactCodeHeader
public boolean isCompactCodeHeader()Gets whether this attribute uses the compact four-byte Code header.- Returns:
- Whether
max_stack,max_locals, andcode_lengthuseu1,u1, andu2, respectively, instead of the standardu2,u2, andu4. - Since:
- 6.14.0
- See Also:
-
setAttributes
Sets the attributes for this Code.- Parameters:
attributes- The attributes to set for this Code.
-
setCode
public void setCode(byte[] code) Sets the byte code.- Parameters:
code- byte code.- Throws:
ClassFormatException- Thrown if the code array is greater thanConst.MAX_CODE_SIZE.
-
setCompactCodeHeader
public void setCompactCodeHeader(boolean compactCodeHeader) Sets the Code header layout and recalculates the attribute length.Compact headers encode
max_stack,max_locals, andcode_lengthasu1,u1, andu2, respectively, totaling 4 bytes instead of the standard 8. This limitsmax_stackandmax_localsto 0 through 255 and the code length to 65535 bytes. The remaining attribute layout is unchanged. Compact headers are intended for historical class file versions 45.0 through 45.2. Enabling compact headers validates these values before changing the layout. Subsequent changes to them are checked bydump(DataOutputStream).This setting controls this attribute's own dump. When dumping an entire class,
JavaClass.dump(DataOutputStream)selects one layout for all method Code attributes based on the class version and the methods' header settings, without changing this attribute's setting.- Parameters:
compactCodeHeader- Whether to use a compact four-byte header (true) or a standard eight-byte header (false).- Throws:
ClassFormatException- Thrown ifcompactCodeHeaderistrueand the current values do not fit in a compact header.- Since:
- 6.14.0
-
setExceptionTable
Sets the exception table.- Parameters:
exceptionTable- exception table.
-
setMaxLocals
public void setMaxLocals(int maxLocals) Sets the maximum number of local variables.- Parameters:
maxLocals- maximum number of local variables.
-
setMaxStack
public void setMaxStack(int maxStack) Sets the maximum stack size.- 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.
-