home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / Exceptions.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  3.9 KB  |  169 lines

  1. /*
  2.  *
  3.  * @(#) Exceptions.java 1.5@(#)
  4.  *
  5.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  6.  * 
  7.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  8.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  9.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  10.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  11.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  12.  * THIS SOFTWARE OR ITS DERIVATIVES.
  13.  * 
  14.  */
  15.  
  16. /**
  17.  * <p>
  18.  * sunw.demo.classfile.Exceptions
  19.  * </p> 
  20.  *
  21.  * @version 1.0
  22.  * @author Laurence P. G. Cable
  23.  */
  24.  
  25.  
  26. package sunw.demo.classfile;
  27.  
  28. import java.io.DataOutputStream;
  29. import java.io.IOException;
  30.  
  31. import sunw.demo.classfile.Attribute;
  32. import sunw.demo.classfile.ClassFile;
  33. import sunw.demo.classfile.ClassConstant;
  34.  
  35. /**
  36.  * <p>
  37.  * The Exceptions class extends the Attribute class to enumerate the
  38.  * exception types generated by method implementations in a class file
  39.  * </p>
  40.  */
  41.  
  42. class Exceptions extends Attribute {
  43.  
  44.     private ClassConstant[] exceptions;
  45.  
  46.     /**
  47.      * <p> construct an Exceptions attribute that enumerates the exceptions </p>
  48.      *
  49.      * @param exs[]    an array of exception class constants
  50.      * @param cf    the containing class file
  51.      */
  52.  
  53.     Exceptions(ClassConstant[] exs, ClassFile cf) {
  54.         super(Attribute.EXCEPTIONS, cf);
  55.  
  56.         // we should validate that the ClassConstants are all
  57.         // subclasses of Exception here ...
  58.  
  59.         exceptions = exs;
  60.     }
  61.  
  62.     /**
  63.      * <p> construct an Exceptions attribute that enumerates the exceptions </p>
  64.      *
  65.      * @param exs[]    an array of exception class types
  66.      * @param cf    the containing class file
  67.      *
  68.      */
  69.     Exceptions(Class[] exs, ClassFile cf) {
  70.         super(Attribute.EXCEPTIONS, cf);
  71.  
  72.         // we should validate that the ClassConstants are all
  73.         // subclasses of Exception here ...
  74.  
  75.     ClassConstant[] cc = new ClassConstant[exs.length];
  76.  
  77.     for (int i = 0; i < exs.length; i++)
  78.         cc[i] = cf.addClassConstant(exs[i].getName());
  79.  
  80.     exceptions = cc;
  81.     }
  82.  
  83.     /**
  84.      * <p> write the Exceptions attribute to the stream </p>
  85.      *
  86.      * @param dos the output stream
  87.      *
  88.      * @throws IOException
  89.      */
  90.  
  91.     void write(DataOutputStream dos) throws IOException {
  92.         dos.writeShort(getNameConstantPoolIndex());
  93.         dos.writeInt(getLength());
  94.     
  95.     if (exceptions != null && exceptions.length > 0) {
  96.             dos.writeShort(exceptions.length);
  97.  
  98.             for (int i = 0; i < exceptions.length; i++) {
  99.         dos.writeShort(exceptions[i].getConstantPoolIndex());
  100.             }
  101.     } else dos.writeShort(0);
  102.     }
  103.  
  104.     /**
  105.      * @return the Object's equality
  106.      */
  107.  
  108.     public boolean equals(Object o) {
  109.         if (o instanceof Exceptions) {
  110.             Exceptions other = (Exceptions)o;
  111.  
  112.             if (exceptions.length == other.exceptions.length) {
  113.                 for (int i = 0; i < exceptions.length; i++) {
  114.                     if (!exceptions[i].equals(other.exceptions[i]))
  115.                 return false;
  116.                 }
  117.  
  118.             return true;
  119.         }
  120.          }
  121.  
  122.         return false;
  123.     }
  124.  
  125.     /**
  126.      * @return a hashcode for the object.
  127.      */
  128.     public int hashCode() {
  129.     // We don't come here often, so we keep it simple.
  130.     return exceptions.length;
  131.     }
  132.  
  133.     /**
  134.      * @return the length of the Attribute in bytes
  135.      */
  136.  
  137.     int getLength() { return exceptions.length * 2 + 2; }
  138.  
  139.     /**
  140.      * <p> adds exception class to the attribute. </p>
  141.      *
  142.      * @param a class constant to add to the attribute
  143.      *
  144.      */
  145.  
  146.     void addException(ClassConstant ex) {
  147.  
  148.     // should verify that ClassConstant is exception subclass and not
  149.     // already in the attribute
  150.  
  151.     if (exceptions == null) {
  152.         exceptions = new ClassConstant[1];
  153.  
  154.         exceptions[0] = ex;
  155.     } else {
  156.         ClassConstant[] temp = new ClassConstant[exceptions.length + 1];
  157.         int             i;
  158.  
  159.         for (i = 0; i < exceptions.length; i++) {
  160.         temp[i] = exceptions[i];
  161.         }
  162.  
  163.         temp[i] = ex;
  164.  
  165.         exceptions = temp;
  166.     }
  167.     }
  168. }
  169.