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
Fields inherited from class 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) Constructs a Code attribute.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.Finds the attribute ofStackMapinstance.voidsetAttributes(Attribute[] attributes) Sets the attributes for this Code.voidsetCode(byte[] code) Sets the byte code.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, removeAttributeReader, setConstantPool, setLength, setNameIndex
-
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.- 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- 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, etc. spawns 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- if an I/O error occurs.
-
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
Finds the attribute ofStackMapinstance.- Returns:
- StackMap of Code, if it has one, else null.
- Since:
- 6.8.0
-
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- if the code array is greater thanConst.MAX_CODE_SIZE.
-
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
-