001    package serp.bytecode.lowlevel;
002    
003    import java.io.*;
004    
005    import serp.bytecode.visitor.*;
006    
007    /**
008     * A constant pool entry describing a class.
009     * Class entries are used to refer to the current class, the superclass,
010     * implemented interfaces, etc. Each class entry contains the constant pool
011     * index of the {@link UTF8Entry} that stores the class name, which is
012     * represented in internal form.
013     *
014     * @author Abe White
015     */
016    public class ClassEntry extends Entry implements ConstantEntry {
017        private int _nameIndex = 0;
018    
019        /**
020         * Default constructor.
021         */
022        public ClassEntry() {
023        }
024    
025        /**
026         * Constructor.
027         *
028         * @param nameIndex the constant pool index of the {@link UTF8Entry}
029         * containing the class name
030         */
031        public ClassEntry(int nameIndex) {
032            _nameIndex = nameIndex;
033        }
034    
035        /**
036         * Return the constant pool index of the {@link UTF8Entry}
037         * containing the class name. Defaults to 0.
038         */
039        public int getNameIndex() {
040            return _nameIndex;
041        }
042    
043        /**
044         * Set the constant pool index of the {@link UTF8Entry}
045         * containing the class name.
046         */
047        public void setNameIndex(int nameIndex) {
048            Object key = beforeModify();
049            _nameIndex = nameIndex;
050            afterModify(key);
051        }
052    
053        /**
054         * Return the referenced {@link UTF8Entry}. This method can only
055         * be run for entries that have been added to a constant pool.
056         */
057        public UTF8Entry getNameEntry() {
058            return (UTF8Entry) getPool().getEntry(_nameIndex);
059        }
060    
061        public int getType() {
062            return Entry.CLASS;
063        }
064    
065        public Object getConstant() {
066            return getNameEntry().getValue();
067        }
068    
069        public void setConstant(Object value) {
070            getNameEntry().setConstant(value);
071        }
072    
073        public void acceptVisit(BCVisitor visit) {
074            visit.enterClassEntry(this);
075            visit.exitClassEntry(this);
076        }
077    
078        void readData(DataInput in) throws IOException {
079            _nameIndex = in.readUnsignedShort();
080        }
081    
082        void writeData(DataOutput out) throws IOException {
083            out.writeShort(_nameIndex);
084        }
085    }