Package org.apache.bcel.classfile
Class Code
- java.lang.Object
-
- org.apache.bcel.classfile.Attribute
-
- org.apache.bcel.classfile.Code
-
- All Implemented Interfaces:
java.lang.Cloneable,Node
public final class Code extends Attribute
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:
Attribute,CodeException,LineNumberTable,LocalVariableTable
-
-
Field Summary
Fields Modifier and Type Field Description private Attribute[]attributesprivate byte[]codeprivate CodeException[]exceptionTableprivate intmaxLocalsprivate intmaxStack-
Fields inherited from class org.apache.bcel.classfile.Attribute
constant_pool, EMPTY_ARRAY, length, name_index, tag
-
-
Constructor Summary
Constructors Constructor Description Code(int nameIndex, int length, int maxStack, int maxLocals, byte[] code, CodeException[] exceptionTable, Attribute[] attributes, ConstantPool constantPool)Constructs a Code attribute.Code(int nameIndex, int length, java.io.DataInput dataInput, ConstantPool constantPool)Constructs a Code attribute object from a DataInput.Code(Code code)Initialize from another object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaccept(Visitor v)Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.private intcalculateLength()Attributecopy(ConstantPool constantPool)Creates a deep copy of this attribute.voiddump(java.io.DataOutputStream file)Dumps code attribute to file stream in binary format.Attribute[]getAttributes()Gets the collection of code attributes.byte[]getCode()Gets the actual byte code of the method.CodeException[]getExceptionTable()Gets the table of handled exceptions.private intgetInternalLength()Gets the internal length of this code attribute (minus the first 6 bytes) and excluding all its attributes.LineNumberTablegetLineNumberTable()Gets the LineNumberTable of Code, if it has one.LocalVariableTablegetLocalVariableTable()Gets the LocalVariableTable of Code, if it has one.LocalVariableTypeTablegetLocalVariableTypeTable()Gets the local variable type table attributeLocalVariableTypeTable.intgetMaxLocals()Gets the number of local variables.intgetMaxStack()Gets the maximum size of stack used by this method.StackMapgetStackMap()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.java.lang.StringtoString()java.lang.StringtoString(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 Detail
-
maxStack
private int maxStack
-
maxLocals
private int maxLocals
-
code
private byte[] code
-
exceptionTable
private CodeException[] exceptionTable
-
attributes
private Attribute[] attributes
-
-
Constructor Detail
-
Code
public Code(Code 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, java.io.DataInput dataInput, ConstantPool constantPool) throws java.io.IOExceptionConstructs 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- if the code array read fromfileis greater thanConst.MAX_CODE_SIZE.java.io.IOException
-
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 Detail
-
accept
public void accept(Visitor v)
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.
-
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
public Attribute copy(ConstantPool constantPool)
Creates a deep copy of this attribute.
-
dump
public void dump(java.io.DataOutputStream file) throws java.io.IOExceptionDumps code attribute to file stream in binary format.
-
getAttributes
public Attribute[] getAttributes()
Gets the collection of code attributes.- Returns:
- Collection of code attributes.
- See Also:
Attribute
-
getCode
public byte[] getCode()
Gets the actual byte code of the method.- Returns:
- Actual byte code of the method.
-
getExceptionTable
public CodeException[] getExceptionTable()
Gets the table of handled exceptions.- Returns:
- Table of handled exceptions.
- See Also:
CodeException
-
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
public LineNumberTable getLineNumberTable()
Gets the LineNumberTable of Code, if it has one.- Returns:
- LineNumberTable of Code, if it has one.
-
getLocalVariableTable
public LocalVariableTable getLocalVariableTable()
Gets the LocalVariableTable of Code, if it has one.- Returns:
- LocalVariableTable of Code, if it has one.
-
getLocalVariableTypeTable
public LocalVariableTypeTable 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
public StackMap getStackMap()
Finds the attribute ofStackMapinstance.- Returns:
- StackMap of Code, if it has one, else null.
- Since:
- 6.8.0
-
setAttributes
public void setAttributes(Attribute[] attributes)
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- if the code array is greater thanConst.MAX_CODE_SIZE.
-
setExceptionTable
public void setExceptionTable(CodeException[] exceptionTable)
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
public java.lang.String toString()
-
toString
public java.lang.String toString(boolean verbose)
Converts this object to a String.- Parameters:
verbose- Provides verbose output when true.- Returns:
- String representation of code chunk.
-
-