001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018 package org.apache.bcel.classfile;
019
020 import java.io.DataInput;
021 import java.io.DataOutputStream;
022 import java.io.IOException;
023
024 import org.apache.bcel.Constants;
025
026 /**
027 * This class is derived from the abstract
028 * <A HREF="org.apache.bcel.classfile.Constant.html">Constant</A> class
029 * and represents a reference to a String object.
030 *
031 * @version $Id: ConstantString.java 1152072 2011-07-29 01:54:05Z dbrosius $
032 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
033 * @see Constant
034 */
035 public final class ConstantString extends Constant implements ConstantObject {
036
037 private static final long serialVersionUID = 6603144389219397225L;
038 private int string_index; // Identical to ConstantClass except for this name
039
040
041 /**
042 * Initialize from another object.
043 */
044 public ConstantString(ConstantString c) {
045 this(c.getStringIndex());
046 }
047
048
049 /**
050 * Initialize instance from file data.
051 *
052 * @param file Input stream
053 * @throws IOException
054 */
055 ConstantString(DataInput file) throws IOException {
056 this(file.readUnsignedShort());
057 }
058
059
060 /**
061 * @param string_index Index of Constant_Utf8 in constant pool
062 */
063 public ConstantString(int string_index) {
064 super(Constants.CONSTANT_String);
065 this.string_index = string_index;
066 }
067
068
069 /**
070 * Called by objects that are traversing the nodes of the tree implicitely
071 * defined by the contents of a Java class. I.e., the hierarchy of methods,
072 * fields, attributes, etc. spawns a tree of objects.
073 *
074 * @param v Visitor object
075 */
076 @Override
077 public void accept( Visitor v ) {
078 v.visitConstantString(this);
079 }
080
081
082 /**
083 * Dump constant field reference to file stream in binary format.
084 *
085 * @param file Output file stream
086 * @throws IOException
087 */
088 @Override
089 public final void dump( DataOutputStream file ) throws IOException {
090 file.writeByte(tag);
091 file.writeShort(string_index);
092 }
093
094
095 /**
096 * @return Index in constant pool of the string (ConstantUtf8).
097 */
098 public final int getStringIndex() {
099 return string_index;
100 }
101
102
103 /**
104 * @param string_index the index into the constant of the string value
105 */
106 public final void setStringIndex( int string_index ) {
107 this.string_index = string_index;
108 }
109
110
111 /**
112 * @return String representation.
113 */
114 @Override
115 public final String toString() {
116 return super.toString() + "(string_index = " + string_index + ")";
117 }
118
119
120 /** @return String object
121 */
122 public Object getConstantValue( ConstantPool cp ) {
123 Constant c = cp.getConstant(string_index, Constants.CONSTANT_Utf8);
124 return ((ConstantUtf8) c).getBytes();
125 }
126
127
128 /** @return dereferenced string
129 */
130 public String getBytes( ConstantPool cp ) {
131 return (String) getConstantValue(cp);
132 }
133 }