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