org.apache.bcel.classfile

Class Code

Implemented Interfaces:
Cloneable, Node, Serializable

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.
Version:
$Id: Code.java 386056 2006-03-15 11:31:56Z tcurdt $
Author:
M. Dahm
See Also:
Attribute, CodeException, LineNumberTable, LocalVariableTable

Field Summary

private Attribute[]
attributes
private int
attributes_count
private byte[]
code
private int
code_length
private CodeException[]
exception_table
private int
exception_table_length
private int
max_locals
private int
max_stack

Fields inherited from class org.apache.bcel.classfile.Attribute

constant_pool, length, name_index, readers, tag

Constructor Summary

Code(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
Code(int name_index, int length, int max_stack, int max_locals, byte[] code, CodeException[] exception_table, Attribute[] attributes, ConstantPool constant_pool)
Code(Code c)
Initialize from another object.

Method Summary

void
accept(Visitor v)
Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
private int
calculateLength()
Attribute
copy(ConstantPool _constant_pool)
void
dump(DataOutputStream file)
Dump code attribute to file stream in binary format.
Attribute[]
getAttributes()
byte[]
getCode()
CodeException[]
getExceptionTable()
private int
getInternalLength()
LineNumberTable
getLineNumberTable()
LocalVariableTable
getLocalVariableTable()
int
getMaxLocals()
int
getMaxStack()
void
setAttributes(Attribute[] attributes)
void
setCode(byte[] code)
void
setExceptionTable(CodeException[] exception_table)
void
setMaxLocals(int max_locals)
void
setMaxStack(int max_stack)
String
toString()
String
toString(boolean verbose)

Methods inherited from class org.apache.bcel.classfile.Attribute

accept, addAttributeReader, clone, copy, dump, getConstantPool, getLength, getNameIndex, getTag, readAttribute, removeAttributeReader, setConstantPool, setLength, setNameIndex, toString

Field Details

attributes

private Attribute[] attributes

attributes_count

private int attributes_count

code

private byte[] code

code_length

private int code_length

exception_table

private CodeException[] exception_table

exception_table_length

private int exception_table_length

max_locals

private int max_locals

max_stack

private int max_stack

Constructor Details

Code

(package private)  Code(int name_index,
                        int length,
                        DataInputStream file,
                        ConstantPool constant_pool)
            throws IOException
Parameters:
name_index - Index pointing to the name Code
length - Content length in bytes
file - Input stream
constant_pool - Array of constants

Code

public Code(int name_index,
            int length,
            int max_stack,
            int max_locals,
            byte[] code,
            CodeException[] exception_table,
            Attribute[] attributes,
            ConstantPool constant_pool)
Parameters:
name_index - Index pointing to the name Code
length - Content length in bytes
max_stack - Maximum size of stack
max_locals - Number of local variables
code - Actual byte code
exception_table - Table of handled exceptions
attributes - Attributes of code: LineNumber or LocalVariable
constant_pool - Array of constants

Code

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

Method Details

accept

public void accept(Visitor v)
Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
Specified by:
accept in interface Node
Overrides:
accept in interface Attribute
Parameters:
v - Visitor object

calculateLength

private final 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 _constant_pool)
Overrides:
copy in interface Attribute
Parameters:
_constant_pool - the constant pool to duplicate
Returns:
deep copy of this attribute

dump

public final void dump(DataOutputStream file)
            throws IOException
Dump code attribute to file stream in binary format.
Overrides:
dump in interface Attribute
Parameters:
file - Output file stream

getAttributes

public final Attribute[] getAttributes()
Returns:
Collection of code attributes.
See Also:
Attribute

getCode

public final byte[] getCode()
Returns:
Actual byte code of the method.

getExceptionTable

public final CodeException[] getExceptionTable()
Returns:
Table of handled exceptions.
See Also:
CodeException

getInternalLength

private final int getInternalLength()
Returns:
the internal length of this code attribute (minus the first 6 bytes) and excluding all its attributes

getLineNumberTable

public LineNumberTable getLineNumberTable()
Returns:
LineNumberTable of Code, if it has one

getLocalVariableTable

public LocalVariableTable getLocalVariableTable()
Returns:
LocalVariableTable of Code, if it has one

getMaxLocals

public final int getMaxLocals()
Returns:
Number of local variables.

getMaxStack

public final int getMaxStack()
Returns:
Maximum size of stack used by this method.

setAttributes

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

setCode

public final void setCode(byte[] code)
Parameters:
code - byte code

setExceptionTable

public final void setExceptionTable(CodeException[] exception_table)
Parameters:
exception_table - exception table

setMaxLocals

public final void setMaxLocals(int max_locals)
Parameters:
max_locals - maximum number of local variables

setMaxStack

public final void setMaxStack(int max_stack)
Parameters:
max_stack - maximum stack size

toString

public final String toString()
Overrides:
toString in interface Attribute
Returns:
String representation of code chunk.

toString

public final String toString(boolean verbose)
Returns:
String representation of code chunk.