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

  1. /*
  2.  *
  3.  * @(#) ConstantValue.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.ConstantValue
  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.ConstantPoolEntry;
  34. import sunw.demo.classfile.IntegerConstant;
  35. import sunw.demo.classfile.FloatConstant;
  36. import sunw.demo.classfile.DoubleConstant;
  37. import sunw.demo.classfile.LongConstant;
  38. import sunw.demo.classfile.StringConstant;
  39.  
  40. /**
  41.  * <p>
  42.  * This class provides Constant Pool support for all the simple constant
  43.  * value data types supported in the class file format.
  44.  * </p>
  45.  */
  46.  
  47. class ConstantValue extends Attribute {
  48.  
  49.     private ConstantPoolEntry constant;
  50.  
  51.     /**
  52.      * <p> construct an Attribute describing a Constant  </p>
  53.      *
  54.      * @param cf    the class file
  55.      * @param cpe     the cpe of the constant
  56.      */
  57.  
  58.     private ConstantValue(ClassFile cf, ConstantPoolEntry cpe) {
  59.         super(Attribute.CONSTANTVALUE, cf);
  60.         constant = cpe;
  61.     }
  62.  
  63.     /**
  64.      * <p> Integer Constant </p>
  65.      *
  66.      * @param cf    the class file
  67.      * @param ic    the Integer Constant
  68.      */
  69.  
  70.     ConstantValue(ClassFile cf, IntegerConstant ic) {
  71.         this(cf, (ConstantPoolEntry)ic);
  72.     }
  73.  
  74.     /**
  75.      * <p> Long Constant </p>
  76.      *
  77.      * @param cf    the class file
  78.      * @param lc    the Long Constant
  79.      */
  80.  
  81.     ConstantValue(ClassFile cf, LongConstant lc) {
  82.         this(cf, (ConstantPoolEntry)lc);
  83.     }
  84.  
  85.     /**
  86.      * <p> Float Constant </p>
  87.      *
  88.      * @param cf    the class file
  89.      * @param fc    the Float Constant
  90.      */
  91.  
  92.     ConstantValue(ClassFile cf, FloatConstant fc) {
  93.         this(cf, (ConstantPoolEntry)fc);
  94.     }
  95.  
  96.     /**
  97.      * <p> Double Constant </p>
  98.      *
  99.      * @param cf    the class file
  100.      * @param dc    the Double Constant
  101.      */
  102.  
  103.     ConstantValue(ClassFile cf, DoubleConstant dc) {
  104.         this(cf, (ConstantPoolEntry)dc);
  105.     }
  106.  
  107.     /**
  108.      * <p> String Constant </p>
  109.      *
  110.      * @param cf    the class file
  111.      * @param sc    the String Constant
  112.      */
  113.  
  114.     ConstantValue(ClassFile cf, StringConstant sc) {
  115.         this(cf, (ConstantPoolEntry)sc);
  116.     }
  117.  
  118.     /**
  119.      * @return the length of this ConstantValue Attribute (minus header)
  120.      */
  121.  
  122.     int getLength() { return 2; } 
  123.  
  124.     /**
  125.      * @return the CPE of the constant represented
  126.      */
  127.  
  128.     ConstantPoolEntry getConstant() { return constant; }
  129.  
  130.     /**
  131.      * @return the CPE type tag of the constant represented
  132.      */
  133.  
  134.     byte getConstantTag() { return constant.getTag(); }
  135.  
  136.     /**
  137.      *<p> write the Attribute to the stream </p>
  138.      *
  139.      * @param dos the output stream
  140.      *
  141.      * @throws IOException
  142.      */
  143.  
  144.     void write(DataOutputStream dos) throws IOException {
  145.         dos.writeShort(getNameConstantPoolIndex());
  146.         dos.writeInt(getLength());
  147.         dos.writeShort(constant.getConstantPoolIndex());
  148.     }
  149.  
  150.     /**
  151.      * @return the objects equality.
  152.      */
  153.  
  154.     public boolean equals(Object o) {
  155.         return constant.equals(o);
  156.     }
  157.  
  158.     /**
  159.      * @return a hashcode for the object.
  160.      */
  161.     public int hashCode() {
  162.     return constant.hashCode();    
  163.     }
  164. }
  165.