View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class is derived from the abstract {@link Constant}
28   * and represents a reference to a String object.
29   *
30   * @see     Constant
31   */
32  public final class ConstantString extends Constant implements ConstantObject {
33  
34      private int stringIndex; // Identical to ConstantClass except for this name
35  
36  
37      /**
38       * Initialize from another object.
39       */
40      public ConstantStringstantString.html#ConstantString">ConstantString(final ConstantString c) {
41          this(c.getStringIndex());
42      }
43  
44  
45      /**
46       * Initialize instance from file data.
47       *
48       * @param file Input stream
49       * @throws IOException
50       */
51      ConstantString(final DataInput file) throws IOException {
52          this(file.readUnsignedShort());
53      }
54  
55  
56      /**
57       * @param stringIndex Index of Constant_Utf8 in constant pool
58       */
59      public ConstantString(final int stringIndex) {
60          super(Const.CONSTANT_String);
61          this.stringIndex = stringIndex;
62      }
63  
64  
65      /**
66       * Called by objects that are traversing the nodes of the tree implicitely
67       * defined by the contents of a Java class. I.e., the hierarchy of methods,
68       * fields, attributes, etc. spawns a tree of objects.
69       *
70       * @param v Visitor object
71       */
72      @Override
73      public void accept( final Visitor v ) {
74          v.visitConstantString(this);
75      }
76  
77  
78      /**
79       * Dump constant field reference to file stream in binary format.
80       *
81       * @param file Output file stream
82       * @throws IOException
83       */
84      @Override
85      public void dump( final DataOutputStream file ) throws IOException {
86          file.writeByte(super.getTag());
87          file.writeShort(stringIndex);
88      }
89  
90  
91      /**
92       * @return Index in constant pool of the string (ConstantUtf8).
93       */
94      public int getStringIndex() {
95          return stringIndex;
96      }
97  
98  
99      /**
100      * @param stringIndex the index into the constant of the string value
101      */
102     public void setStringIndex( final int stringIndex ) {
103         this.stringIndex = stringIndex;
104     }
105 
106 
107     /**
108      * @return String representation.
109      */
110     @Override
111     public String toString() {
112         return super.toString() + "(stringIndex = " + stringIndex + ")";
113     }
114 
115 
116     /** @return String object
117      */
118     @Override
119     public Object getConstantValue( final ConstantPool cp ) {
120         final Constant c = cp.getConstant(stringIndex, Const.CONSTANT_Utf8);
121         return ((ConstantUtf8) c).getBytes();
122     }
123 
124 
125     /** @return dereferenced string
126      */
127     public String getBytes( final ConstantPool cp ) {
128         return (String) getConstantValue(cp);
129     }
130 }