FileDocCategorySizeDatePackage
CodeAttribute.javaAPI DocJ2ME CLDC 1.17306Wed Feb 05 15:56:04 GMT 2003kdp.classparser.attributes

CodeAttribute

public class CodeAttribute extends Attribute
Encapsulates the Code attribute of a Java class file.
author
Aaron Dietrich
version
$Id: CodeAttribute.java,v 1.2 2000/08/14 19:49:11 zmm3876 Exp $ Revision History $Log: CodeAttribute.java,v $ Revision 1.2 2000/08/14 19:49:11 zmm3876 *** empty log message *** Revision 1.1 2000/08/09 15:51:44 zmm3876 *** empty log message *** Revision 1.1.1.1 2000/07/07 13:34:23 jrv Initial import of kdp code Revision 1.1.1.1 2000/05/31 19:14:47 ritsun Initial import of kvmdt to CVS Revision 1.1 2000/04/25 00:30:39 ritsun Initial revision

Fields Summary
private int
attributeNameIndex
index into constant pool table containing the name of this class
private int
attributeLength
length of this attribute in bytes
private int
maxStack
the maximum depth of the operand stack of this method
private int
maxLocals
the number of local variables in the local variable array allocated upon invocation of this method
private int
codeLength
the number of bytes in the code array for this method
private byte[]
code
the bytes of Java virtual machine code that implement a method
private int
exceptionTableLength
the number of entries in the exceptionTable table
private ExceptionTable[]
exceptionTable
each entry describes one exception handler in the code array
private int
attributesCount
the number of attributes of the Code attribute
private AttributeInfo[]
attributes
array of attributes for this CodeAttribute
Constructors Summary
public CodeAttribute(DataInputStream iStream, ConstantPoolInfo[] constantPool, int attributeNameIndex, int attributeLength)
Constructor. Reads the CodeAttribute attribute from the class file.

param
iStream the input stream on which to read the class file
param
constantPool constant pool of the class file
param
attributeNameIndex attributeNameIndex member of attribute structure.
param
attributeLength attributeLength member of attribute structure.
exception
IOException pass IOExceptions up

      this.attributeNameIndex = attributeNameIndex;
      this.attributeLength = attributeLength;

      //read in the maxStack and maxLocals items
      maxStack = iStream.readUnsignedShort ();
      maxLocals = iStream.readUnsignedShort ();

      //read in the length of the code array
      codeLength = iStream.readInt ();
      
      //allocate space for and read in the code array
      code = new byte[codeLength];
      for (int lcv=0; lcv < codeLength; ++lcv)
        code[lcv] = iStream.readByte ();
      
      //read in the number of items in the exception table
      exceptionTableLength = iStream.readUnsignedShort ();

      //allocate space for and read in the exception table      
      exceptionTable = new ExceptionTable[exceptionTableLength];
      for (int lcv=0; lcv < exceptionTableLength; ++lcv)
        exceptionTable[lcv] = new ExceptionTable (iStream);
      
      //read in the number of attributes
      attributesCount = iStream.readUnsignedShort ();

      //allocate space for and read in the attributes
      attributes = new AttributeInfo[attributesCount];
      for (int lcv=0; lcv < attributesCount; ++lcv)
        attributes[lcv] = new AttributeInfo (iStream, constantPool);
     
Methods Summary
public booleancontainsLine(int lineNumber)
Query this code attribute to see if it contains the source file line number specified by lineNumber.

return
boolean true if the line number is in this code attribute, false otherwise

      //get the line number table
      LineNumberTableAttribute     lines = getLineNumberTable ();

      //if the line number table was found tell it to look up the line number
      if (lines != null)
        return (lines.containsLine (lineNumber));
      else
        return false; //could not find the line number table so return false
     
public intgetAttributeCount()

        return attributesCount;
     
public AttributeInfo[]getAttributes()

        return attributes;
     
public byte[]getByteCodes()
Returns the byte code for the method

return
byte[] byte code

       byte[] ret = new byte[ code.length ];
       System.arraycopy( code, 0, ret, 0, code.length );
       return ret;
   
public intgetCodeLength()

        return codeLength;
     
public LineNumberTableAttributegetLineNumberTable()
Retrieves the line number table of this code attribute

return
LineNumberTableAttribute this code attribute's line number table.

      int           attrIndex = 0;
      boolean       found = false;

      //loop through this code attribute's attributes until the line number
      //table is found or there are no more to look through
      while ((!found) && (attrIndex < attributesCount))
        {
         if (attributes[attrIndex].getType () == AttributeInfo.ATTR_LINENUMBER)
           found = true;
         else
           ++attrIndex;
        }

      if (found)
        {//if we found the line number table return it
         AttributeInfo              attrInfo = attributes[attrIndex];
         LineNumberTableAttribute   lineNumberTable =
                                 (LineNumberTableAttribute) attrInfo.getInfo ();
         return lineNumberTable;
        }
      else
        return null; //did not find the line number table so return null
     
public java.lang.StringtoString(ConstantPoolInfo[] constantPool)
Returns the CodeAttribute attribute in a nice easy to read format as a string.

param
constantPool constant pool of the class file
return
String the attribute as a nice easy to read String

      ConstantUtf8Info            utf8Info;
      String                    s = new String ("");

      utf8Info = (ConstantUtf8Info) constantPool[attributeNameIndex];
      s = s + utf8Info.toString ();

      for (int lcv = 0; lcv < attributesCount; ++lcv)
        s = s + "\n\t\t" + attributes[lcv].toString ();

      return s;