View Javadoc

1   package serp.bytecode.lowlevel;
2   
3   import java.io.*;
4   
5   import serp.bytecode.visitor.*;
6   
7   /***
8    * Entry containing indexes referencing a name and a descriptor. Used
9    * to describe fields and methods of other classes referenced by opcodes.
10   *
11   * @author Abe White
12   */
13  public class NameAndTypeEntry extends Entry {
14      private int _nameIndex = 0;
15      private int _descriptorIndex = 0;
16  
17      /***
18       * Default constructor.
19       */
20      public NameAndTypeEntry() {
21      }
22  
23      /***
24       * Constructor.
25       *
26       * @param nameIndex the constant pool index of the
27       * {@link UTF8Entry} containing the name of this entity
28       * @param descriptorIndex the constant pool index of the
29       * {@link UTF8Entry} containing the descriptor for this entity
30       */
31      public NameAndTypeEntry(int nameIndex, int descriptorIndex) {
32          _nameIndex = nameIndex;
33          _descriptorIndex = descriptorIndex;
34      }
35  
36      public int getType() {
37          return Entry.NAMEANDTYPE;
38      }
39  
40      /***
41       * Return the constant pool index of the {@link UTF8Entry}
42       * containing the name of this entity.
43       */
44      public int getNameIndex() {
45          return _nameIndex;
46      }
47  
48      /***
49       * Set the constant pool index of the {@link UTF8Entry}
50       * containing the name of this entity.
51       */
52      public void setNameIndex(int nameIndex) {
53          Object key = beforeModify();
54          _nameIndex = nameIndex;
55          afterModify(key);
56      }
57  
58      /***
59       * Return the name's referenced {@link UTF8Entry}. This method can only
60       * be run for entries that have been added to a constant pool.
61       */
62      public UTF8Entry getNameEntry() {
63          return (UTF8Entry) getPool().getEntry(_nameIndex);
64      }
65  
66      /***
67       * Return the constant pool index of the {@link UTF8Entry}
68       * containing the descriptor for this entity.
69       */
70      public int getDescriptorIndex() {
71          return _descriptorIndex;
72      }
73  
74      /***
75       * Set the constant pool index of a {@link UTF8Entry}
76       * containing the descriptor for this entity.
77       */
78      public void setDescriptorIndex(int descriptorIndex) {
79          Object key = beforeModify();
80          _descriptorIndex = descriptorIndex;
81          afterModify(key);
82      }
83  
84      /***
85       * Return the descriptor's referenced {@link UTF8Entry}. This method
86       * can only be run for entries that have been added to a constant pool.
87       */
88      public UTF8Entry getDescriptorEntry() {
89          return (UTF8Entry) getPool().getEntry(_descriptorIndex);
90      }
91  
92      public void acceptVisit(BCVisitor visit) {
93          visit.enterNameAndTypeEntry(this);
94          visit.exitNameAndTypeEntry(this);
95      }
96  
97      void readData(DataInput in) throws IOException {
98          _nameIndex = in.readUnsignedShort();
99          _descriptorIndex = in.readUnsignedShort();
100     }
101 
102     void writeData(DataOutput out) throws IOException {
103         out.writeShort(_nameIndex);
104         out.writeShort(_descriptorIndex);
105     }
106 }