1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 
  25 import  com.sun.org.apache.bcel.internal.Constants;
  26 import  java.io.*;
  27 
  28 /**
  29  * This class is derived from the abstract
  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  *
  33  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  34  * @see     Constant
  35  */
  36 public final class ConstantString extends Constant implements ConstantObject {
  37   private int string_index; // Identical to ConstantClass except for this name
  38 
  39   /**
  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 
 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 
 109   /** @return dereferenced string
 110    */
 111   public String getBytes(ConstantPool cp) {
 112     return (String)getConstantValue(cp);
 113   }
 114 }