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:41:37 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(Constants.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],
00103                          Constants.CONSTANT_Class).
00104     replace('/', '.');
00105     return names;
00106   }  
00107   /**
00108    * @return Length of exception table.
00109    */  
00110   public final int getNumberOfExceptions() { return number_of_exceptions; }  
00111   /**
00112    * @param exception_index_table.
00113    * Also redefines number_of_exceptions according to table length.
00114    */
00115   public final void setExceptionIndexTable(int[] exception_index_table) {
00116     this.exception_index_table = exception_index_table;
00117     number_of_exceptions       = (exception_index_table == null)? 0 :
00118       exception_index_table.length;
00119   }  
00120   /**
00121    * @return String representation, i.e., a list of thrown exceptions.
00122    */ 
00123   public final String toString() {
00124     StringBuffer buf = new StringBuffer("");
00125     String       str;
00126 
00127     for(int i=0; i < number_of_exceptions; i++) {
00128       str = constant_pool.getConstantString(exception_index_table[i],
00129                         Constants.CONSTANT_Class);
00130       buf.append(Utility.compactClassName(str, false));
00131 
00132       if(i < number_of_exceptions - 1)
00133     buf.append(", ");
00134     }
00135 
00136     return buf.toString();    
00137   }  
00138 }

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