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.
- See Also:
-
Field Summary
Fields inherited from class Attribute
constant_pool, EMPTY_ARRAY, length, name_index, tagModifier and TypeFieldDescriptionprotected ConstantPoolDeprecated.(since 6.0) will be made private; do not access directly, use getter/setter.static final Attribute[]Empty array.protected intDeprecated.(since 6.0) (since 6.0) will be made private; do not access directly, use getter/setter.protected intDeprecated.(since 6.0) will be made private; do not access directly, use getter/setter.protected byteDeprecated.(since 6.0) will be made private; do not access directly, use getter/setter. -
Constructor Summary
ConstructorsConstructorDescriptionCode(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.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.copy(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.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 Attribute
addAttributeReader, addAttributeReader, clone, getConstantPool, getLength, getName, getNameIndex, getTag, println, readAttribute, readAttribute, readAttribute, removeAttributeReader, setConstantPool, setLength, setNameIndexModifier and TypeMethodDescriptionstatic voidaddAttributeReader(String name, AttributeReader attributeReader) Deprecated.(6.0) UseAttribute.addAttributeReader(String, UnknownAttributeReader)instead.static voidaddAttributeReader(String name, UnknownAttributeReader unknownAttributeReader) Add an Attribute reader capable of parsing (user-defined) attributes named "name".clone()Use copy() if you want to have a deep copy(), that is, with all references copied correctly.final ConstantPoolGets the constant pool used by this object.final intGets the length of attribute field in bytes.getName()Gets the name of the attribute.final intGets the name index in constant pool of attribute name.final bytegetTag()Gets the tag of attribute, that is, its type.protected static voidPrints a message to stderr if debug mode is enabled.static AttributereadAttribute(DataInput dataInput, ConstantPool constantPool) Reads one attribute using standard eight-byte Code headers.static AttributereadAttribute(DataInput dataInput, ConstantPool constantPool, boolean compactCodeHeader) Reads one attribute using the selected Code header layout.static AttributereadAttribute(DataInputStream dataInputStream, ConstantPool constantPool) Reads one attribute using standard eight-byte Code headers.static voidremoveAttributeReader(String name) Remove attribute readerfinal voidsetConstantPool(ConstantPool constantPool) Sets the constant pool to be used for this object.final voidsetLength(int length) Sets the length in bytes.final voidsetNameIndex(int nameIndex) Sets the name index of attribute.
-
Constructor Details
-
Code
-
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.
-
-
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. -
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
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:
-
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
-
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
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
Sets the byte code.- Parameters:
code- byte code.- Throws:
ClassFormatException- Thrown if the code array is greater thanConst.MAX_CODE_SIZE.
-
setCompactCodeHeader
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
Sets the maximum number of local variables.- Parameters:
maxLocals- maximum number of local variables.
-
setMaxStack
Sets the maximum stack size.- Parameters:
maxStack- maximum stack size.
-
toString
-
toString
-