0
|
1 /*
|
|
2 * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 *
|
|
5 * This code is free software; you can redistribute it and/or modify it
|
|
6 * under the terms of the GNU General Public License version 2 only, as
|
|
7 * published by the Free Software Foundation.
|
|
8 *
|
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 * version 2 for more details (a copy is included in the LICENSE file that
|
|
13 * accompanied this code).
|
|
14 *
|
|
15 * You should have received a copy of the GNU General Public License version
|
|
16 * 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 *
|
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 * have any questions.
|
|
22 *
|
|
23 */
|
|
24
|
|
25 package sun.jvm.hotspot.utilities.soql;
|
|
26
|
|
27 import java.util.*;
|
|
28 import sun.jvm.hotspot.oops.*;
|
|
29 import sun.jvm.hotspot.utilities.*;
|
|
30
|
|
31 /**
|
|
32 This is JavaScript wrapper for InstanceKlass.
|
|
33 */
|
|
34 public class JSJavaInstanceKlass extends JSJavaKlass {
|
|
35 private static final int FIELD_SOURCE_FILE = 1;
|
|
36 private static final int FIELD_INTERFACES = 2;
|
|
37 private static final int FIELD_FIELDS = 3;
|
|
38 private static final int FIELD_METHODS = 4;
|
|
39 private static final int FIELD_IS_PRIVATE = 5;
|
|
40 private static final int FIELD_IS_PUBLIC = 6;
|
|
41 private static final int FIELD_IS_PROTECTED = 7;
|
|
42 private static final int FIELD_IS_PACKAGE_PRIVATE = 8;
|
|
43 private static final int FIELD_IS_STATIC = 9;
|
|
44 private static final int FIELD_IS_FINAL = 10;
|
|
45 private static final int FIELD_IS_ABSTRACT = 11;
|
|
46 private static final int FIELD_IS_STRICT = 12;
|
|
47 private static final int FIELD_IS_SYNTHETIC = 13;
|
|
48 private static final int FIELD_IS_INTERFACE = 14;
|
|
49 private static final int FIELD_CLASS_LOADER = 15;
|
|
50 private static final int FIELD_PROTECTION_DOMAIN = 16;
|
|
51 private static final int FIELD_SIGNERS = 17;
|
|
52 private static final int FIELD_STATICS = 18;
|
|
53 private static final int FIELD_UNDEFINED = -1;
|
|
54
|
|
55 public JSJavaInstanceKlass(InstanceKlass kls, JSJavaFactory fac) {
|
|
56 super(kls, fac);
|
|
57 this.instanceFields = new HashMap();
|
|
58 this.staticFields = new HashMap();
|
|
59 }
|
|
60
|
|
61 public final InstanceKlass getInstanceKlass() {
|
|
62 return (InstanceKlass) getKlass();
|
|
63 }
|
|
64
|
|
65 public Object getMetaClassFieldValue(String name) {
|
|
66 int fieldID = getFieldID(name);
|
|
67 InstanceKlass ik = getInstanceKlass();
|
|
68 switch (fieldID) {
|
|
69 case FIELD_SOURCE_FILE: {
|
|
70 Symbol sourceFile = ik.getSourceFileName();
|
|
71 return (sourceFile != null)? sourceFile.asString() : "<unknown>";
|
|
72 }
|
|
73 case FIELD_INTERFACES:
|
|
74 return getInterfaces();
|
|
75 case FIELD_FIELDS:
|
|
76 return factory.newJSList(ik.getImmediateFields());
|
|
77 case FIELD_METHODS:
|
|
78 return factory.newJSList(ik.getImmediateMethods());
|
|
79 case FIELD_IS_PRIVATE:
|
|
80 return Boolean.valueOf(getAccessFlags().isPrivate());
|
|
81 case FIELD_IS_PUBLIC:
|
|
82 return Boolean.valueOf(getAccessFlags().isPublic());
|
|
83 case FIELD_IS_PROTECTED:
|
|
84 return Boolean.valueOf(getAccessFlags().isProtected());
|
|
85 case FIELD_IS_PACKAGE_PRIVATE: {
|
|
86 AccessFlags acc = getAccessFlags();
|
|
87 return Boolean.valueOf(!acc.isPrivate() && !acc.isPublic() && !acc.isProtected());
|
|
88 }
|
|
89 case FIELD_IS_STATIC:
|
|
90 return Boolean.valueOf(getAccessFlags().isStatic());
|
|
91 case FIELD_IS_FINAL:
|
|
92 return Boolean.valueOf(getAccessFlags().isFinal());
|
|
93 case FIELD_IS_ABSTRACT:
|
|
94 return Boolean.valueOf(getAccessFlags().isAbstract());
|
|
95 case FIELD_IS_STRICT:
|
|
96 return Boolean.valueOf(getAccessFlags().isStrict());
|
|
97 case FIELD_IS_SYNTHETIC:
|
|
98 return Boolean.valueOf(getAccessFlags().isSynthetic());
|
|
99 case FIELD_IS_INTERFACE:
|
|
100 return Boolean.valueOf(ik.isInterface());
|
|
101 case FIELD_CLASS_LOADER:
|
|
102 return factory.newJSJavaObject(ik.getClassLoader());
|
|
103 case FIELD_PROTECTION_DOMAIN:
|
|
104 return factory.newJSJavaObject(ik.getProtectionDomain());
|
|
105 case FIELD_SIGNERS:
|
|
106 return factory.newJSJavaObject(ik.getSigners());
|
|
107 case FIELD_STATICS:
|
|
108 return getStatics();
|
|
109 case FIELD_UNDEFINED:
|
|
110 default:
|
|
111 return super.getMetaClassFieldValue(name);
|
|
112 }
|
|
113 }
|
|
114
|
|
115 public boolean hasMetaClassField(String name) {
|
|
116 if (getFieldID(name) != FIELD_UNDEFINED) {
|
|
117 return true;
|
|
118 } else {
|
|
119 return super.hasMetaClassField(name);
|
|
120 }
|
|
121 }
|
|
122
|
|
123 public String getName() {
|
|
124 return getInstanceKlass().getName().asString().replace('/', '.');
|
|
125 }
|
|
126
|
|
127 public boolean isArray() {
|
|
128 return false;
|
|
129 }
|
|
130
|
|
131 public String[] getMetaClassFieldNames() {
|
|
132 String[] superFields = super.getMetaClassFieldNames();
|
|
133 Set k = fields.keySet();
|
|
134 String[] res = new String[k.size() + superFields.length];
|
|
135 System.arraycopy(superFields, 0, res, 0, superFields.length);
|
|
136 int i = superFields.length;
|
|
137 for (Iterator itr = k.iterator(); itr.hasNext();) {
|
|
138 res[i] = (String) itr.next();
|
|
139 i++;
|
|
140 }
|
|
141 return res;
|
|
142 }
|
|
143
|
|
144 public Object getInstanceFieldValue(String name, Instance instance) throws NoSuchFieldException {
|
|
145 Field fld = findInstanceField(name);
|
|
146 if (fld != null) {
|
|
147 return getFieldValue(fld, name, instance);
|
|
148 } else {
|
|
149 throw new NoSuchFieldException(name + " is not field of "
|
|
150 + getInstanceKlass().getName().asString().replace('/', '.'));
|
|
151 }
|
|
152 }
|
|
153
|
|
154 public Object getStaticFieldValue(String name) throws NoSuchFieldException {
|
|
155 Field fld = findStaticField(name);
|
|
156 if (fld != null) {
|
|
157 return getFieldValue(fld, name, getInstanceKlass());
|
|
158 } else {
|
|
159 throw new NoSuchFieldException(name + " is not field of "
|
|
160 + getInstanceKlass().getName().asString().replace('/', '.'));
|
|
161 }
|
|
162 }
|
|
163
|
|
164 public String[] getInstanceFieldNames() {
|
|
165 if (instanceFieldNames == null) {
|
|
166 InstanceKlass current = getInstanceKlass();
|
|
167 while (current != null) {
|
|
168 List tmp = current.getImmediateFields();
|
|
169 for (Iterator itr = tmp.iterator(); itr.hasNext();) {
|
|
170 Field fld = (Field) itr.next();
|
|
171 if (!fld.isStatic()) {
|
|
172 String name = fld.getID().getName();
|
|
173 if (instanceFields.get(name) == null) {
|
|
174 instanceFields.put(name, fld);
|
|
175 }
|
|
176 }
|
|
177 }
|
|
178 current = (InstanceKlass) current.getSuper();
|
|
179 }
|
|
180
|
|
181 Set s = instanceFields.keySet();
|
|
182 instanceFieldNames = new String[s.size()];
|
|
183 int i = 0;
|
|
184 for (Iterator itr = s.iterator(); itr.hasNext(); i++) {
|
|
185 instanceFieldNames[i] = (String) itr.next();
|
|
186 }
|
|
187 }
|
|
188 return instanceFieldNames;
|
|
189 }
|
|
190
|
|
191 public boolean hasInstanceField(String name) {
|
|
192 Field fld = findInstanceField(name);
|
|
193 return (fld != null)? true: false;
|
|
194 }
|
|
195
|
|
196 public String[] getStaticFieldNames() {
|
|
197 if (staticFieldNames == null) {
|
|
198 InstanceKlass current = getInstanceKlass();
|
|
199 List tmp = current.getImmediateFields();
|
|
200 for (Iterator itr = tmp.iterator(); itr.hasNext();) {
|
|
201 Field fld = (Field) itr.next();
|
|
202 if (fld.isStatic()) {
|
|
203 staticFields.put(fld.getID().getName(), fld);
|
|
204 }
|
|
205 }
|
|
206
|
|
207 Set s = staticFields.keySet();
|
|
208 staticFieldNames = new String[s.size()];
|
|
209 int i = 0;
|
|
210 for (Iterator itr = s.iterator(); itr.hasNext(); i++) {
|
|
211 staticFieldNames[i] = (String) itr.next();
|
|
212 }
|
|
213 }
|
|
214 return staticFieldNames;
|
|
215 }
|
|
216
|
|
217 public boolean hasStaticField(String name) {
|
|
218 Field fld = findStaticField(name);
|
|
219 return (fld != null)? true: false;
|
|
220 }
|
|
221
|
|
222 //-- Intenals only below this point
|
|
223 private static Map fields = new HashMap();
|
|
224 private static void addField(String name, int fieldId) {
|
|
225 fields.put(name, new Integer(fieldId));
|
|
226 }
|
|
227
|
|
228 private static int getFieldID(String name) {
|
|
229 Integer res = (Integer) fields.get(name);
|
|
230 return (res != null)? res.intValue() : FIELD_UNDEFINED;
|
|
231 }
|
|
232
|
|
233 static {
|
|
234 addField("sourceFile", FIELD_SOURCE_FILE);
|
|
235 addField("interfaces", FIELD_INTERFACES);
|
|
236 addField("fields", FIELD_FIELDS);
|
|
237 addField("methods", FIELD_METHODS);
|
|
238 addField("isPrivate", FIELD_IS_PRIVATE);
|
|
239 addField("isPublic", FIELD_IS_PUBLIC);
|
|
240 addField("isProtected", FIELD_IS_PROTECTED);
|
|
241 addField("isPackagePrivate", FIELD_IS_PACKAGE_PRIVATE);
|
|
242 addField("isStatic", FIELD_IS_STATIC);
|
|
243 addField("isFinal", FIELD_IS_FINAL);
|
|
244 addField("isAbstract", FIELD_IS_ABSTRACT);
|
|
245 addField("isStrict", FIELD_IS_STRICT);
|
|
246 addField("isSynthetic", FIELD_IS_SYNTHETIC);
|
|
247 addField("isInterface", FIELD_IS_INTERFACE);
|
|
248 addField("classLoader", FIELD_CLASS_LOADER);
|
|
249 addField("protectionDomain", FIELD_PROTECTION_DOMAIN);
|
|
250 addField("signers", FIELD_SIGNERS);
|
|
251 addField("statics", FIELD_STATICS);
|
|
252 }
|
|
253
|
|
254 private AccessFlags getAccessFlags() {
|
|
255 if (accFlags == null) {
|
|
256 accFlags = new AccessFlags(getInstanceKlass().computeModifierFlags());
|
|
257 }
|
|
258 return accFlags;
|
|
259 }
|
|
260
|
|
261 private Object getFieldValue(Field fld, String name, Oop oop) {
|
|
262 FieldType fd = fld.getFieldType();
|
|
263 if (fd.isObject() || fd.isArray()) {
|
|
264 return factory.newJSJavaObject(((OopField)fld).getValue(oop));
|
|
265 } else if (fd.isByte()) {
|
|
266 return new Byte(((ByteField)fld).getValue(oop));
|
|
267 } else if (fd.isChar()) {
|
|
268 return new String(new char[] { ((CharField)fld).getValue(oop) });
|
|
269 } else if (fd.isDouble()) {
|
|
270 return new Double(((DoubleField)fld).getValue(oop));
|
|
271 } else if (fd.isFloat()) {
|
|
272 return new Float(((FloatField)fld).getValue(oop));
|
|
273 } else if (fd.isInt()) {
|
|
274 return new Integer(((IntField)fld).getValue(oop));
|
|
275 } else if (fd.isLong()) {
|
|
276 return new Long(((LongField)fld).getValue(oop));
|
|
277 } else if (fd.isShort()) {
|
|
278 return new Short(((ShortField)fld).getValue(oop));
|
|
279 } else if (fd.isBoolean()) {
|
|
280 return Boolean.valueOf(((BooleanField)fld).getValue(oop));
|
|
281 } else {
|
|
282 if (Assert.ASSERTS_ENABLED) {
|
|
283 Assert.that(false, "invalid field type for " + name);
|
|
284 }
|
|
285 return null;
|
|
286 }
|
|
287 }
|
|
288
|
|
289 private Field findInstanceField(String name) {
|
|
290 Field fld = (Field) instanceFields.get(name);
|
|
291 if (fld != null) {
|
|
292 return fld;
|
|
293 } else {
|
|
294 InstanceKlass current = getInstanceKlass();
|
|
295 while (current != null) {
|
|
296 List tmp = current.getImmediateFields();
|
|
297 for (Iterator itr = tmp.iterator(); itr.hasNext();) {
|
|
298 fld = (Field) itr.next();
|
|
299 if (fld.getID().getName().equals(name) && !fld.isStatic()) {
|
|
300 instanceFields.put(name, fld);
|
|
301 return fld;
|
|
302 }
|
|
303 }
|
|
304 // lookup in super class.
|
|
305 current = (InstanceKlass) current.getSuper();
|
|
306 }
|
|
307 }
|
|
308 // no match
|
|
309 return null;
|
|
310 }
|
|
311
|
|
312 private Field findStaticField(String name) {
|
|
313 Field fld = (Field) staticFields.get(name);
|
|
314 if (fld != null) {
|
|
315 return fld;
|
|
316 } else {
|
|
317 // static fields are searched only in current.
|
|
318 // Direct/indirect super classes and interfaces
|
|
319 // are not included in search.
|
|
320 InstanceKlass current = getInstanceKlass();
|
|
321 List tmp = current.getImmediateFields();
|
|
322 for (Iterator itr = tmp.iterator(); itr.hasNext();) {
|
|
323 fld = (Field) itr.next();
|
|
324 if (fld.getID().getName().equals(name) && fld.isStatic()) {
|
|
325 staticFields.put(name, fld);
|
|
326 return fld;
|
|
327 }
|
|
328 }
|
|
329 // no match
|
|
330 return null;
|
|
331 }
|
|
332 }
|
|
333
|
|
334 private JSList getInterfaces() {
|
|
335 InstanceKlass ik = getInstanceKlass();
|
|
336 List intfs = ik.getDirectImplementedInterfaces();
|
|
337 List res = new ArrayList(0);
|
|
338 for (Iterator itr = intfs.iterator(); itr.hasNext();) {
|
|
339 Klass k = (Klass) itr.next();
|
|
340 res.add(k.getJavaMirror());
|
|
341 }
|
|
342 return factory.newJSList(res);
|
|
343 }
|
|
344
|
|
345 private JSMap getStatics() {
|
|
346 String[] names = getStaticFieldNames();
|
|
347 Map map = new HashMap();
|
|
348 for (int i=0; i < names.length; i++) {
|
|
349 try {
|
|
350 map.put(names[i], getStaticFieldValue(names[i]));
|
|
351 } catch (NoSuchFieldException exp) {}
|
|
352 }
|
|
353 return factory.newJSMap(map);
|
|
354 }
|
|
355
|
|
356 private Map instanceFields;
|
|
357 private Map staticFields;
|
|
358 private String[] instanceFieldNames;
|
|
359 private String[] staticFieldNames;
|
|
360 private AccessFlags accFlags;
|
|
361 }
|