1 package serp.bytecode.lowlevel;
2
3 import java.io.*;
4
5 import serp.bytecode.visitor.*;
6
7 /***
8 * A String constant in the constant pool. String constants
9 * hold a reference to a {@link UTF8Entry} that stores the actual value.
10 *
11 * @author Abe White
12 */
13 public class StringEntry extends Entry implements ConstantEntry {
14 private int _stringIndex = -1;
15
16 /***
17 * Default constructor.
18 */
19 public StringEntry() {
20 }
21
22 /***
23 * Constructor.
24 *
25 * @param stringIndex the constant pool index of the {@link UTF8Entry}
26 * containing the value of this string
27 */
28 public StringEntry(int stringIndex) {
29 _stringIndex = stringIndex;
30 }
31
32 public int getType() {
33 return Entry.STRING;
34 }
35
36 /***
37 * Return the constant pool index of the {@link UTF8Entry}
38 * storing the value of this string.
39 */
40 public int getStringIndex() {
41 return _stringIndex;
42 }
43
44 /***
45 * Set the constant pool index of the {@link UTF8Entry}
46 * storing the value of this string.
47 */
48 public void setStringIndex(int stringIndex) {
49 Object key = beforeModify();
50 _stringIndex = stringIndex;
51 afterModify(key);
52 }
53
54 /***
55 * Return the referenced {@link UTF8Entry}. This method can only
56 * be run for entries that have been added to a constant pool.
57 */
58 public UTF8Entry getStringEntry() {
59 return (UTF8Entry) getPool().getEntry(_stringIndex);
60 }
61
62 public Object getConstant() {
63 return getStringEntry().getValue();
64 }
65
66 public void setConstant(Object value) {
67 getStringEntry().setConstant(value);
68 }
69
70 public void acceptVisit(BCVisitor visit) {
71 visit.enterStringEntry(this);
72 visit.exitStringEntry(this);
73 }
74
75 void readData(DataInput in) throws IOException {
76 _stringIndex = in.readUnsignedShort();
77 }
78
79 void writeData(DataOutput out) throws IOException {
80 out.writeShort(_stringIndex);
81 }
82 }