001    package serp.bytecode.lowlevel;
002    
003    import java.io.*;
004    
005    import serp.bytecode.visitor.*;
006    
007    /**
008     * Entry containing indexes referencing a name and a descriptor. Used
009     * to describe fields and methods of other classes referenced by opcodes.
010     *
011     * @author Abe White
012     */
013    public class NameAndTypeEntry extends Entry {
014        private int _nameIndex = 0;
015        private int _descriptorIndex = 0;
016    
017        /**
018         * Default constructor.
019         */
020        public NameAndTypeEntry() {
021        }
022    
023        /**
024         * Constructor.
025         *
026         * @param nameIndex the constant pool index of the
027         * {@link UTF8Entry} containing the name of this entity
028         * @param descriptorIndex the constant pool index of the
029         * {@link UTF8Entry} containing the descriptor for this entity
030         */
031        public NameAndTypeEntry(int nameIndex, int descriptorIndex) {
032            _nameIndex = nameIndex;
033            _descriptorIndex = descriptorIndex;
034        }
035    
036        public int getType() {
037            return Entry.NAMEANDTYPE;
038        }
039    
040        /**
041         * Return the constant pool index of the {@link UTF8Entry}
042         * containing the name of this entity.
043         */
044        public int getNameIndex() {
045            return _nameIndex;
046        }
047    
048        /**
049         * Set the constant pool index of the {@link UTF8Entry}
050         * containing the name of this entity.
051         */
052        public void setNameIndex(int nameIndex) {
053            Object key = beforeModify();
054            _nameIndex = nameIndex;
055            afterModify(key);
056        }
057    
058        /**
059         * Return the name's referenced {@link UTF8Entry}. This method can only
060         * be run for entries that have been added to a constant pool.
061         */
062        public UTF8Entry getNameEntry() {
063            return (UTF8Entry) getPool().getEntry(_nameIndex);
064        }
065    
066        /**
067         * Return the constant pool index of the {@link UTF8Entry}
068         * containing the descriptor for this entity.
069         */
070        public int getDescriptorIndex() {
071            return _descriptorIndex;
072        }
073    
074        /**
075         * Set the constant pool index of a {@link UTF8Entry}
076         * containing the descriptor for this entity.
077         */
078        public void setDescriptorIndex(int descriptorIndex) {
079            Object key = beforeModify();
080            _descriptorIndex = descriptorIndex;
081            afterModify(key);
082        }
083    
084        /**
085         * Return the descriptor's referenced {@link UTF8Entry}. This method
086         * can only be run for entries that have been added to a constant pool.
087         */
088        public UTF8Entry getDescriptorEntry() {
089            return (UTF8Entry) getPool().getEntry(_descriptorIndex);
090        }
091    
092        public void acceptVisit(BCVisitor visit) {
093            visit.enterNameAndTypeEntry(this);
094            visit.exitNameAndTypeEntry(this);
095        }
096    
097        void readData(DataInput in) throws IOException {
098            _nameIndex = in.readUnsignedShort();
099            _descriptorIndex = in.readUnsignedShort();
100        }
101    
102        void writeData(DataOutput out) throws IOException {
103            out.writeShort(_nameIndex);
104            out.writeShort(_descriptorIndex);
105        }
106    }