author | coleenp |
Wed, 22 May 2013 14:37:49 -0400 | |
changeset 17826 | 9ad5cd464a75 |
parent 13728 | 882756847a04 |
child 22234 | da823d78ad65 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
13728
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
21 |
* questions. |
1 | 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_STATICS = 18; |
|
51 |
private static final int FIELD_UNDEFINED = -1; |
|
52 |
||
53 |
public JSJavaInstanceKlass(InstanceKlass kls, JSJavaFactory fac) { |
|
54 |
super(kls, fac); |
|
55 |
this.instanceFields = new HashMap(); |
|
56 |
this.staticFields = new HashMap(); |
|
57 |
} |
|
58 |
||
59 |
public final InstanceKlass getInstanceKlass() { |
|
60 |
return (InstanceKlass) getKlass(); |
|
61 |
} |
|
62 |
||
63 |
public Object getMetaClassFieldValue(String name) { |
|
64 |
int fieldID = getFieldID(name); |
|
65 |
InstanceKlass ik = getInstanceKlass(); |
|
66 |
switch (fieldID) { |
|
67 |
case FIELD_SOURCE_FILE: { |
|
68 |
Symbol sourceFile = ik.getSourceFileName(); |
|
69 |
return (sourceFile != null)? sourceFile.asString() : "<unknown>"; |
|
70 |
} |
|
71 |
case FIELD_INTERFACES: |
|
72 |
return getInterfaces(); |
|
73 |
case FIELD_FIELDS: |
|
74 |
return factory.newJSList(ik.getImmediateFields()); |
|
75 |
case FIELD_METHODS: |
|
76 |
return factory.newJSList(ik.getImmediateMethods()); |
|
77 |
case FIELD_IS_PRIVATE: |
|
78 |
return Boolean.valueOf(getAccessFlags().isPrivate()); |
|
79 |
case FIELD_IS_PUBLIC: |
|
80 |
return Boolean.valueOf(getAccessFlags().isPublic()); |
|
81 |
case FIELD_IS_PROTECTED: |
|
82 |
return Boolean.valueOf(getAccessFlags().isProtected()); |
|
83 |
case FIELD_IS_PACKAGE_PRIVATE: { |
|
84 |
AccessFlags acc = getAccessFlags(); |
|
85 |
return Boolean.valueOf(!acc.isPrivate() && !acc.isPublic() && !acc.isProtected()); |
|
86 |
} |
|
87 |
case FIELD_IS_STATIC: |
|
88 |
return Boolean.valueOf(getAccessFlags().isStatic()); |
|
89 |
case FIELD_IS_FINAL: |
|
90 |
return Boolean.valueOf(getAccessFlags().isFinal()); |
|
91 |
case FIELD_IS_ABSTRACT: |
|
92 |
return Boolean.valueOf(getAccessFlags().isAbstract()); |
|
93 |
case FIELD_IS_STRICT: |
|
94 |
return Boolean.valueOf(getAccessFlags().isStrict()); |
|
95 |
case FIELD_IS_SYNTHETIC: |
|
96 |
return Boolean.valueOf(getAccessFlags().isSynthetic()); |
|
97 |
case FIELD_IS_INTERFACE: |
|
98 |
return Boolean.valueOf(ik.isInterface()); |
|
99 |
case FIELD_CLASS_LOADER: |
|
100 |
return factory.newJSJavaObject(ik.getClassLoader()); |
|
101 |
case FIELD_STATICS: |
|
102 |
return getStatics(); |
|
103 |
case FIELD_UNDEFINED: |
|
104 |
default: |
|
105 |
return super.getMetaClassFieldValue(name); |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
public boolean hasMetaClassField(String name) { |
|
110 |
if (getFieldID(name) != FIELD_UNDEFINED) { |
|
111 |
return true; |
|
112 |
} else { |
|
113 |
return super.hasMetaClassField(name); |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
public String getName() { |
|
118 |
return getInstanceKlass().getName().asString().replace('/', '.'); |
|
119 |
} |
|
120 |
||
121 |
public boolean isArray() { |
|
122 |
return false; |
|
123 |
} |
|
124 |
||
125 |
public String[] getMetaClassFieldNames() { |
|
126 |
String[] superFields = super.getMetaClassFieldNames(); |
|
127 |
Set k = fields.keySet(); |
|
128 |
String[] res = new String[k.size() + superFields.length]; |
|
129 |
System.arraycopy(superFields, 0, res, 0, superFields.length); |
|
130 |
int i = superFields.length; |
|
131 |
for (Iterator itr = k.iterator(); itr.hasNext();) { |
|
132 |
res[i] = (String) itr.next(); |
|
133 |
i++; |
|
134 |
} |
|
135 |
return res; |
|
136 |
} |
|
137 |
||
138 |
public Object getInstanceFieldValue(String name, Instance instance) throws NoSuchFieldException { |
|
139 |
Field fld = findInstanceField(name); |
|
140 |
if (fld != null) { |
|
141 |
return getFieldValue(fld, name, instance); |
|
142 |
} else { |
|
143 |
throw new NoSuchFieldException(name + " is not field of " |
|
144 |
+ getInstanceKlass().getName().asString().replace('/', '.')); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
public Object getStaticFieldValue(String name) throws NoSuchFieldException { |
|
149 |
Field fld = findStaticField(name); |
|
150 |
if (fld != null) { |
|
151 |
return getFieldValue(fld, name, getInstanceKlass()); |
|
152 |
} else { |
|
153 |
throw new NoSuchFieldException(name + " is not field of " |
|
154 |
+ getInstanceKlass().getName().asString().replace('/', '.')); |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
public String[] getInstanceFieldNames() { |
|
159 |
if (instanceFieldNames == null) { |
|
160 |
InstanceKlass current = getInstanceKlass(); |
|
161 |
while (current != null) { |
|
162 |
List tmp = current.getImmediateFields(); |
|
163 |
for (Iterator itr = tmp.iterator(); itr.hasNext();) { |
|
164 |
Field fld = (Field) itr.next(); |
|
165 |
if (!fld.isStatic()) { |
|
166 |
String name = fld.getID().getName(); |
|
167 |
if (instanceFields.get(name) == null) { |
|
168 |
instanceFields.put(name, fld); |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
current = (InstanceKlass) current.getSuper(); |
|
173 |
} |
|
174 |
||
175 |
Set s = instanceFields.keySet(); |
|
176 |
instanceFieldNames = new String[s.size()]; |
|
177 |
int i = 0; |
|
178 |
for (Iterator itr = s.iterator(); itr.hasNext(); i++) { |
|
179 |
instanceFieldNames[i] = (String) itr.next(); |
|
180 |
} |
|
181 |
} |
|
182 |
return instanceFieldNames; |
|
183 |
} |
|
184 |
||
185 |
public boolean hasInstanceField(String name) { |
|
186 |
Field fld = findInstanceField(name); |
|
187 |
return (fld != null)? true: false; |
|
188 |
} |
|
189 |
||
190 |
public String[] getStaticFieldNames() { |
|
191 |
if (staticFieldNames == null) { |
|
192 |
InstanceKlass current = getInstanceKlass(); |
|
193 |
List tmp = current.getImmediateFields(); |
|
194 |
for (Iterator itr = tmp.iterator(); itr.hasNext();) { |
|
195 |
Field fld = (Field) itr.next(); |
|
196 |
if (fld.isStatic()) { |
|
197 |
staticFields.put(fld.getID().getName(), fld); |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
Set s = staticFields.keySet(); |
|
202 |
staticFieldNames = new String[s.size()]; |
|
203 |
int i = 0; |
|
204 |
for (Iterator itr = s.iterator(); itr.hasNext(); i++) { |
|
205 |
staticFieldNames[i] = (String) itr.next(); |
|
206 |
} |
|
207 |
} |
|
208 |
return staticFieldNames; |
|
209 |
} |
|
210 |
||
211 |
public boolean hasStaticField(String name) { |
|
212 |
Field fld = findStaticField(name); |
|
213 |
return (fld != null)? true: false; |
|
214 |
} |
|
215 |
||
216 |
//-- Intenals only below this point |
|
217 |
private static Map fields = new HashMap(); |
|
218 |
private static void addField(String name, int fieldId) { |
|
219 |
fields.put(name, new Integer(fieldId)); |
|
220 |
} |
|
221 |
||
222 |
private static int getFieldID(String name) { |
|
223 |
Integer res = (Integer) fields.get(name); |
|
224 |
return (res != null)? res.intValue() : FIELD_UNDEFINED; |
|
225 |
} |
|
226 |
||
227 |
static { |
|
228 |
addField("sourceFile", FIELD_SOURCE_FILE); |
|
229 |
addField("interfaces", FIELD_INTERFACES); |
|
230 |
addField("fields", FIELD_FIELDS); |
|
231 |
addField("methods", FIELD_METHODS); |
|
232 |
addField("isPrivate", FIELD_IS_PRIVATE); |
|
233 |
addField("isPublic", FIELD_IS_PUBLIC); |
|
234 |
addField("isProtected", FIELD_IS_PROTECTED); |
|
235 |
addField("isPackagePrivate", FIELD_IS_PACKAGE_PRIVATE); |
|
236 |
addField("isStatic", FIELD_IS_STATIC); |
|
237 |
addField("isFinal", FIELD_IS_FINAL); |
|
238 |
addField("isAbstract", FIELD_IS_ABSTRACT); |
|
239 |
addField("isStrict", FIELD_IS_STRICT); |
|
240 |
addField("isSynthetic", FIELD_IS_SYNTHETIC); |
|
241 |
addField("isInterface", FIELD_IS_INTERFACE); |
|
242 |
addField("classLoader", FIELD_CLASS_LOADER); |
|
243 |
addField("statics", FIELD_STATICS); |
|
244 |
} |
|
245 |
||
246 |
private AccessFlags getAccessFlags() { |
|
247 |
if (accFlags == null) { |
|
248 |
accFlags = new AccessFlags(getInstanceKlass().computeModifierFlags()); |
|
249 |
} |
|
250 |
return accFlags; |
|
251 |
} |
|
252 |
||
253 |
private Object getFieldValue(Field fld, String name, Oop oop) { |
|
254 |
FieldType fd = fld.getFieldType(); |
|
255 |
if (fd.isObject() || fd.isArray()) { |
|
256 |
return factory.newJSJavaObject(((OopField)fld).getValue(oop)); |
|
257 |
} else if (fd.isByte()) { |
|
258 |
return new Byte(((ByteField)fld).getValue(oop)); |
|
259 |
} else if (fd.isChar()) { |
|
260 |
return new String(new char[] { ((CharField)fld).getValue(oop) }); |
|
261 |
} else if (fd.isDouble()) { |
|
262 |
return new Double(((DoubleField)fld).getValue(oop)); |
|
263 |
} else if (fd.isFloat()) { |
|
264 |
return new Float(((FloatField)fld).getValue(oop)); |
|
265 |
} else if (fd.isInt()) { |
|
266 |
return new Integer(((IntField)fld).getValue(oop)); |
|
267 |
} else if (fd.isLong()) { |
|
268 |
return new Long(((LongField)fld).getValue(oop)); |
|
269 |
} else if (fd.isShort()) { |
|
270 |
return new Short(((ShortField)fld).getValue(oop)); |
|
271 |
} else if (fd.isBoolean()) { |
|
272 |
return Boolean.valueOf(((BooleanField)fld).getValue(oop)); |
|
273 |
} else { |
|
274 |
if (Assert.ASSERTS_ENABLED) { |
|
275 |
Assert.that(false, "invalid field type for " + name); |
|
276 |
} |
|
277 |
return null; |
|
278 |
} |
|
279 |
} |
|
280 |
||
13728
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
281 |
private Object getFieldValue(Field fld, String name, InstanceKlass oop) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
282 |
FieldType fd = fld.getFieldType(); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
283 |
if (fd.isObject() || fd.isArray()) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
284 |
return factory.newJSJavaObject(((OopField)fld).getValue(oop)); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
285 |
} else if (fd.isByte()) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
286 |
return new Byte(((ByteField)fld).getValue(oop)); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
287 |
} else if (fd.isChar()) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
288 |
return new String(new char[] { ((CharField)fld).getValue(oop) }); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
289 |
} else if (fd.isDouble()) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
290 |
return new Double(((DoubleField)fld).getValue(oop)); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
291 |
} else if (fd.isFloat()) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
292 |
return new Float(((FloatField)fld).getValue(oop)); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
293 |
} else if (fd.isInt()) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
294 |
return new Integer(((IntField)fld).getValue(oop)); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
295 |
} else if (fd.isLong()) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
296 |
return new Long(((LongField)fld).getValue(oop)); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
297 |
} else if (fd.isShort()) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
298 |
return new Short(((ShortField)fld).getValue(oop)); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
299 |
} else if (fd.isBoolean()) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
300 |
return Boolean.valueOf(((BooleanField)fld).getValue(oop)); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
301 |
} else { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
302 |
if (Assert.ASSERTS_ENABLED) { |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
303 |
Assert.that(false, "invalid field type for " + name); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
304 |
} |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
305 |
return null; |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
306 |
} |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
307 |
} |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
5547
diff
changeset
|
308 |
|
1 | 309 |
private Field findInstanceField(String name) { |
310 |
Field fld = (Field) instanceFields.get(name); |
|
311 |
if (fld != null) { |
|
312 |
return fld; |
|
313 |
} else { |
|
314 |
InstanceKlass current = getInstanceKlass(); |
|
315 |
while (current != null) { |
|
316 |
List tmp = current.getImmediateFields(); |
|
317 |
for (Iterator itr = tmp.iterator(); itr.hasNext();) { |
|
318 |
fld = (Field) itr.next(); |
|
319 |
if (fld.getID().getName().equals(name) && !fld.isStatic()) { |
|
320 |
instanceFields.put(name, fld); |
|
321 |
return fld; |
|
322 |
} |
|
323 |
} |
|
324 |
// lookup in super class. |
|
325 |
current = (InstanceKlass) current.getSuper(); |
|
326 |
} |
|
327 |
} |
|
328 |
// no match |
|
329 |
return null; |
|
330 |
} |
|
331 |
||
332 |
private Field findStaticField(String name) { |
|
333 |
Field fld = (Field) staticFields.get(name); |
|
334 |
if (fld != null) { |
|
335 |
return fld; |
|
336 |
} else { |
|
337 |
// static fields are searched only in current. |
|
338 |
// Direct/indirect super classes and interfaces |
|
339 |
// are not included in search. |
|
340 |
InstanceKlass current = getInstanceKlass(); |
|
341 |
List tmp = current.getImmediateFields(); |
|
342 |
for (Iterator itr = tmp.iterator(); itr.hasNext();) { |
|
343 |
fld = (Field) itr.next(); |
|
344 |
if (fld.getID().getName().equals(name) && fld.isStatic()) { |
|
345 |
staticFields.put(name, fld); |
|
346 |
return fld; |
|
347 |
} |
|
348 |
} |
|
349 |
// no match |
|
350 |
return null; |
|
351 |
} |
|
352 |
} |
|
353 |
||
354 |
private JSList getInterfaces() { |
|
355 |
InstanceKlass ik = getInstanceKlass(); |
|
356 |
List intfs = ik.getDirectImplementedInterfaces(); |
|
357 |
List res = new ArrayList(0); |
|
358 |
for (Iterator itr = intfs.iterator(); itr.hasNext();) { |
|
359 |
Klass k = (Klass) itr.next(); |
|
360 |
res.add(k.getJavaMirror()); |
|
361 |
} |
|
362 |
return factory.newJSList(res); |
|
363 |
} |
|
364 |
||
365 |
private JSMap getStatics() { |
|
366 |
String[] names = getStaticFieldNames(); |
|
367 |
Map map = new HashMap(); |
|
368 |
for (int i=0; i < names.length; i++) { |
|
369 |
try { |
|
370 |
map.put(names[i], getStaticFieldValue(names[i])); |
|
371 |
} catch (NoSuchFieldException exp) {} |
|
372 |
} |
|
373 |
return factory.newJSMap(map); |
|
374 |
} |
|
375 |
||
376 |
private Map instanceFields; |
|
377 |
private Map staticFields; |
|
378 |
private String[] instanceFieldNames; |
|
379 |
private String[] staticFieldNames; |
|
380 |
private AccessFlags accFlags; |
|
381 |
} |