Top Description Fields Constructors Methods
org.apache.bcel.classfile

public final Class Code

extends Attribute
Class Inheritance
Imports
java.io.DataInput, .DataOutputStream, .IOException, java.util.Arrays, org.apache.bcel.Const, org.apache.bcel.util.Args, org.apache.commons.lang3.ArrayUtils

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

Modifier and TypeField and Description
private Attribute[]
private byte[]
private CodeException[]
exceptionTable

Deprecated as a consequence of CodeException being deprecated.
private int
private int
Inherited from org.apache.bcel.classfile.Attribute:
constant_poolEMPTY_ARRAYlengthname_indextag

Constructor Summary

AccessConstructor and Description
public
Code(final Code
The source Code.
code
)

Initialize from another object.

pack-priv
Code(final int
Index pointing to the name Code
nameIndex
,
final int
Content length in bytes
length
,
final DataInput
Input stream
file
,
final ConstantPool
Array of constants
constantPool
)

public
Code(final int
Index pointing to the name Code
nameIndex
,
final int
Content length in bytes
length
,
final int
Maximum size of stack
maxStack
,
final int
Number of local variables
maxLocals
,
final byte[]
Actual byte code
code
,
final CodeException[]
of handled exceptions
exceptionTable
,
final Attribute[]
Attributes of code: LineNumber or LocalVariable
attributes
,
final ConstantPool
Array of constants
constantPool
)

Deprecated as a consequence of CodeException being deprecated.

Method Summary

Modifier and TypeMethod and Description
public void
accept(final Visitor
Visitor object
v
)

Implements abstract org.apache.bcel.classfile.Attribute.accept.

Implements org.apache.bcel.classfile.Node.accept.

Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
private int

Returns:

the full size of this code attribute, minus its first 6 bytes, including the size of all its contained attributes
calculateLength
()

public Attribute

Returns:

deep copy of this attribute
copy
(final ConstantPool
the constant pool to duplicate
constantPool
)

Implements abstract org.apache.bcel.classfile.Attribute.copy.

public void
dump(final DataOutputStream
Output file stream
file
)

Overrides org.apache.bcel.classfile.Attribute.dump.

Dump code attribute to file stream in binary format.
public Attribute[]

Returns:

Collection of code attributes.
getAttributes
()

public byte[]

Returns:

Actual byte code of the method.
getCode
()

public CodeException[]

Returns:

Table of handled exceptions.
getExceptionTable
()

Deprecated as a consequence of CodeException being deprecated.
private int

Returns:

the internal length of this code attribute (minus the first 6 bytes) and excluding all its attributes
getInternalLength
()

public LineNumberTable

Returns:

LineNumberTable of Code, if it has one
getLineNumberTable
()

public LocalVariableTable

Returns:

LocalVariableTable of Code, if it has one
getLocalVariableTable
()

public int

Returns:

Number of local variables.
getMaxLocals
()

public int

Returns:

Maximum size of stack used by this method.
getMaxStack
()

public StackMap

Returns:

StackMap of Code, if it has one, else null.
getStackMap
()

Finds the attribute of StackMap instance.

public void
setAttributes(final Attribute[]
the attributes to set for this Code
attributes
)

public void
setCode(final byte[]
byte code
code
)

public void
setExceptionTable(final CodeException[]
exception table
exceptionTable
)

Deprecated as a consequence of CodeException being deprecated.
public void
setMaxLocals(final int
maximum number of local variables
maxLocals
)

public void
setMaxStack(final int
maximum stack size
maxStack
)

public String

Returns:

String representation of code chunk.
toString
()

Overrides org.apache.bcel.classfile.Attribute.toString.

Returns a string representation of the object.
public String

Returns:

String representation of code chunk.
toString
(final boolean
Provides verbose output when true.
verbose
)

Converts this object to a String.

Inherited from org.apache.bcel.classfile.Attribute:
addAttributeReaderaddAttributeReaderclonegetConstantPoolgetLengthgetNamegetNameIndexgetTagprintlnreadAttributereadAttributeremoveAttributeReadersetConstantPoolsetLengthsetNameIndex

Field Detail

attributesback to summary
private Attribute[] attributes
codeback to summary
private byte[] code
exceptionTableback to summary
private CodeException[] exceptionTable

Deprecated

as a consequence of CodeException being deprecated. See corresponding docs for further information.

maxLocalsback to summary
private int maxLocals
maxStackback to summary
private int maxStack

Constructor Detail

Codeback to summary
public Code(final Code code)

Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical copy.

Parameters
code:Code

The source Code.

Codeback to summary
pack-priv Code(final int nameIndex, final int length, final DataInput file, final ConstantPool constantPool) throws IOException
Parameters
nameIndex:int

Index pointing to the name Code

length:int

Content length in bytes

file:DataInput

Input stream

constantPool:ConstantPool

Array of constants

Codeback to summary
public Code(final int nameIndex, final int length, final int maxStack, final int maxLocals, final byte[] code, final CodeException[] exceptionTable, final Attribute[] attributes, final ConstantPool constantPool)

Deprecated

as a consequence of CodeException being deprecated. See corresponding docs for further information.

Parameters
nameIndex:int

Index pointing to the name Code

length:int

Content length in bytes

maxStack:int

Maximum size of stack

maxLocals:int

Number of local variables

code:byte[]

Actual byte code

exceptionTable:CodeException[]

of handled exceptions

attributes:Attribute[]

Attributes of code: LineNumber or LocalVariable

constantPool:ConstantPool

Array of constants

Method Detail

acceptback to summary
public void accept(final Visitor v)

Implements abstract org.apache.bcel.classfile.Attribute.accept.

Implements org.apache.bcel.classfile.Node.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.

Parameters
v:Visitor

Visitor object

Annotations
@Override
calculateLengthback to summary
private int calculateLength()
Returns:int

the full size of this code attribute, minus its first 6 bytes, including the size of all its contained attributes

copyback to summary
public Attribute copy(final ConstantPool constantPool)

Implements abstract org.apache.bcel.classfile.Attribute.copy.

Parameters
constantPool:ConstantPool

the constant pool to duplicate

Returns:Attribute

deep copy of this attribute

Annotations
@Override
dumpback to summary
public void dump(final DataOutputStream file) throws IOException

Overrides org.apache.bcel.classfile.Attribute.dump.

Dump code attribute to file stream in binary format.

Parameters
file:DataOutputStream

Output file stream

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
getAttributesback to summary
public Attribute[] getAttributes()
Returns:Attribute[]

Collection of code attributes.

See Also
Attribute
getCodeback to summary
public byte[] getCode()
Returns:byte[]

Actual byte code of the method.

getExceptionTableback to summary
public CodeException[] getExceptionTable()

Deprecated

as a consequence of CodeException being deprecated. See corresponding docs for further information.

Returns:CodeException[]

Table of handled exceptions.

See Also
CodeException
getInternalLengthback to summary
private int getInternalLength()
Returns:int

the internal length of this code attribute (minus the first 6 bytes) and excluding all its attributes

getLineNumberTableback to summary
public LineNumberTable getLineNumberTable()
Returns:LineNumberTable

LineNumberTable of Code, if it has one

getLocalVariableTableback to summary
public LocalVariableTable getLocalVariableTable()
Returns:LocalVariableTable

LocalVariableTable of Code, if it has one

getMaxLocalsback to summary
public int getMaxLocals()
Returns:int

Number of local variables.

getMaxStackback to summary
public int getMaxStack()
Returns:int

Maximum size of stack used by this method.

getStackMapback to summary
public StackMap getStackMap()

Finds the attribute of StackMap instance.

Returns:StackMap

StackMap of Code, if it has one, else null.

Since
6.8.0
setAttributesback to summary
public void setAttributes(final Attribute[] attributes)
Parameters
attributes:Attribute[]

the attributes to set for this Code

setCodeback to summary
public void setCode(final byte[] code)
Parameters
code:byte[]

byte code

setExceptionTableback to summary
public void setExceptionTable(final CodeException[] exceptionTable)

Deprecated

as a consequence of CodeException being deprecated. See corresponding docs for further information.

Parameters
exceptionTable:CodeException[]

exception table

setMaxLocalsback to summary
public void setMaxLocals(final int maxLocals)
Parameters
maxLocals:int

maximum number of local variables

setMaxStackback to summary
public void setMaxStack(final int maxStack)
Parameters
maxStack:int

maximum stack size

toStringback to summary
public String toString()

Overrides org.apache.bcel.classfile.Attribute.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

String representation of code chunk.

Annotations
@Override
toStringback to summary
public String toString(final boolean verbose)

Converts this object to a String.

Parameters
verbose:boolean

Provides verbose output when true.

Returns:String

String representation of code chunk.