KickJava   Java API By Example, From Geeks To Geeks.

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


1 package polyglot.types.reflect;
2
3 import polyglot.types.*;
4 import java.util.*;
5 import java.io.*;
6
7 /**
8  * Field models a field (member variable) in a class. The Field class
9  * grants access to information such as the field's modifiers, its name
10  * and type descriptor (represented as indices into the constant pool),
11  * and any attributes of the field. Static fields have a ConstantValue
12  * attribute.
13  *
14  * @see polyglot.types.reflect ConstantValue
15  *
16  * @author Nate Nystrom
17  * (<a HREF="mailto:nystrom@cs.purdue.edu">nystrom@cs.purdue.edu</a>)
18  */

19 class Field {
20     ClassFile clazz;
21     int modifiers;
22     int name;
23     int type;
24     Attribute[] attrs;
25     ConstantValue constantValue;
26     boolean synthetic;
27
28     /**
29      * Return true of t is java.lang.String.
30      * We don't compare against ts.String() because ts.String() may not
31      * yet be set.
32      */

33     boolean isString(Type t) {
34       return t.isClass()
35           && t.toClass().isTopLevel()
36           && t.toClass().fullName().equals("java.lang.String");
37     }
38
39     FieldInstance fieldInstance(TypeSystem ts, ClassType ct) {
40       String JavaDoc name = (String JavaDoc) clazz.constants[this.name].value();
41       String JavaDoc type = (String JavaDoc) clazz.constants[this.type].value();
42
43       FieldInstance fi = ts.fieldInstance(ct.position(), ct,
44                                           ts.flagsForBits(modifiers),
45                                           clazz.typeForString(ts, type), name);
46
47       Constant c = constantValue();
48
49       if (c != null) {
50         Object JavaDoc o = null;
51
52         try {
53           switch (c.tag()) {
54             case Constant.STRING: o = getString(); break;
55             case Constant.INTEGER: o = new Integer JavaDoc(getInt()); break;
56             case Constant.LONG: o = new Long JavaDoc(getLong()); break;
57             case Constant.FLOAT: o = new Float JavaDoc(getFloat()); break;
58             case Constant.DOUBLE: o = new Double JavaDoc(getDouble()); break;
59           }
60         }
61         catch (SemanticException e) {
62           throw new ClassFormatError JavaDoc("Unexpected constant pool entry.");
63         }
64
65         if (o != null) {
66           return fi.constantValue(o);
67         }
68       }
69
70       return fi;
71     }
72
73     boolean isSynthetic() {
74       return synthetic;
75     }
76
77     Constant constantValue() {
78       if (this.constantValue != null) {
79         int index = this.constantValue.index;
80         return clazz.constants[index];
81       }
82
83       return null;
84     }
85
86     int getInt() throws SemanticException {
87       Constant c = constantValue();
88
89       if (c != null && c.tag() == Constant.INTEGER) {
90         Integer JavaDoc v = (Integer JavaDoc) c.value();
91         return v.intValue();
92       }
93
94       throw new SemanticException("Could not find expected constant " +
95                                   "pool entry with tag INTEGER.");
96     }
97
98     float getFloat() throws SemanticException {
99       Constant c = constantValue();
100
101       if (c != null && c.tag() == Constant.FLOAT) {
102         Float JavaDoc v = (Float JavaDoc) c.value();
103         return v.floatValue();
104       }
105
106       throw new SemanticException("Could not find expected constant " +
107                                   "pool entry with tag FLOAT.");
108     }
109
110     double getDouble() throws SemanticException {
111       Constant c = constantValue();
112
113       if (c != null && c.tag() == Constant.DOUBLE) {
114         Double JavaDoc v = (Double JavaDoc) c.value();
115         return v.doubleValue();
116       }
117
118       throw new SemanticException("Could not find expected constant " +
119                                   "pool entry with tag DOUBLE.");
120     }
121
122     long getLong() throws SemanticException {
123       Constant c = constantValue();
124
125       if (c != null && c.tag() == Constant.LONG) {
126         Long JavaDoc v = (Long JavaDoc) c.value();
127         return v.longValue();
128       }
129
130       throw new SemanticException("Could not find expected constant " +
131                                   "pool entry with tag LONG.");
132     }
133
134     String JavaDoc getString() throws SemanticException {
135       Constant c = constantValue();
136
137       if (c != null && c.tag() == Constant.STRING) {
138         Integer JavaDoc i = (Integer JavaDoc) c.value();
139         c = clazz.constants[i.intValue()];
140
141         if (c != null && c.tag() == Constant.UTF8) {
142           String JavaDoc v = (String JavaDoc) c.value();
143           return v;
144         }
145       }
146
147       throw new SemanticException("Could not find expected constant " +
148                                   "pool entry with tag STRING or UTF8.");
149     }
150
151     String JavaDoc name() {
152       return (String JavaDoc) clazz.constants[this.name].value();
153     }
154
155     /**
156      * Constructor. Read a field from a class file.
157      *
158      * @param in
159      * The data stream of the class file.
160      * @param clazz
161      * The class file containing the field.
162      * @exception IOException
163      * If an error occurs while reading.
164      */

165     Field(DataInputStream in, ClassFile clazz)
166         throws IOException
167     {
168         this.clazz = clazz;
169
170         modifiers = in.readUnsignedShort();
171
172     name = in.readUnsignedShort();
173     type = in.readUnsignedShort();
174
175     int numAttributes = in.readUnsignedShort();
176
177         attrs = new Attribute[numAttributes];
178
179         for (int i = 0; i < numAttributes; i++) {
180             int nameIndex = in.readUnsignedShort();
181             int length = in.readInt();
182
183             Constant name = clazz.constants[nameIndex];
184
185             if (name != null) {
186                 if ("ConstantValue".equals(name.value())) {
187                     constantValue = new ConstantValue(in, nameIndex, length);
188                     attrs[i] = constantValue;
189                 }
190                 if ("Synthetic".equals(name.value())) {
191                     synthetic = true;
192                 }
193             }
194
195         if (attrs[i] == null) {
196                 long n = in.skip(length);
197                 if (n != length) {
198                     throw new EOFException();
199                 }
200             }
201     }
202     }
203 }
204
Popular Tags