edu.wisc.cs.java
Class CodeAttribute

java.lang.Object
  |
  +--edu.wisc.cs.java.AttributeInfo
        |
        +--edu.wisc.cs.java.CodeAttribute

public class CodeAttribute
extends AttributeInfo

This class represents the code attribute of a method. One such code attribute is expected for any method which is not native or abstract.


Fields inherited from class edu.wisc.cs.java.AttributeInfo
attributeData, name_index, num_bytes
 
Constructor Summary
CodeAttribute(char name_index, java.io.DataInputStream in)
          Constructs a new code attribute reading data from the given stream with the given name index.
CodeAttribute(int name_index, boolean static_meth)
          Creates a new CodeAttribute with the specified name index.
 
Method Summary
 int addLocal()
          Adds a new local variable to this code.
 void appendBytecode(Bytecode b)
          Adds a new bytecode to the end of this code attribute.
 Bytecode getBytecode(int i)
          Returns the specified bytecode in this method.
 int getMaxStack()
          Returns the maximum number of word on this methods operand stack at any time.
 int getNBytecodes()
          Returns the number of instructions in this code attribute.
 int getNBytes()
          Resolves label offsets and computes the size of this code attribute, not including the 6 byte attribute header.
 int getNLocals()
          Returns the number of local variables this code uses.
 int getSize()
          Returns the number of bytes necessary to represent this code attribute.
 void insertBytecode(int i, Bytecode b)
          Inserts a new instruction at the specified index.
 void removeBytecode(int i)
          Removes a bytecode at a specified index.
 void setMaxStack(int max)
          Sets the maximum number of words on the operand stack.
 void writeTo(java.io.DataOutputStream out)
          Writes code data to the specified stream.
 
Methods inherited from class edu.wisc.cs.java.AttributeInfo
getNameIndex
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CodeAttribute

public CodeAttribute(int name_index,
                     boolean static_meth)
Creates a new CodeAttribute with the specified name index. The code attribute will be empty, with a max_stack and max_locals of one.
Parameters:
name_index - the constant pool index of the UTF8 constant "Code".
static_meth - true if this code belongs to a static method.

CodeAttribute

public CodeAttribute(char name_index,
                     java.io.DataInputStream in)
              throws java.io.IOException
Constructs a new code attribute reading data from the given stream with the given name index.
Parameters:
n_index - the index in the constant pool of the attribute name.
in - the stream from which to read all attribute data.
Method Detail

getNLocals

public int getNLocals()
Returns the number of local variables this code uses.
Returns:
the number of local variables this code uses.

addLocal

public int addLocal()
Adds a new local variable to this code.
Returns:
the index of the new local variable.

setMaxStack

public void setMaxStack(int max)
Sets the maximum number of words on the operand stack.
Parameters:
max - the new max_stack value.

getMaxStack

public int getMaxStack()
Returns the maximum number of word on this methods operand stack at any time.
Returns:
the maximum number of words possible on the operand stack.

writeTo

public void writeTo(java.io.DataOutputStream out)
             throws java.io.IOException
Writes code data to the specified stream. The format of the data written is the format required by the Java Virtual Machine specification.
Overrides:
writeTo in class AttributeInfo
Parameters:
out - the destination stream to write data to.
Throws:
java.io.IOException - if there is a problem writing the code data.

getSize

public int getSize()
Returns the number of bytes necessary to represent this code attribute.
Overrides:
getSize in class AttributeInfo
Returns:
the number of bytes necessary to represent this code attribute.

getNBytes

public int getNBytes()
Resolves label offsets and computes the size of this code attribute, not including the 6 byte attribute header.
Returns:
the number of bytes needed for this data of this code attribute.

getNBytecodes

public int getNBytecodes()
Returns the number of instructions in this code attribute.
Returns:
the number of instructions in this code attribute.

getBytecode

public Bytecode getBytecode(int i)
Returns the specified bytecode in this method. Indeces are zero basesd and are indexed on the sequence of bytecodes, not the byte offset.
Parameters:
i - the index of the bytecode to be returned.
Returns:
the sepecified bytecode.
Throws:
ArrayIndexOutOfBoundsException - if the specified element is not in this method.

removeBytecode

public void removeBytecode(int i)
Removes a bytecode at a specified index.
Parameters:
i - the index of the instruction to remove.

insertBytecode

public void insertBytecode(int i,
                           Bytecode b)
Inserts a new instruction at the specified index.
Parameters:
i - the position at which to insert the new instruction.
b - the instruction to insert.

appendBytecode

public void appendBytecode(Bytecode b)
Adds a new bytecode to the end of this code attribute.
Parameters:
b - the instruction to append.