FileDocCategorySizeDatePackage
Code.javaAPI DocJava SE 6 API12400Tue Jun 10 00:22:16 BST 2008com.sun.org.apache.bcel.internal.classfile

Code

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,v 1.1.2.1 2005/07/31 23:46:37 jeffsuttor Exp $
author
M. Dahm
see
Attribute
see
CodeException
see
LineNumberTable
see
LocalVariableTable

Fields Summary
private int
max_stack
private int
max_locals
private int
code_length
private byte[]
code
private int
exception_table_length
private CodeException[]
exception_table
private int
attributes_count
private Attribute[]
attributes
Constructors Summary
public Code(Code c)
Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a physical copy.

    this(c.getNameIndex(), c.getLength(), c.getMaxStack(), c.getMaxLocals(),
	 c.getCode(), c.getExceptionTable(), c.getAttributes(), 
	 c.getConstantPool());
  
Code(int name_index, int length, DataInputStream file, ConstantPool constant_pool)

param
name_index Index pointing to the name Code
param
length Content length in bytes
param
file Input stream
param
constant_pool Array of constants

    // Initialize with some default values which will be overwritten later
    this(name_index, length,
	 file.readUnsignedShort(), file.readUnsignedShort(),
	 (byte[])null, (CodeException[])null, (Attribute[])null,
	 constant_pool);

    code_length = file.readInt();
    code = new byte[code_length]; // Read byte code
    file.readFully(code);

    /* Read exception table that contains all regions where an exception
     * handler is active, i.e., a try { ... } catch() block.
     */
    exception_table_length = file.readUnsignedShort();
    exception_table        = new CodeException[exception_table_length];

    for(int i=0; i < exception_table_length; i++)
      exception_table[i] = new CodeException(file);

    /* Read all attributes, currently `LineNumberTable' and
     * `LocalVariableTable'
     */
    attributes_count = file.readUnsignedShort();
    attributes = new Attribute[attributes_count];
    for(int i=0; i < attributes_count; i++)
      attributes[i] = Attribute.readAttribute(file, constant_pool);

    /* Adjust length, because of setAttributes in this(), s.b.  length
     * is incorrect, because it didn't take the internal attributes
     * into account yet! Very subtle bug, fixed in 3.1.1.
     */
    this.length = length;
  
public Code(int name_index, int length, int max_stack, int max_locals, byte[] code, CodeException[] exception_table, Attribute[] attributes, ConstantPool constant_pool)

param
name_index Index pointing to the name Code
param
length Content length in bytes
param
max_stack Maximum size of stack
param
max_locals Number of local variables
param
code Actual byte code
param
exception_table Table of handled exceptions
param
attributes Attributes of code: LineNumber or LocalVariable
param
constant_pool Array of constants

    super(Constants.ATTR_CODE, name_index, length, constant_pool);

    this.max_stack         = max_stack;
    this.max_locals        = max_locals;

    setCode(code);
    setExceptionTable(exception_table);
    setAttributes(attributes); // Overwrites length!
  
Methods Summary
public voidaccept(com.sun.org.apache.bcel.internal.classfile.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.

param
v Visitor object

    v.visitCode(this);
  
private final intcalculateLength()

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

    int len = 0;

    for(int i=0; i < attributes_count; i++)
      len += attributes[i].length + 6 /*attribute header size*/;

    return len + getInternalLength();
  
public com.sun.org.apache.bcel.internal.classfile.Attributecopy(com.sun.org.apache.bcel.internal.classfile.ConstantPool constant_pool)

return
deep copy of this attribute

    Code c = (Code)clone();
    c.code          = (byte[])code.clone();
    c.constant_pool = constant_pool;
  
    c.exception_table = new CodeException[exception_table_length];
    for(int i=0; i < exception_table_length; i++)
      c.exception_table[i] = exception_table[i].copy();

    c.attributes = new Attribute[attributes_count];
    for(int i=0; i < attributes_count; i++)
      c.attributes[i] = attributes[i].copy(constant_pool);

    return c;
  
public final voiddump(java.io.DataOutputStream file)
Dump code attribute to file stream in binary format.

param
file Output file stream
throws
IOException

    super.dump(file);

    file.writeShort(max_stack);
    file.writeShort(max_locals);
    file.writeInt(code_length);
    file.write(code, 0, code_length);

    file.writeShort(exception_table_length);
    for(int i=0; i < exception_table_length; i++)
      exception_table[i].dump(file);
	
    file.writeShort(attributes_count);
    for(int i=0; i < attributes_count; i++)
      attributes[i].dump(file);
  
public final com.sun.org.apache.bcel.internal.classfile.Attribute[]getAttributes()

return
Collection of code attributes.
see
Attribute

 return attributes; 
public final byte[]getCode()

return
Actual byte code of the method.

 return code; 
public final com.sun.org.apache.bcel.internal.classfile.CodeException[]getExceptionTable()

return
Table of handled exceptions.
see
CodeException

 return exception_table; 
private final intgetInternalLength()

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

    return 2 /*max_stack*/ + 2 /*max_locals*/ + 4 /*code length*/ 
      + code_length /*byte-code*/
      + 2 /*exception-table length*/ 
      + 8 * exception_table_length /* exception table */
      + 2 /* attributes count */;
  
public com.sun.org.apache.bcel.internal.classfile.LineNumberTablegetLineNumberTable()

return
LineNumberTable of Code, if it has one

    for(int i=0; i < attributes_count; i++)
      if(attributes[i] instanceof LineNumberTable)
	return (LineNumberTable)attributes[i];

    return null;
  
public com.sun.org.apache.bcel.internal.classfile.LocalVariableTablegetLocalVariableTable()

return
LocalVariableTable of Code, if it has one

    for(int i=0; i < attributes_count; i++)
      if(attributes[i] instanceof LocalVariableTable)
	return (LocalVariableTable)attributes[i];

    return null;
  
public final intgetMaxLocals()

return
Number of local variables.

 return max_locals; 
public final intgetMaxStack()

return
Maximum size of stack used by this method.

 return max_stack; 
public final voidsetAttributes(com.sun.org.apache.bcel.internal.classfile.Attribute[] attributes)

param
attributes.

    this.attributes  = attributes;
    attributes_count = (attributes == null)? 0 : attributes.length;
    length = calculateLength(); // Adjust length
  
public final voidsetCode(byte[] code)

param
code byte code

    this.code   = code;
    code_length = (code == null)? 0 : code.length;
  
public final voidsetExceptionTable(com.sun.org.apache.bcel.internal.classfile.CodeException[] exception_table)

param
exception_table exception table

    this.exception_table   = exception_table;
    exception_table_length = (exception_table == null)? 0 :
      exception_table.length;
  
public final voidsetMaxLocals(int max_locals)

param
max_locals maximum number of local variables

    this.max_locals = max_locals;
  
public final voidsetMaxStack(int max_stack)

param
max_stack maximum stack size

    this.max_stack = max_stack;
  
public final java.lang.StringtoString(boolean verbose)

return
String representation of code chunk.

    StringBuffer buf;

    buf = new StringBuffer("Code(max_stack = " + max_stack +
			   ", max_locals = " + max_locals +
			   ", code_length = " + code_length + ")\n" +
			   Utility.codeToString(code, constant_pool, 0, -1, verbose));

    if(exception_table_length > 0) {
      buf.append("\nException handler(s) = \n" + "From\tTo\tHandler\tType\n");

      for(int i=0; i < exception_table_length; i++)
	buf.append(exception_table[i].toString(constant_pool, verbose) + "\n");
    }

    if(attributes_count > 0) {
      buf.append("\nAttribute(s) = \n");

      for(int i=0; i < attributes_count; i++)
	buf.append(attributes[i].toString() + "\n");
    }

    return buf.toString();    
  
public final java.lang.StringtoString()

return
String representation of code chunk.

    return toString(true);