jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/ConstantString.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 the abstract
    31  * This class is derived from the abstract {@link Constant}
    30  * <A HREF="com.sun.org.apache.bcel.internal.classfile.Constant.html">Constant</A> class
       
    31  * and represents a reference to a String object.
    32  * and represents a reference to a String object.
    32  *
    33  *
    33  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
    34  * @version $Id: ConstantString.java 1749603 2016-06-21 20:50:19Z ggregory $
    34  * @see     Constant
    35  * @see     Constant
    35  */
    36  */
    36 public final class ConstantString extends Constant implements ConstantObject {
    37 public final class ConstantString extends Constant implements ConstantObject {
    37   private int string_index; // Identical to ConstantClass except for this name
       
    38 
    38 
    39   /**
    39     private int string_index; // Identical to ConstantClass except for this name
    40    * Initialize from another object.
       
    41    */
       
    42   public ConstantString(ConstantString c) {
       
    43     this(c.getStringIndex());
       
    44   }
       
    45   /**
       
    46    * Initialize instance from file data.
       
    47    *
       
    48    * @param file Input stream
       
    49    * @throws IOException
       
    50    */
       
    51   ConstantString(DataInputStream file) throws IOException
       
    52   {
       
    53     this((int)file.readUnsignedShort());
       
    54   }
       
    55   /**
       
    56    * @param string_index Index of Constant_Utf8 in constant pool
       
    57    */
       
    58   public ConstantString(int string_index)
       
    59   {
       
    60     super(Constants.CONSTANT_String);
       
    61     this.string_index = string_index;
       
    62   }
       
    63   /**
       
    64    * Called by objects that are traversing the nodes of the tree implicitely
       
    65    * defined by the contents of a Java class. I.e., the hierarchy of methods,
       
    66    * fields, attributes, etc. spawns a tree of objects.
       
    67    *
       
    68    * @param v Visitor object
       
    69    */
       
    70   public void accept(Visitor v) {
       
    71     v.visitConstantString(this);
       
    72   }
       
    73   /**
       
    74    * Dump constant field reference to file stream in binary format.
       
    75    *
       
    76    * @param file Output file stream
       
    77    * @throws IOException
       
    78    */
       
    79   public final void dump(DataOutputStream file) throws IOException
       
    80   {
       
    81     file.writeByte(tag);
       
    82     file.writeShort(string_index);
       
    83   }
       
    84   /**
       
    85    * @return Index in constant pool of the string (ConstantUtf8).
       
    86    */
       
    87   public final int getStringIndex() { return string_index; }
       
    88   /**
       
    89    * @param string_index.
       
    90    */
       
    91   public final void setStringIndex(int string_index) {
       
    92     this.string_index = string_index;
       
    93   }
       
    94   /**
       
    95    * @return String representation.
       
    96    */
       
    97   public final String toString()
       
    98   {
       
    99     return super.toString() + "(string_index = " + string_index + ")";
       
   100   }
       
   101 
    40 
   102   /** @return String object
       
   103    */
       
   104   public Object getConstantValue(ConstantPool cp) {
       
   105     Constant c = cp.getConstant(string_index, Constants.CONSTANT_Utf8);
       
   106     return ((ConstantUtf8)c).getBytes();
       
   107   }
       
   108 
    41 
   109   /** @return dereferenced string
    42     /**
   110    */
    43      * Initialize from another object.
   111   public String getBytes(ConstantPool cp) {
    44      */
   112     return (String)getConstantValue(cp);
    45     public ConstantString(final ConstantString c) {
   113   }
    46         this(c.getStringIndex());
       
    47     }
       
    48 
       
    49 
       
    50     /**
       
    51      * Initialize instance from file data.
       
    52      *
       
    53      * @param file Input stream
       
    54      * @throws IOException
       
    55      */
       
    56     ConstantString(final DataInput file) throws IOException {
       
    57         this(file.readUnsignedShort());
       
    58     }
       
    59 
       
    60 
       
    61     /**
       
    62      * @param string_index Index of Constant_Utf8 in constant pool
       
    63      */
       
    64     public ConstantString(final int string_index) {
       
    65         super(Const.CONSTANT_String);
       
    66         this.string_index = string_index;
       
    67     }
       
    68 
       
    69 
       
    70     /**
       
    71      * Called by objects that are traversing the nodes of the tree implicitely
       
    72      * defined by the contents of a Java class. I.e., the hierarchy of methods,
       
    73      * fields, attributes, etc. spawns a tree of objects.
       
    74      *
       
    75      * @param v Visitor object
       
    76      */
       
    77     @Override
       
    78     public void accept( final Visitor v ) {
       
    79         v.visitConstantString(this);
       
    80     }
       
    81 
       
    82 
       
    83     /**
       
    84      * Dump constant field reference to file stream in binary format.
       
    85      *
       
    86      * @param file Output file stream
       
    87      * @throws IOException
       
    88      */
       
    89     @Override
       
    90     public final void dump( final DataOutputStream file ) throws IOException {
       
    91         file.writeByte(super.getTag());
       
    92         file.writeShort(string_index);
       
    93     }
       
    94 
       
    95 
       
    96     /**
       
    97      * @return Index in constant pool of the string (ConstantUtf8).
       
    98      */
       
    99     public final int getStringIndex() {
       
   100         return string_index;
       
   101     }
       
   102 
       
   103 
       
   104     /**
       
   105      * @param string_index the index into the constant of the string value
       
   106      */
       
   107     public final void setStringIndex( final int string_index ) {
       
   108         this.string_index = string_index;
       
   109     }
       
   110 
       
   111 
       
   112     /**
       
   113      * @return String representation.
       
   114      */
       
   115     @Override
       
   116     public final String toString() {
       
   117         return super.toString() + "(string_index = " + string_index + ")";
       
   118     }
       
   119 
       
   120 
       
   121     /** @return String object
       
   122      */
       
   123     @Override
       
   124     public Object getConstantValue( final ConstantPool cp ) {
       
   125         final Constant c = cp.getConstant(string_index, Const.CONSTANT_Utf8);
       
   126         return ((ConstantUtf8) c).getBytes();
       
   127     }
       
   128 
       
   129 
       
   130     /** @return dereferenced string
       
   131      */
       
   132     public String getBytes( final ConstantPool cp ) {
       
   133         return (String) getConstantValue(cp);
       
   134     }
   114 }
   135 }