    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 */
    018package org.apache.bcel.classfile;
    019
    020import java.io.DataInput;
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023
    024import org.apache.bcel.Const;
    025
    026/**
    027 * This class is derived from the abstract {@link Constant}
    028 * 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    /**
    038     * Initialize from another object.
    039     */
    040    public ConstantString(final ConstantString c) {
    041        this(c.getStringIndex());
    042    }
    043
    044
    045    /**
    046     * Initialize instance from file data.
    047     *
    048     * @param file Input stream
    049     * @throws IOException
    050     */
    051    ConstantString(final DataInput file) throws IOException {
    052        this(file.readUnsignedShort());
    053    }
    054
    055
    056    /**
    057     * @param stringIndex Index of Constant_Utf8 in constant pool
    058     */
    059    public ConstantString(final int stringIndex) {
    060        super(Const.CONSTANT_String);
    061        this.stringIndex = stringIndex;
    062    }
    063
    064
    065    /**
    066     * Called by objects that are traversing the nodes of the tree implicitely
    067     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    068     * fields, attributes, etc. spawns a tree of objects.
    069     *
    070     * @param v Visitor object
    071     */
    072    @Override
    073    public void accept( final Visitor v ) {
    074        v.visitConstantString(this);
    075    }
    076
    077
    078    /**
    079     * Dump constant field reference to file stream in binary format.
    080     *
    081     * @param file Output file stream
    082     * @throws IOException
    083     */
    084    @Override
    085    public void dump( final DataOutputStream file ) throws IOException {
    086        file.writeByte(super.getTag());
    087        file.writeShort(stringIndex);
    088    }
    089
    090
    091    /**
    092     * @return Index in constant pool of the string (ConstantUtf8).
    093     */
    094    public int getStringIndex() {
    095        return stringIndex;
    096    }
    097
    098
    099    /**
    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}