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

  1. /*
  2.  *
  3.  * @(#) ConstantPoolEntry.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.ConstantPoolEntry
  19.  * </p> 
  20.  *
  21.  * @version 1.0
  22.  * @author Laurence P. G. Cable
  23.  */
  24.  
  25. package sunw.demo.classfile;
  26.  
  27. import java.io.DataOutputStream;
  28. import java.io.IOException;
  29.  
  30. import sunw.demo.classfile.ClassFile;
  31.  
  32. /**
  33.  * <p>
  34.  * The ConstantPoolEntry is an abstract base class representing common 
  35.  * behaviors of specific subtypes, as defined below and in the VM spec.
  36.  * </p>
  37.  *
  38.  * <p>
  39.  * In particular this class handles, equality, sharing, output and indexing
  40.  * of all subtypes.
  41.  * </p>
  42.  */
  43.  
  44. abstract class ConstantPoolEntry {
  45.     
  46.     /*
  47.      * subtype tag values.
  48.      */
  49.     
  50.     final static byte CONSTANT_UTF8                 = 1;
  51.     final static byte CONSTANT_UNICODE              = 2;
  52.     final static byte CONSTANT_INTEGER              = 3;
  53.     final static byte CONSTANT_FLOAT                = 4;
  54.     final static byte CONSTANT_LONG              = 5;
  55.     final static byte CONSTANT_DOUBLE              = 6;
  56.     final static byte CONSTANT_CLASS                = 7;
  57.     final static byte CONSTANT_STRING              = 8;
  58.     final static byte CONSTANT_FIELDREF              = 9;
  59.     final static byte CONSTANT_METHODREF           = 10;
  60.     final static byte CONSTANT_INTERFACEMETHODREF = 11;
  61.     final static byte CONSTANT_NAMEANDTYPE        = 12;
  62.  
  63.     /*
  64.      * 
  65.      */
  66.     
  67.     private byte       tag;
  68.  
  69.     private ClassFile    classFile;
  70.  
  71.     private short    index = -1;
  72.  
  73.     /**
  74.      * <p> construct the CPE, set the type tag and class file </p>
  75.      */
  76.  
  77.     ConstantPoolEntry(byte t, ClassFile cf) {
  78.         tag         = t;
  79.         classFile    = cf;
  80.     }
  81.  
  82.     /*
  83.      * <p> construct the CPE, set the type tag and class file </p>
  84.      * This variant is used when the ClassFile is doing the "new"
  85.      * and already knows the intended index.
  86.      *
  87.      * @param s     the string
  88.      * @param cf    the class file
  89.      * @param index    the constant pool index.
  90.      */
  91.     ConstantPoolEntry(byte t, ClassFile cf, short index) {
  92.         tag         = t;
  93.         classFile    = cf;
  94.     this.index    = index;
  95.     }
  96.  
  97.     /**
  98.      *
  99.      */
  100.  
  101.     byte    getTag() { return tag; }
  102.  
  103.     /**
  104.      * @return the CPE's constant pool index.
  105.      */
  106.  
  107.     short getConstantPoolIndex() {
  108.         if (index == -1) index = classFile.addConstantPoolEntry(this);
  109.  
  110.         return (short)index;
  111.     }
  112.  
  113.     /**
  114.      * @return the Class File this CPE is contained within.
  115.      */
  116.  
  117.     ClassFile getClassFile() { return classFile; };
  118.  
  119.     /**
  120.      * <p> * write the CPE to the stream </p> 
  121.      *
  122.      * @throws IOException
  123.      */
  124.  
  125.     abstract void write(DataOutputStream dos) throws IOException;
  126.  
  127.     /**
  128.      * <p> test the CPE for equality </p>
  129.      *
  130.      * @return object's equality.
  131.      */
  132.  
  133.     public abstract boolean equals(Object o);
  134.  
  135.     /**
  136.      * @return a hashcode for the object.
  137.      */
  138.     abstract public int hashCode();
  139.  
  140.     /**
  141.      * <p> add the CPE into the Class File's constant pool </p>
  142.      */
  143.  
  144.     protected void addToConstantPool() {
  145.         if (index == -1) index = classFile.addConstantPoolEntry(this);
  146.     }
  147.  
  148.     /**
  149.      * @return are we in debug mode?
  150.      */
  151.  
  152.     protected static boolean debug() { return ClassFile.debug(); }
  153. }
  154.