jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/ConstantValue.java
changeset 46174 5611d2529b49
parent 44797 8b3b3b911b8a
equal deleted inserted replaced
46173:5546b5710844 46174:5611d2529b49
    19  * limitations under the License.
    19  * limitations under the License.
    20  */
    20  */
    21 
    21 
    22 package com.sun.org.apache.bcel.internal.classfile;
    22 package com.sun.org.apache.bcel.internal.classfile;
    23 
    23 
       
    24 import java.io.DataInput;
       
    25 import java.io.DataOutputStream;
       
    26 import java.io.IOException;
    24 
    27 
    25 import  com.sun.org.apache.bcel.internal.Constants;
    28 import com.sun.org.apache.bcel.internal.Const;
    26 import  java.io.*;
       
    27 
    29 
    28 /**
    30 /**
    29  * This class is derived from <em>Attribute</em> and represents a constant
    31  * This class is derived from <em>Attribute</em> and represents a constant
    30  * value, i.e., a default value for initializing a class field.
    32  * value, i.e., a default value for initializing a class field.
    31  * This class is instantiated by the <em>Attribute.readAttribute()</em> method.
    33  * This class is instantiated by the <em>Attribute.readAttribute()</em> method.
    32  *
    34  *
    33  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
    35  * @version $Id: ConstantValue.java 1749603 2016-06-21 20:50:19Z ggregory $
    34  * @see     Attribute
    36  * @see     Attribute
    35  */
    37  */
    36 public final class ConstantValue extends Attribute {
    38 public final class ConstantValue extends Attribute {
    37   private int constantvalue_index;
       
    38 
    39 
    39   /**
    40     private int constantvalue_index;
    40    * Initialize from another object. Note that both objects use the same
       
    41    * references (shallow copy). Use clone() for a physical copy.
       
    42    */
       
    43   public ConstantValue(ConstantValue c) {
       
    44     this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(),
       
    45          c.getConstantPool());
       
    46   }
       
    47 
    41 
    48   /**
       
    49    * Construct object from file stream.
       
    50    * @param name_index Name index in constant pool
       
    51    * @param length Content length in bytes
       
    52    * @param file Input stream
       
    53    * @param constant_pool Array of constants
       
    54    * @throw IOException
       
    55    */
       
    56   ConstantValue(int name_index, int length, DataInputStream file,
       
    57                 ConstantPool constant_pool) throws IOException
       
    58   {
       
    59     this(name_index, length, (int)file.readUnsignedShort(), constant_pool);
       
    60   }
       
    61 
    42 
    62   /**
    43     /**
    63    * @param name_index Name index in constant pool
    44      * Initialize from another object. Note that both objects use the same
    64    * @param length Content length in bytes
    45      * references (shallow copy). Use clone() for a physical copy.
    65    * @param constantvalue_index Index in constant pool
    46      */
    66    * @param constant_pool Array of constants
    47     public ConstantValue(final ConstantValue c) {
    67    */
    48         this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
    68   public ConstantValue(int name_index, int length,
       
    69                        int constantvalue_index,
       
    70                        ConstantPool constant_pool)
       
    71   {
       
    72     super(Constants.ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
       
    73     this.constantvalue_index = constantvalue_index;
       
    74   }
       
    75 
       
    76   /**
       
    77    * Called by objects that are traversing the nodes of the tree implicitely
       
    78    * defined by the contents of a Java class. I.e., the hierarchy of methods,
       
    79    * fields, attributes, etc. spawns a tree of objects.
       
    80    *
       
    81    * @param v Visitor object
       
    82    */
       
    83   public void accept(Visitor v) {
       
    84     v.visitConstantValue(this);
       
    85   }
       
    86   /**
       
    87    * Dump constant value attribute to file stream on binary format.
       
    88    *
       
    89    * @param file Output file stream
       
    90    * @throws IOException
       
    91    */
       
    92   public final void dump(DataOutputStream file) throws IOException
       
    93   {
       
    94     super.dump(file);
       
    95     file.writeShort(constantvalue_index);
       
    96   }
       
    97   /**
       
    98    * @return Index in constant pool of constant value.
       
    99    */
       
   100   public final int getConstantValueIndex() { return constantvalue_index; }
       
   101 
       
   102   /**
       
   103    * @param constantvalue_index.
       
   104    */
       
   105   public final void setConstantValueIndex(int constantvalue_index) {
       
   106     this.constantvalue_index = constantvalue_index;
       
   107   }
       
   108 
       
   109   /**
       
   110    * @return String representation of constant value.
       
   111    */
       
   112   public final String toString() {
       
   113     Constant c = constant_pool.getConstant(constantvalue_index);
       
   114 
       
   115     String   buf;
       
   116     int    i;
       
   117 
       
   118     // Print constant to string depending on its type
       
   119     switch(c.getTag()) {
       
   120     case Constants.CONSTANT_Long:    buf = "" + ((ConstantLong)c).getBytes();    break;
       
   121     case Constants.CONSTANT_Float:   buf = "" + ((ConstantFloat)c).getBytes();   break;
       
   122     case Constants.CONSTANT_Double:  buf = "" + ((ConstantDouble)c).getBytes();  break;
       
   123     case Constants.CONSTANT_Integer: buf = "" + ((ConstantInteger)c).getBytes(); break;
       
   124     case Constants.CONSTANT_String:
       
   125       i   = ((ConstantString)c).getStringIndex();
       
   126       c   = constant_pool.getConstant(i, Constants.CONSTANT_Utf8);
       
   127       buf = "\"" + Utility.convertString(((ConstantUtf8)c).getBytes()) + "\"";
       
   128       break;
       
   129 
       
   130     default:
       
   131       throw new IllegalStateException("Type of ConstValue invalid: " + c);
       
   132     }
    49     }
   133 
    50 
   134     return buf;
       
   135   }
       
   136 
    51 
   137   /**
    52     /**
   138    * @return deep copy of this attribute
    53      * Construct object from input stream.
   139    */
    54      * @param name_index Name index in constant pool
   140   public Attribute copy(ConstantPool constant_pool) {
    55      * @param length Content length in bytes
   141     ConstantValue c = (ConstantValue)clone();
    56      * @param input Input stream
   142     c.constant_pool = constant_pool;
    57      * @param constant_pool Array of constants
   143     return c;
    58      * @throws IOException
   144   }
    59      */
       
    60     ConstantValue(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
       
    61             throws IOException {
       
    62         this(name_index, length, input.readUnsignedShort(), constant_pool);
       
    63     }
       
    64 
       
    65 
       
    66     /**
       
    67      * @param name_index Name index in constant pool
       
    68      * @param length Content length in bytes
       
    69      * @param constantvalue_index Index in constant pool
       
    70      * @param constant_pool Array of constants
       
    71      */
       
    72     public ConstantValue(final int name_index, final int length, final int constantvalue_index,
       
    73             final ConstantPool constant_pool) {
       
    74         super(Const.ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
       
    75         this.constantvalue_index = constantvalue_index;
       
    76     }
       
    77 
       
    78 
       
    79     /**
       
    80      * Called by objects that are traversing the nodes of the tree implicitely
       
    81      * defined by the contents of a Java class. I.e., the hierarchy of methods,
       
    82      * fields, attributes, etc. spawns a tree of objects.
       
    83      *
       
    84      * @param v Visitor object
       
    85      */
       
    86     @Override
       
    87     public void accept( final Visitor v ) {
       
    88         v.visitConstantValue(this);
       
    89     }
       
    90 
       
    91 
       
    92     /**
       
    93      * Dump constant value attribute to file stream on binary format.
       
    94      *
       
    95      * @param file Output file stream
       
    96      * @throws IOException
       
    97      */
       
    98     @Override
       
    99     public final void dump( final DataOutputStream file ) throws IOException {
       
   100         super.dump(file);
       
   101         file.writeShort(constantvalue_index);
       
   102     }
       
   103 
       
   104 
       
   105     /**
       
   106      * @return Index in constant pool of constant value.
       
   107      */
       
   108     public final int getConstantValueIndex() {
       
   109         return constantvalue_index;
       
   110     }
       
   111 
       
   112 
       
   113     /**
       
   114      * @param constantvalue_index the index info the constant pool of this constant value
       
   115      */
       
   116     public final void setConstantValueIndex( final int constantvalue_index ) {
       
   117         this.constantvalue_index = constantvalue_index;
       
   118     }
       
   119 
       
   120 
       
   121     /**
       
   122      * @return String representation of constant value.
       
   123      */
       
   124     @Override
       
   125     public final String toString() {
       
   126         Constant c = super.getConstantPool().getConstant(constantvalue_index);
       
   127         String buf;
       
   128         int i;
       
   129         // Print constant to string depending on its type
       
   130         switch (c.getTag()) {
       
   131             case Const.CONSTANT_Long:
       
   132                 buf = String.valueOf(((ConstantLong) c).getBytes());
       
   133                 break;
       
   134             case Const.CONSTANT_Float:
       
   135                 buf = String.valueOf(((ConstantFloat) c).getBytes());
       
   136                 break;
       
   137             case Const.CONSTANT_Double:
       
   138                 buf = String.valueOf(((ConstantDouble) c).getBytes());
       
   139                 break;
       
   140             case Const.CONSTANT_Integer:
       
   141                 buf = String.valueOf(((ConstantInteger) c).getBytes());
       
   142                 break;
       
   143             case Const.CONSTANT_String:
       
   144                 i = ((ConstantString) c).getStringIndex();
       
   145                 c = super.getConstantPool().getConstant(i, Const.CONSTANT_Utf8);
       
   146                 buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
       
   147                 break;
       
   148             default:
       
   149                 throw new IllegalStateException("Type of ConstValue invalid: " + c);
       
   150         }
       
   151         return buf;
       
   152     }
       
   153 
       
   154 
       
   155     /**
       
   156      * @return deep copy of this attribute
       
   157      */
       
   158     @Override
       
   159     public Attribute copy( final ConstantPool _constant_pool ) {
       
   160         final ConstantValue c = (ConstantValue) clone();
       
   161         c.setConstantPool(_constant_pool);
       
   162         return c;
       
   163     }
   145 }
   164 }