Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

ExceptionTable.java

00001 package de.fub.bytecode.classfile;
00002 
00003 import  de.fub.bytecode.Constants;
00004 import  java.io.*;
00005 
00006 /** 
00007  * Class is derived from <em>Attribute</em> and represents the table
00008  * of exceptions that are thrown by a method. This attribute may be used
00009  * once per method.
00010  *
00011  * @version $Id: ExceptionTable.java,v 1.1.1.1 2002/01/24 03:44:01 pserver Exp $
00012  * @author  <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A>
00013  * @see     Code
00014  * @see     Attribute 
00015  */
00016 public final class ExceptionTable extends Attribute {
00017   private int   number_of_exceptions;  // Table of indices into
00018   private int[] exception_index_table; // constant pool
00019 
00020   /**
00021    * @param name_index Index in constant pool
00022    * @param length Content length in bytes
00023    * @param exception_index_table Table of indices in constant pool
00024    * @param constant_pool Array of constants
00025    */
00026   public ExceptionTable(int        name_index, int length, 
00027             int[]      exception_index_table,
00028             ConstantPool constant_pool)
00029   {
00030     super(ATTR_EXCEPTIONS, name_index, length, constant_pool);
00031     setExceptionIndexTable(exception_index_table);
00032   }  
00033   /**
00034    * Construct object from file stream.
00035    * @param name_index Index in constant pool
00036    * @param length Content length in bytes
00037    * @param file Input stream
00038    * @param constant_pool Array of constants
00039    * @throw IOException
00040    */
00041   ExceptionTable(int name_index, int length, DataInputStream file,
00042          ConstantPool constant_pool) throws IOException
00043   {
00044     this(name_index, length, (int[])null, constant_pool);
00045 
00046     number_of_exceptions  = file.readUnsignedShort();
00047     exception_index_table = new int[number_of_exceptions];
00048 
00049     for(int i=0; i < number_of_exceptions; i++)
00050       exception_index_table[i] = file.readUnsignedShort();
00051   }  
00052   /**
00053    * Initialize from another object. Note that both objects use the same
00054    * references (shallow copy). Use clone() for a physical copy.
00055    */
00056   public ExceptionTable(ExceptionTable c) {
00057     this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(),
00058      c.getConstantPool());
00059   }  
00060   /**
00061    * Called by objects that are traversing the nodes of the tree implicitely
00062    * defined by the contents of a Java class. I.e., the hierarchy of methods,
00063    * fields, attributes, etc. spawns a tree of objects.
00064    *
00065    * @param v Visitor object
00066    */
00067   public void accept(Visitor v) {
00068     v.visitExceptionTable(this);
00069   }  
00070   /**
00071    * @return deep copy of this attribute
00072    */
00073   public Attribute copy(ConstantPool constant_pool) {
00074     ExceptionTable c = (ExceptionTable)clone();
00075     c.exception_index_table = (int[])exception_index_table.clone();
00076     c.constant_pool = constant_pool;
00077     return c;
00078   }  
00079   /**
00080    * Dump exceptions attribute to file stream in binary format.
00081    *
00082    * @param file Output file stream
00083    * @throw IOException
00084    */ 
00085   public final void dump(DataOutputStream file) throws IOException
00086   {
00087     super.dump(file);
00088     file.writeShort(number_of_exceptions);
00089     for(int i=0; i < number_of_exceptions; i++)
00090       file.writeShort(exception_index_table[i]);
00091   }  
00092   /**
00093    * @return Array of indices into constant pool of thrown exceptions.
00094    */  
00095   public final int[] getExceptionIndexTable() {return exception_index_table;}  
00096   /**
00097    * @return class names of thrown exceptions
00098    */
00099   public final String[] getExceptionNames() {
00100     String[] names = new String[number_of_exceptions];
00101     for(int i=0; i < number_of_exceptions; i++)
00102       names[i] = constant_pool.getConstantString(exception_index_table[i], CONSTANT_Class).
00103     replace('/', '.');
00104     return names;
00105   }  
00106   /**
00107    * @return Length of exception table.
00108    */  
00109   public final int getNumberOfExceptions() { return number_of_exceptions; }  
00110   /**
00111    * @param exception_index_table.
00112    * Also redefines number_of_exceptions according to table length.
00113    */
00114   public final void setExceptionIndexTable(int[] exception_index_table) {
00115     this.exception_index_table = exception_index_table;
00116     number_of_exceptions       = (exception_index_table == null)? 0 :
00117       exception_index_table.length;
00118   }  
00119   /**
00120    * @return String representation, i.e. a list of thrown exceptions.
00121    */ 
00122   public final String toString() {
00123     StringBuffer buf = new StringBuffer("");
00124     String       str;
00125 
00126     for(int i=0; i < number_of_exceptions; i++) {
00127       str = constant_pool.getConstantString(exception_index_table[i],
00128                         CONSTANT_Class);
00129       buf.append(Utility.compactClassName(str));
00130 
00131       if(i < number_of_exceptions - 1)
00132     buf.append(", ");
00133     }
00134 
00135     return buf.toString();    
00136   }  
00137 }

Generated at Thu Feb 7 06:45:15 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001