KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > types > reflect > Constant


1 package polyglot.types.reflect;
2
3 import java.io.*;
4
5 /**
6  * A Constant is used to represent an item in the constant pool of a class.
7  *
8  * @author Nate Nystrom
9  * (<a HREF="mailto:nystrom@cs.purdue.edu">nystrom@cs.purdue.edu</a>)
10  */

11 class Constant
12 {
13     int tag;
14     Object JavaDoc value;
15
16     /**
17      * Constant tag for class types.
18      * This is used to reference other classes, such as the superclass,
19      * and is used by the checkcast and instanceof instructions.
20      * The Fieldref, Methodref and InterfaceMethodref constant types
21      * refer to this constant type.
22      */

23     static final byte CLASS = 7;
24
25     /**
26      * Constant tag for field references.
27      * This is used to reference a field in (possibly) another class.
28      * The getfield, putfield, getstatic, and putstatic instructions use
29      * this constant type.
30      */

31     static final byte FIELD_REF = 9;
32
33     /**
34      * Constant tag for method references.
35      * This is used to reference a method in (possibly) another class.
36      * The invokevirtual, invokespecial, and invokestatic instructions use
37      * this constant type.
38      */

39     static final byte METHOD_REF = 10;
40
41     /**
42      * Constant tag for java.lang.String constants.
43      * The actual string value is stored indirectly in a Utf8 constant.
44      */

45     static final byte STRING = 8;
46
47     /**
48      * Constant tag for int, short, byte, char, and boolean constants.
49      */

50     static final byte INTEGER = 3;
51
52     /**
53      * Constant tag for float constants.
54      */

55     static final byte FLOAT = 4;
56
57     /**
58      * Constant tag for long constants.
59      */

60     static final byte LONG = 5;
61
62     /**
63      * Constant tag for double constants.
64      */

65     static final byte DOUBLE = 6;
66
67     /**
68      * Constant tag for method references.
69      * This is used to reference a method in an interface.
70      * The invokeinterface instruction uses this constant type.
71      */

72     static final byte INTERFACE_METHOD_REF = 11;
73
74     /**
75      * Constant tag for holding the name and type of a field or method.
76      * The Fieldref, Methodref and InterfaceMethodref constant types
77      * refer to this constant type.
78      */

79     static final byte NAME_AND_TYPE = 12;
80
81     /**
82      * Constant tag for holding the a UTF8 format string.
83      * The string is used to hold the name and type descriptor for
84      * NameandType constants, the class name for Class constants,
85      * the string value for String constants.
86      */

87     static final byte UTF8 = 1;
88
89     /**
90      * @param tag
91      * The constant's tag.
92      * @param value
93      * The constant's value.
94      */

95     Constant(final int tag, final Object JavaDoc value)
96     {
97     this.tag = tag;
98     this.value = value;
99     }
100
101     /**
102      * Get the tag of the constant.
103      *
104      * @return
105      * The tag.
106      */

107     final int tag()
108     {
109     return tag;
110     }
111
112     /**
113      * Get the value of the constant.
114      *
115      * @return
116      * The value.
117      */

118     final Object JavaDoc value()
119     {
120     return value;
121     }
122
123     /**
124      * Hash the constant.
125      *
126      * @return
127      * The hash code.
128      */

129     public int hashCode()
130     {
131     switch (tag) {
132         case CLASS:
133         case STRING:
134         case INTEGER:
135         case FLOAT:
136         case LONG:
137         case DOUBLE:
138         case UTF8:
139         return tag ^ value.hashCode();
140         case FIELD_REF:
141         case METHOD_REF:
142         case INTERFACE_METHOD_REF:
143         case NAME_AND_TYPE:
144         return tag ^ ((int[]) value)[0] ^ ((int[]) value)[1];
145     }
146
147     return tag;
148     }
149
150     /**
151      * Check if an object is equal to this constant.
152      *
153      * @param obj
154      * The object to compare against.
155      * @return
156      * true if equal, false if not.
157      */

158     public boolean equals(Object JavaDoc other)
159     {
160     if (! (other instanceof Constant)) {
161         return false;
162     }
163
164     Constant c = (Constant) other;
165
166     if (tag != c.tag) {
167         return false;
168     }
169
170     switch (tag) {
171         case CLASS:
172         case STRING:
173         case INTEGER:
174         case FLOAT:
175         case LONG:
176         case DOUBLE:
177         case UTF8:
178         return value.equals(c.value);
179         case FIELD_REF:
180         case METHOD_REF:
181         case INTERFACE_METHOD_REF:
182         case NAME_AND_TYPE:
183         return ((int[]) value)[0] == ((int[]) c.value)[0] &&
184                ((int[]) value)[1] == ((int[]) c.value)[1];
185     }
186
187     return false;
188     }
189 }
190
Popular Tags