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