001package serp.bytecode.lowlevel;
002
003import java.io.*;
004
005import serp.bytecode.visitor.*;
006
007/**
008 * A String constant in the constant pool. String constants
009 * hold a reference to a {@link UTF8Entry} that stores the actual value.
010 *
011 * @author Abe White
012 */
013public class StringEntry extends Entry implements ConstantEntry {
014    private int _stringIndex = -1;
015
016    /**
017     * Default constructor.
018     */
019    public StringEntry() {
020    }
021
022    /**
023     * Constructor.
024     *
025     * @param stringIndex the constant pool index of the {@link UTF8Entry}
026     * containing the value of this string
027     */
028    public StringEntry(int stringIndex) {
029        _stringIndex = stringIndex;
030    }
031
032    public int getType() {
033        return Entry.STRING;
034    }
035
036    /**
037     * Return the constant pool index of the {@link UTF8Entry}
038     * storing the value of this string.
039     */
040    public int getStringIndex() {
041        return _stringIndex;
042    }
043
044    /**
045     * Set the constant pool index of the {@link UTF8Entry}
046     * storing the value of this string.
047     */
048    public void setStringIndex(int stringIndex) {
049        Object key = beforeModify();
050        _stringIndex = stringIndex;
051        afterModify(key);
052    }
053
054    /**
055     * Return the referenced {@link UTF8Entry}. This method can only
056     * be run for entries that have been added to a constant pool.
057     */
058    public UTF8Entry getStringEntry() {
059        return (UTF8Entry) getPool().getEntry(_stringIndex);
060    }
061
062    public Object getConstant() {
063        return getStringEntry().getValue();
064    }
065
066    public void setConstant(Object value) {
067        getStringEntry().setConstant(value);
068    }
069
070    public void acceptVisit(BCVisitor visit) {
071        visit.enterStringEntry(this);
072        visit.exitStringEntry(this);
073    }
074
075    void readData(DataInput in) throws IOException {
076        _stringIndex = in.readUnsignedShort();
077    }
078
079    void writeData(DataOutput out) throws IOException {
080        out.writeShort(_stringIndex);
081    }
082}