001package serp.bytecode.lowlevel;
002
003import java.io.*;
004
005/**
006 * Base class for field, method, and interface method constant pool
007 * entries. All complex entries reference the {@link ClassEntry} of the
008 * class that owns the entity and a {@link NameAndTypeEntry} describing
009 * the entity.
010 *
011 * @author Abe White
012 */
013public abstract class ComplexEntry extends Entry {
014    private int _classIndex = 0;
015    private int _nameAndTypeIndex = 0;
016
017    /**
018     * Default constructor.
019     */
020    public ComplexEntry() {
021    }
022
023    /**
024     * Constructor.
025     *
026     * @param classIndex the constant pool index of the
027     * {@link ClassEntry} describing the owner of this entity
028     * @param nameAndTypeIndex the constant pool index of the
029     * {@link NameAndTypeEntry} describing this entity
030     */
031    public ComplexEntry(int classIndex, int nameAndTypeIndex) {
032        _classIndex = classIndex;
033        _nameAndTypeIndex = nameAndTypeIndex;
034    }
035
036    /**
037     * Return the constant pool index of the {@link ClassEntry} describing
038     * the owning class of this entity. Defaults to 0.
039     */
040    public int getClassIndex() {
041        return _classIndex;
042    }
043
044    /**
045     * Set the constant pool index of the {@link ClassEntry} describing
046     * the owning class of this entity.
047     */
048    public void setClassIndex(int classIndex) {
049        Object key = beforeModify();
050        _classIndex = classIndex;
051        afterModify(key);
052    }
053
054    /**
055     * Return the referenced {@link ClassEntry}. This method can only
056     * be run for entries that have been added to a constant pool.
057     */
058    public ClassEntry getClassEntry() {
059        return (ClassEntry) getPool().getEntry(_classIndex);
060    }
061
062    /**
063     * Return the constant pool index of the {@link NameAndTypeEntry}
064     * describing this entity.
065     */
066    public int getNameAndTypeIndex() {
067        return _nameAndTypeIndex;
068    }
069
070    /**
071     * Set the constant pool index of the {@link NameAndTypeEntry}
072     * describing this entity.
073     */
074    public void setNameAndTypeIndex(int nameAndTypeIndex) {
075        Object key = beforeModify();
076        _nameAndTypeIndex = nameAndTypeIndex;
077        afterModify(key);
078    }
079
080    /**
081     * Return the referenced {@link NameAndTypeEntry}. This method can only
082     * be run for entries that have been added to a constant pool.
083     */
084    public NameAndTypeEntry getNameAndTypeEntry() {
085        return (NameAndTypeEntry) getPool().getEntry(_nameAndTypeIndex);
086    }
087
088    void readData(DataInput in) throws IOException {
089        _classIndex = in.readUnsignedShort();
090        _nameAndTypeIndex = in.readUnsignedShort();
091    }
092
093    void writeData(DataOutput out) throws IOException {
094        out.writeShort(_classIndex);
095        out.writeShort(_nameAndTypeIndex);
096    }
097}