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