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

  1. /*
  2.  *
  3.  * @(#) ClassConstant.java 1.4@(#)
  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.ClassConstant
  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.ClassFile;
  32. import sunw.demo.classfile.ConstantPoolEntry;
  33. import sunw.demo.classfile.UTF8Constant;
  34.  
  35. class ClassConstant extends ConstantPoolEntry {
  36.  
  37.     private UTF8Constant name;
  38.  
  39.     /**
  40.      * <p> Construct a CONSTANT_CLASS constant pool entry </p>
  41.      */
  42.  
  43.     ClassConstant(String className, ClassFile cf) {
  44.         super(CONSTANT_CLASS, cf);
  45.     
  46.         name = cf.addUTF8Constant(ClassFile.fullyQualifiedForm(className));
  47.  
  48.         addToConstantPool();
  49.     }
  50.  
  51.     /**
  52.      * <p> write the CONSTANT_CLASS to the stream </p>
  53.      *
  54.      * @param dos the stream.
  55.      *
  56.      * @throws IOException
  57.      */
  58.  
  59.     void write(DataOutputStream dos) throws IOException {
  60.     
  61.     if (debug()) {
  62.         System.err.println(getConstantPoolIndex() +
  63.                    " CLASS: "          +
  64.                    name.getConstantPoolIndex()
  65.         );
  66.     }
  67.  
  68.         dos.writeByte(getTag());
  69.         dos.writeShort(name.getConstantPoolIndex());
  70.     }
  71.  
  72.     /**
  73.      * <p> return the class represented by the CONSTANT_CLASS </p>
  74.      *
  75.      * @return the name of the class
  76.      */
  77.  
  78.     String getClassName() { return name.getString(); }
  79.  
  80.     /**
  81.      * <p> returns the Class object for the class represented by the constant. </p>
  82.      *
  83.      * @return The java.lang.Class object for the class.
  84.      */
  85.  
  86.     Class getClassObject() throws ClassNotFoundException {
  87.         return Class.forName(name.getString());
  88.     }
  89.  
  90.     /**
  91.      * <p> compare the object, by name or value. </p>
  92.      * 
  93.      * @param the object for comparison
  94.      * 
  95.      * @return object equality.
  96.      */
  97.  
  98.     public boolean equals(Object o) {
  99.         if (o == null) return false;
  100.  
  101.         if (o instanceof ClassConstant) {
  102.             ClassConstant cc = (ClassConstant)o;
  103.             return name.equals(cc.name);
  104.         }
  105.     
  106.     return false;
  107.     }
  108.  
  109.     /**
  110.      * @return a hashcode for the object.
  111.      */
  112.     public int hashCode() {
  113.     return name.getString().hashCode();    
  114.     }
  115. }
  116.