Mercurial > hg > graal-compiler
annotate src/share/vm/runtime/reflection.cpp @ 542:9a25e0c45327
6792421: assert(_bitMap->isMarked(addr+size-1),inconsistent Printezis mark)
Summary: The CMS concurrent precleaning and concurrent marking phases should work around classes that are undergoing redefinition.
Reviewed-by: ysr, tonyp
author | jmasa |
---|---|
date | Sat, 31 Jan 2009 00:15:00 -0800 |
parents | a45484ea312d |
children | c89f86385056 |
rev | line source |
---|---|
0 | 1 /* |
196 | 2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. |
0 | 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 #include "incls/_precompiled.incl" | |
26 #include "incls/_reflection.cpp.incl" | |
27 | |
28 #define JAVA_1_5_VERSION 49 | |
29 | |
30 static void trace_class_resolution(klassOop to_class) { | |
31 ResourceMark rm; | |
32 int line_number = -1; | |
33 const char * source_file = NULL; | |
34 klassOop caller = NULL; | |
35 JavaThread* jthread = JavaThread::current(); | |
36 if (jthread->has_last_Java_frame()) { | |
37 vframeStream vfst(jthread); | |
38 // skip over any frames belonging to java.lang.Class | |
39 while (!vfst.at_end() && | |
40 instanceKlass::cast(vfst.method()->method_holder())->name() == vmSymbols::java_lang_Class()) { | |
41 vfst.next(); | |
42 } | |
43 if (!vfst.at_end()) { | |
44 // this frame is a likely suspect | |
45 caller = vfst.method()->method_holder(); | |
46 line_number = vfst.method()->line_number_from_bci(vfst.bci()); | |
47 symbolOop s = instanceKlass::cast(vfst.method()->method_holder())->source_file_name(); | |
48 if (s != NULL) { | |
49 source_file = s->as_C_string(); | |
50 } | |
51 } | |
52 } | |
53 if (caller != NULL) { | |
54 const char * from = Klass::cast(caller)->external_name(); | |
55 const char * to = Klass::cast(to_class)->external_name(); | |
56 // print in a single call to reduce interleaving between threads | |
57 if (source_file != NULL) { | |
58 tty->print("RESOLVE %s %s %s:%d (reflection)\n", from, to, source_file, line_number); | |
59 } else { | |
60 tty->print("RESOLVE %s %s (reflection)\n", from, to); | |
61 } | |
62 } | |
63 } | |
64 | |
65 | |
66 oop Reflection::box(jvalue* value, BasicType type, TRAPS) { | |
67 if (type == T_VOID) { | |
68 return NULL; | |
69 } | |
70 if (type == T_OBJECT || type == T_ARRAY) { | |
71 // regular objects are not boxed | |
72 return (oop) value->l; | |
73 } | |
74 oop result = java_lang_boxing_object::create(type, value, CHECK_NULL); | |
75 if (result == NULL) { | |
76 THROW_(vmSymbols::java_lang_IllegalArgumentException(), result); | |
77 } | |
78 return result; | |
79 } | |
80 | |
81 | |
82 BasicType Reflection::unbox_for_primitive(oop box, jvalue* value, TRAPS) { | |
83 if (box == NULL) { | |
84 THROW_(vmSymbols::java_lang_IllegalArgumentException(), T_ILLEGAL); | |
85 } | |
86 return java_lang_boxing_object::get_value(box, value); | |
87 } | |
88 | |
89 BasicType Reflection::unbox_for_regular_object(oop box, jvalue* value) { | |
90 // Note: box is really the unboxed oop. It might even be a Short, etc.! | |
91 value->l = (jobject) box; | |
92 return T_OBJECT; | |
93 } | |
94 | |
95 | |
96 void Reflection::widen(jvalue* value, BasicType current_type, BasicType wide_type, TRAPS) { | |
97 assert(wide_type != current_type, "widen should not be called with identical types"); | |
98 switch (wide_type) { | |
99 case T_BOOLEAN: | |
100 case T_BYTE: | |
101 case T_CHAR: | |
102 break; // fail | |
103 case T_SHORT: | |
104 switch (current_type) { | |
105 case T_BYTE: | |
106 value->s = (jshort) value->b; | |
107 return; | |
108 } | |
109 break; // fail | |
110 case T_INT: | |
111 switch (current_type) { | |
112 case T_BYTE: | |
113 value->i = (jint) value->b; | |
114 return; | |
115 case T_CHAR: | |
116 value->i = (jint) value->c; | |
117 return; | |
118 case T_SHORT: | |
119 value->i = (jint) value->s; | |
120 return; | |
121 } | |
122 break; // fail | |
123 case T_LONG: | |
124 switch (current_type) { | |
125 case T_BYTE: | |
126 value->j = (jlong) value->b; | |
127 return; | |
128 case T_CHAR: | |
129 value->j = (jlong) value->c; | |
130 return; | |
131 case T_SHORT: | |
132 value->j = (jlong) value->s; | |
133 return; | |
134 case T_INT: | |
135 value->j = (jlong) value->i; | |
136 return; | |
137 } | |
138 break; // fail | |
139 case T_FLOAT: | |
140 switch (current_type) { | |
141 case T_BYTE: | |
142 value->f = (jfloat) value->b; | |
143 return; | |
144 case T_CHAR: | |
145 value->f = (jfloat) value->c; | |
146 return; | |
147 case T_SHORT: | |
148 value->f = (jfloat) value->s; | |
149 return; | |
150 case T_INT: | |
151 value->f = (jfloat) value->i; | |
152 return; | |
153 case T_LONG: | |
154 value->f = (jfloat) value->j; | |
155 return; | |
156 } | |
157 break; // fail | |
158 case T_DOUBLE: | |
159 switch (current_type) { | |
160 case T_BYTE: | |
161 value->d = (jdouble) value->b; | |
162 return; | |
163 case T_CHAR: | |
164 value->d = (jdouble) value->c; | |
165 return; | |
166 case T_SHORT: | |
167 value->d = (jdouble) value->s; | |
168 return; | |
169 case T_INT: | |
170 value->d = (jdouble) value->i; | |
171 return; | |
172 case T_FLOAT: | |
173 value->d = (jdouble) value->f; | |
174 return; | |
175 case T_LONG: | |
176 value->d = (jdouble) value->j; | |
177 return; | |
178 } | |
179 break; // fail | |
180 default: | |
181 break; // fail | |
182 } | |
183 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch"); | |
184 } | |
185 | |
186 | |
187 BasicType Reflection::array_get(jvalue* value, arrayOop a, int index, TRAPS) { | |
188 if (!a->is_within_bounds(index)) { | |
189 THROW_(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), T_ILLEGAL); | |
190 } | |
191 if (a->is_objArray()) { | |
192 value->l = (jobject) objArrayOop(a)->obj_at(index); | |
193 return T_OBJECT; | |
194 } else { | |
195 assert(a->is_typeArray(), "just checking"); | |
196 BasicType type = typeArrayKlass::cast(a->klass())->element_type(); | |
197 switch (type) { | |
198 case T_BOOLEAN: | |
199 value->z = typeArrayOop(a)->bool_at(index); | |
200 break; | |
201 case T_CHAR: | |
202 value->c = typeArrayOop(a)->char_at(index); | |
203 break; | |
204 case T_FLOAT: | |
205 value->f = typeArrayOop(a)->float_at(index); | |
206 break; | |
207 case T_DOUBLE: | |
208 value->d = typeArrayOop(a)->double_at(index); | |
209 break; | |
210 case T_BYTE: | |
211 value->b = typeArrayOop(a)->byte_at(index); | |
212 break; | |
213 case T_SHORT: | |
214 value->s = typeArrayOop(a)->short_at(index); | |
215 break; | |
216 case T_INT: | |
217 value->i = typeArrayOop(a)->int_at(index); | |
218 break; | |
219 case T_LONG: | |
220 value->j = typeArrayOop(a)->long_at(index); | |
221 break; | |
222 default: | |
223 return T_ILLEGAL; | |
224 } | |
225 return type; | |
226 } | |
227 } | |
228 | |
229 | |
230 void Reflection::array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS) { | |
231 if (!a->is_within_bounds(index)) { | |
232 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); | |
233 } | |
234 if (a->is_objArray()) { | |
235 if (value_type == T_OBJECT) { | |
236 oop obj = (oop) value->l; | |
237 if (obj != NULL) { | |
238 klassOop element_klass = objArrayKlass::cast(a->klass())->element_klass(); | |
239 if (!obj->is_a(element_klass)) { | |
240 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch"); | |
241 } | |
242 } | |
243 objArrayOop(a)->obj_at_put(index, obj); | |
244 } | |
245 } else { | |
246 assert(a->is_typeArray(), "just checking"); | |
247 BasicType array_type = typeArrayKlass::cast(a->klass())->element_type(); | |
248 if (array_type != value_type) { | |
249 // The widen operation can potentially throw an exception, but cannot block, | |
250 // so typeArrayOop a is safe if the call succeeds. | |
251 widen(value, value_type, array_type, CHECK); | |
252 } | |
253 switch (array_type) { | |
254 case T_BOOLEAN: | |
255 typeArrayOop(a)->bool_at_put(index, value->z); | |
256 break; | |
257 case T_CHAR: | |
258 typeArrayOop(a)->char_at_put(index, value->c); | |
259 break; | |
260 case T_FLOAT: | |
261 typeArrayOop(a)->float_at_put(index, value->f); | |
262 break; | |
263 case T_DOUBLE: | |
264 typeArrayOop(a)->double_at_put(index, value->d); | |
265 break; | |
266 case T_BYTE: | |
267 typeArrayOop(a)->byte_at_put(index, value->b); | |
268 break; | |
269 case T_SHORT: | |
270 typeArrayOop(a)->short_at_put(index, value->s); | |
271 break; | |
272 case T_INT: | |
273 typeArrayOop(a)->int_at_put(index, value->i); | |
274 break; | |
275 case T_LONG: | |
276 typeArrayOop(a)->long_at_put(index, value->j); | |
277 break; | |
278 default: | |
279 THROW(vmSymbols::java_lang_IllegalArgumentException()); | |
280 } | |
281 } | |
282 } | |
283 | |
284 | |
285 klassOop Reflection::basic_type_mirror_to_arrayklass(oop basic_type_mirror, TRAPS) { | |
286 assert(java_lang_Class::is_primitive(basic_type_mirror), "just checking"); | |
287 BasicType type = java_lang_Class::primitive_type(basic_type_mirror); | |
288 if (type == T_VOID) { | |
289 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); | |
290 } else { | |
291 return Universe::typeArrayKlassObj(type); | |
292 } | |
293 } | |
294 | |
295 | |
296 oop Reflection:: basic_type_arrayklass_to_mirror(klassOop basic_type_arrayklass, TRAPS) { | |
297 BasicType type = typeArrayKlass::cast(basic_type_arrayklass)->element_type(); | |
298 return Universe::java_mirror(type); | |
299 } | |
300 | |
301 | |
302 arrayOop Reflection::reflect_new_array(oop element_mirror, jint length, TRAPS) { | |
303 if (element_mirror == NULL) { | |
304 THROW_0(vmSymbols::java_lang_NullPointerException()); | |
305 } | |
306 if (length < 0) { | |
307 THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); | |
308 } | |
309 if (java_lang_Class::is_primitive(element_mirror)) { | |
310 klassOop tak = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL); | |
311 return typeArrayKlass::cast(tak)->allocate(length, THREAD); | |
312 } else { | |
313 klassOop k = java_lang_Class::as_klassOop(element_mirror); | |
314 if (Klass::cast(k)->oop_is_array() && arrayKlass::cast(k)->dimension() >= MAX_DIM) { | |
315 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); | |
316 } | |
317 return oopFactory::new_objArray(k, length, THREAD); | |
318 } | |
319 } | |
320 | |
321 | |
322 arrayOop Reflection::reflect_new_multi_array(oop element_mirror, typeArrayOop dim_array, TRAPS) { | |
323 assert(dim_array->is_typeArray(), "just checking"); | |
324 assert(typeArrayKlass::cast(dim_array->klass())->element_type() == T_INT, "just checking"); | |
325 | |
326 if (element_mirror == NULL) { | |
327 THROW_0(vmSymbols::java_lang_NullPointerException()); | |
328 } | |
329 | |
330 int len = dim_array->length(); | |
331 if (len <= 0 || len > MAX_DIM) { | |
332 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); | |
333 } | |
334 | |
335 jint dimensions[MAX_DIM]; // C array copy of intArrayOop | |
336 for (int i = 0; i < len; i++) { | |
337 int d = dim_array->int_at(i); | |
338 if (d < 0) { | |
339 THROW_0(vmSymbols::java_lang_NegativeArraySizeException()); | |
340 } | |
341 dimensions[i] = d; | |
342 } | |
343 | |
344 klassOop klass; | |
345 int dim = len; | |
346 if (java_lang_Class::is_primitive(element_mirror)) { | |
347 klass = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL); | |
348 } else { | |
349 klass = java_lang_Class::as_klassOop(element_mirror); | |
350 if (Klass::cast(klass)->oop_is_array()) { | |
351 int k_dim = arrayKlass::cast(klass)->dimension(); | |
352 if (k_dim + len > MAX_DIM) { | |
353 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); | |
354 } | |
355 dim += k_dim; | |
356 } | |
357 } | |
358 klass = Klass::cast(klass)->array_klass(dim, CHECK_NULL); | |
359 oop obj = arrayKlass::cast(klass)->multi_allocate(len, dimensions, THREAD); | |
360 assert(obj->is_array(), "just checking"); | |
361 return arrayOop(obj); | |
362 } | |
363 | |
364 | |
365 oop Reflection::array_component_type(oop mirror, TRAPS) { | |
366 if (java_lang_Class::is_primitive(mirror)) { | |
367 return NULL; | |
368 } | |
369 | |
370 klassOop klass = java_lang_Class::as_klassOop(mirror); | |
371 if (!Klass::cast(klass)->oop_is_array()) { | |
372 return NULL; | |
373 } | |
374 | |
375 oop result = arrayKlass::cast(klass)->component_mirror(); | |
376 #ifdef ASSERT | |
377 oop result2 = NULL; | |
378 if (arrayKlass::cast(klass)->dimension() == 1) { | |
379 if (Klass::cast(klass)->oop_is_typeArray()) { | |
380 result2 = basic_type_arrayklass_to_mirror(klass, CHECK_NULL); | |
381 } else { | |
382 result2 = Klass::cast(objArrayKlass::cast(klass)->element_klass())->java_mirror(); | |
383 } | |
384 } else { | |
385 klassOop lower_dim = arrayKlass::cast(klass)->lower_dimension(); | |
386 assert(Klass::cast(lower_dim)->oop_is_array(), "just checking"); | |
387 result2 = Klass::cast(lower_dim)->java_mirror(); | |
388 } | |
389 assert(result == result2, "results must be consistent"); | |
390 #endif //ASSERT | |
391 return result; | |
392 } | |
393 | |
394 | |
395 bool Reflection::reflect_check_access(klassOop field_class, AccessFlags acc, klassOop target_class, bool is_method_invoke, TRAPS) { | |
396 // field_class : declaring class | |
397 // acc : declared field access | |
398 // target_class : for protected | |
399 | |
400 // Check if field or method is accessible to client. Throw an | |
401 // IllegalAccessException and return false if not. | |
402 | |
403 // The "client" is the class associated with the nearest real frame | |
404 // getCallerClass already skips Method.invoke frames, so pass 0 in | |
405 // that case (same as classic). | |
406 ResourceMark rm(THREAD); | |
407 assert(THREAD->is_Java_thread(), "sanity check"); | |
408 klassOop client_class = ((JavaThread *)THREAD)->security_get_caller_class(is_method_invoke ? 0 : 1); | |
409 | |
410 if (client_class != field_class) { | |
411 if (!verify_class_access(client_class, field_class, false) | |
412 || !verify_field_access(client_class, | |
413 field_class, | |
414 field_class, | |
415 acc, | |
416 false)) { | |
417 THROW_(vmSymbols::java_lang_IllegalAccessException(), false); | |
418 } | |
419 } | |
420 | |
421 // Additional test for protected members: JLS 6.6.2 | |
422 | |
423 if (acc.is_protected()) { | |
424 if (target_class != client_class) { | |
425 if (!is_same_class_package(client_class, field_class)) { | |
426 if (!Klass::cast(target_class)->is_subclass_of(client_class)) { | |
427 THROW_(vmSymbols::java_lang_IllegalAccessException(), false); | |
428 } | |
429 } | |
430 } | |
431 } | |
432 | |
433 // Passed all tests | |
434 return true; | |
435 } | |
436 | |
437 | |
438 bool Reflection::verify_class_access(klassOop current_class, klassOop new_class, bool classloader_only) { | |
439 // Verify that current_class can access new_class. If the classloader_only | |
440 // flag is set, we automatically allow any accesses in which current_class | |
441 // doesn't have a classloader. | |
442 if ((current_class == NULL) || | |
443 (current_class == new_class) || | |
444 (instanceKlass::cast(new_class)->is_public()) || | |
445 is_same_class_package(current_class, new_class)) { | |
446 return true; | |
447 } | |
448 // New (1.4) reflection implementation. Allow all accesses from | |
449 // sun/reflect/MagicAccessorImpl subclasses to succeed trivially. | |
450 if ( JDK_Version::is_gte_jdk14x_version() | |
451 && UseNewReflection | |
452 && Klass::cast(current_class)->is_subclass_of(SystemDictionary::reflect_magic_klass())) { | |
453 return true; | |
454 } | |
455 | |
456 return can_relax_access_check_for(current_class, new_class, classloader_only); | |
457 } | |
458 | |
431
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
459 static bool under_host_klass(instanceKlass* ik, klassOop host_klass) { |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
460 DEBUG_ONLY(int inf_loop_check = 1000 * 1000 * 1000); |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
461 for (;;) { |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
462 klassOop hc = (klassOop) ik->host_klass(); |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
463 if (hc == NULL) return false; |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
464 if (hc == host_klass) return true; |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
465 ik = instanceKlass::cast(hc); |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
466 |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
467 // There's no way to make a host class loop short of patching memory. |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
468 // Therefore there cannot be a loop here unles there's another bug. |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
469 // Still, let's check for it. |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
470 assert(--inf_loop_check > 0, "no host_klass loop"); |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
471 } |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
472 } |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
473 |
0 | 474 bool Reflection::can_relax_access_check_for( |
475 klassOop accessor, klassOop accessee, bool classloader_only) { | |
476 instanceKlass* accessor_ik = instanceKlass::cast(accessor); | |
477 instanceKlass* accessee_ik = instanceKlass::cast(accessee); | |
431
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
478 |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
479 // If either is on the other's host_klass chain, access is OK, |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
480 // because one is inside the other. |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
481 if (under_host_klass(accessor_ik, accessee) || |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
482 under_host_klass(accessee_ik, accessor)) |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
483 return true; |
a45484ea312d
6653858: dynamic languages need to be able to load anonymous classes
jrose
parents:
196
diff
changeset
|
484 |
0 | 485 if (RelaxAccessControlCheck || |
486 (accessor_ik->major_version() < JAVA_1_5_VERSION && | |
487 accessee_ik->major_version() < JAVA_1_5_VERSION)) { | |
488 return classloader_only && | |
489 Verifier::relax_verify_for(accessor_ik->class_loader()) && | |
490 accessor_ik->protection_domain() == accessee_ik->protection_domain() && | |
491 accessor_ik->class_loader() == accessee_ik->class_loader(); | |
492 } else { | |
493 return false; | |
494 } | |
495 } | |
496 | |
497 bool Reflection::verify_field_access(klassOop current_class, | |
498 klassOop resolved_class, | |
499 klassOop field_class, | |
500 AccessFlags access, | |
501 bool classloader_only, | |
502 bool protected_restriction) { | |
503 // Verify that current_class can access a field of field_class, where that | |
504 // field's access bits are "access". We assume that we've already verified | |
505 // that current_class can access field_class. | |
506 // | |
507 // If the classloader_only flag is set, we automatically allow any accesses | |
508 // in which current_class doesn't have a classloader. | |
509 // | |
510 // "resolved_class" is the runtime type of "field_class". Sometimes we don't | |
511 // need this distinction (e.g. if all we have is the runtime type, or during | |
512 // class file parsing when we only care about the static type); in that case | |
513 // callers should ensure that resolved_class == field_class. | |
514 // | |
515 if ((current_class == NULL) || | |
516 (current_class == field_class) || | |
517 access.is_public()) { | |
518 return true; | |
519 } | |
520 | |
521 if (access.is_protected()) { | |
522 if (!protected_restriction) { | |
523 // See if current_class is a subclass of field_class | |
524 if (Klass::cast(current_class)->is_subclass_of(field_class)) { | |
115 | 525 if (access.is_static() || // static fields are ok, see 6622385 |
526 current_class == resolved_class || | |
0 | 527 field_class == resolved_class || |
528 Klass::cast(current_class)->is_subclass_of(resolved_class) || | |
529 Klass::cast(resolved_class)->is_subclass_of(current_class)) { | |
530 return true; | |
531 } | |
532 } | |
533 } | |
534 } | |
535 | |
536 if (!access.is_private() && is_same_class_package(current_class, field_class)) { | |
537 return true; | |
538 } | |
539 | |
540 // New (1.4) reflection implementation. Allow all accesses from | |
541 // sun/reflect/MagicAccessorImpl subclasses to succeed trivially. | |
542 if ( JDK_Version::is_gte_jdk14x_version() | |
543 && UseNewReflection | |
544 && Klass::cast(current_class)->is_subclass_of(SystemDictionary::reflect_magic_klass())) { | |
545 return true; | |
546 } | |
547 | |
548 return can_relax_access_check_for( | |
549 current_class, field_class, classloader_only); | |
550 } | |
551 | |
552 | |
553 bool Reflection::is_same_class_package(klassOop class1, klassOop class2) { | |
554 return instanceKlass::cast(class1)->is_same_class_package(class2); | |
555 } | |
556 | |
557 | |
558 // Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not, | |
559 // throw an incompatible class change exception | |
560 void Reflection::check_for_inner_class(instanceKlassHandle outer, instanceKlassHandle inner, TRAPS) { | |
561 const int inner_class_info_index = 0; | |
562 const int outer_class_info_index = 1; | |
563 | |
564 typeArrayHandle icls (THREAD, outer->inner_classes()); | |
565 constantPoolHandle cp (THREAD, outer->constants()); | |
566 for(int i = 0; i < icls->length(); i += 4) { | |
567 int ioff = icls->ushort_at(i + inner_class_info_index); | |
568 int ooff = icls->ushort_at(i + outer_class_info_index); | |
569 | |
570 if (ioff != 0 && ooff != 0) { | |
571 klassOop o = cp->klass_at(ooff, CHECK); | |
572 if (o == outer()) { | |
573 klassOop i = cp->klass_at(ioff, CHECK); | |
574 if (i == inner()) { | |
575 return; | |
576 } | |
577 } | |
578 } | |
579 } | |
580 | |
581 // 'inner' not declared as an inner klass in outer | |
582 ResourceMark rm(THREAD); | |
583 Exceptions::fthrow( | |
584 THREAD_AND_LOCATION, | |
585 vmSymbolHandles::java_lang_IncompatibleClassChangeError(), | |
586 "%s and %s disagree on InnerClasses attribute", | |
587 outer->external_name(), | |
588 inner->external_name() | |
589 ); | |
590 } | |
591 | |
592 // Utility method converting a single SignatureStream element into java.lang.Class instance | |
593 | |
594 oop get_mirror_from_signature(methodHandle method, SignatureStream* ss, TRAPS) { | |
595 switch (ss->type()) { | |
596 default: | |
597 assert(ss->type() != T_VOID || ss->at_return_type(), "T_VOID should only appear as return type"); | |
598 return java_lang_Class::primitive_mirror(ss->type()); | |
599 case T_OBJECT: | |
600 case T_ARRAY: | |
601 symbolOop name = ss->as_symbol(CHECK_NULL); | |
602 oop loader = instanceKlass::cast(method->method_holder())->class_loader(); | |
603 oop protection_domain = instanceKlass::cast(method->method_holder())->protection_domain(); | |
604 klassOop k = SystemDictionary::resolve_or_fail( | |
605 symbolHandle(THREAD, name), | |
606 Handle(THREAD, loader), | |
607 Handle(THREAD, protection_domain), | |
608 true, CHECK_NULL); | |
609 if (TraceClassResolution) { | |
610 trace_class_resolution(k); | |
611 } | |
612 return k->klass_part()->java_mirror(); | |
613 }; | |
614 } | |
615 | |
616 | |
617 objArrayHandle Reflection::get_parameter_types(methodHandle method, int parameter_count, oop* return_type, TRAPS) { | |
618 // Allocate array holding parameter types (java.lang.Class instances) | |
619 objArrayOop m = oopFactory::new_objArray(SystemDictionary::class_klass(), parameter_count, CHECK_(objArrayHandle())); | |
620 objArrayHandle mirrors (THREAD, m); | |
621 int index = 0; | |
622 // Collect parameter types | |
623 symbolHandle signature (THREAD, method->signature()); | |
624 SignatureStream ss(signature); | |
625 while (!ss.at_return_type()) { | |
626 oop mirror = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle())); | |
627 mirrors->obj_at_put(index++, mirror); | |
628 ss.next(); | |
629 } | |
630 assert(index == parameter_count, "invalid parameter count"); | |
631 if (return_type != NULL) { | |
632 // Collect return type as well | |
633 assert(ss.at_return_type(), "return type should be present"); | |
634 *return_type = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle())); | |
635 } | |
636 return mirrors; | |
637 } | |
638 | |
639 objArrayHandle Reflection::get_exception_types(methodHandle method, TRAPS) { | |
640 return method->resolved_checked_exceptions(CHECK_(objArrayHandle())); | |
641 } | |
642 | |
643 | |
644 Handle Reflection::new_type(symbolHandle signature, KlassHandle k, TRAPS) { | |
645 // Basic types | |
646 BasicType type = vmSymbols::signature_type(signature()); | |
647 if (type != T_OBJECT) { | |
648 return Handle(THREAD, Universe::java_mirror(type)); | |
649 } | |
650 | |
651 oop loader = instanceKlass::cast(k())->class_loader(); | |
652 oop protection_domain = Klass::cast(k())->protection_domain(); | |
653 klassOop result = SystemDictionary::resolve_or_fail(signature, | |
654 Handle(THREAD, loader), | |
655 Handle(THREAD, protection_domain), | |
656 true, CHECK_(Handle())); | |
657 | |
658 if (TraceClassResolution) { | |
659 trace_class_resolution(result); | |
660 } | |
661 | |
662 oop nt = Klass::cast(result)->java_mirror(); | |
663 return Handle(THREAD, nt); | |
664 } | |
665 | |
666 | |
667 oop Reflection::new_method(methodHandle method, bool intern_name, bool for_constant_pool_access, TRAPS) { | |
668 // In jdk1.2.x, getMethods on an interface erroneously includes <clinit>, thus the complicated assert. | |
669 // Also allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods. | |
670 assert(!method()->is_initializer() || | |
671 (for_constant_pool_access && method()->is_static()) || | |
672 (method()->name() == vmSymbols::class_initializer_name() | |
673 && Klass::cast(method()->method_holder())->is_interface() && JDK_Version::is_jdk12x_version()), "should call new_constructor instead"); | |
674 instanceKlassHandle holder (THREAD, method->method_holder()); | |
675 int slot = method->method_idnum(); | |
676 | |
677 symbolHandle signature (THREAD, method->signature()); | |
678 int parameter_count = ArgumentCount(signature).size(); | |
679 oop return_type_oop = NULL; | |
680 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL); | |
681 if (parameter_types.is_null() || return_type_oop == NULL) return NULL; | |
682 | |
683 Handle return_type(THREAD, return_type_oop); | |
684 | |
685 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL); | |
686 | |
687 if (exception_types.is_null()) return NULL; | |
688 | |
689 symbolHandle method_name(THREAD, method->name()); | |
690 Handle name; | |
691 if (intern_name) { | |
692 // intern_name is only true with UseNewReflection | |
693 oop name_oop = StringTable::intern(method_name(), CHECK_NULL); | |
694 name = Handle(THREAD, name_oop); | |
695 } else { | |
696 name = java_lang_String::create_from_symbol(method_name, CHECK_NULL); | |
697 } | |
698 if (name.is_null()) return NULL; | |
699 | |
700 int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; | |
701 | |
702 Handle mh = java_lang_reflect_Method::create(CHECK_NULL); | |
703 | |
704 java_lang_reflect_Method::set_clazz(mh(), holder->java_mirror()); | |
705 java_lang_reflect_Method::set_slot(mh(), slot); | |
706 java_lang_reflect_Method::set_name(mh(), name()); | |
707 java_lang_reflect_Method::set_return_type(mh(), return_type()); | |
708 java_lang_reflect_Method::set_parameter_types(mh(), parameter_types()); | |
709 java_lang_reflect_Method::set_exception_types(mh(), exception_types()); | |
710 java_lang_reflect_Method::set_modifiers(mh(), modifiers); | |
711 java_lang_reflect_Method::set_override(mh(), false); | |
712 if (java_lang_reflect_Method::has_signature_field() && | |
713 method->generic_signature() != NULL) { | |
714 symbolHandle gs(THREAD, method->generic_signature()); | |
715 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL); | |
716 java_lang_reflect_Method::set_signature(mh(), sig()); | |
717 } | |
718 if (java_lang_reflect_Method::has_annotations_field()) { | |
719 java_lang_reflect_Method::set_annotations(mh(), method->annotations()); | |
720 } | |
721 if (java_lang_reflect_Method::has_parameter_annotations_field()) { | |
722 java_lang_reflect_Method::set_parameter_annotations(mh(), method->parameter_annotations()); | |
723 } | |
724 if (java_lang_reflect_Method::has_annotation_default_field()) { | |
725 java_lang_reflect_Method::set_annotation_default(mh(), method->annotation_default()); | |
726 } | |
727 return mh(); | |
728 } | |
729 | |
730 | |
731 oop Reflection::new_constructor(methodHandle method, TRAPS) { | |
732 assert(method()->is_initializer(), "should call new_method instead"); | |
733 | |
734 instanceKlassHandle holder (THREAD, method->method_holder()); | |
735 int slot = method->method_idnum(); | |
736 | |
737 symbolHandle signature (THREAD, method->signature()); | |
738 int parameter_count = ArgumentCount(signature).size(); | |
739 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, NULL, CHECK_NULL); | |
740 if (parameter_types.is_null()) return NULL; | |
741 | |
742 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL); | |
743 if (exception_types.is_null()) return NULL; | |
744 | |
745 int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; | |
746 | |
747 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL); | |
748 | |
749 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror()); | |
750 java_lang_reflect_Constructor::set_slot(ch(), slot); | |
751 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types()); | |
752 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types()); | |
753 java_lang_reflect_Constructor::set_modifiers(ch(), modifiers); | |
754 java_lang_reflect_Constructor::set_override(ch(), false); | |
755 if (java_lang_reflect_Constructor::has_signature_field() && | |
756 method->generic_signature() != NULL) { | |
757 symbolHandle gs(THREAD, method->generic_signature()); | |
758 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL); | |
759 java_lang_reflect_Constructor::set_signature(ch(), sig()); | |
760 } | |
761 if (java_lang_reflect_Constructor::has_annotations_field()) { | |
762 java_lang_reflect_Constructor::set_annotations(ch(), method->annotations()); | |
763 } | |
764 if (java_lang_reflect_Constructor::has_parameter_annotations_field()) { | |
765 java_lang_reflect_Constructor::set_parameter_annotations(ch(), method->parameter_annotations()); | |
766 } | |
767 return ch(); | |
768 } | |
769 | |
770 | |
771 oop Reflection::new_field(fieldDescriptor* fd, bool intern_name, TRAPS) { | |
772 symbolHandle field_name(THREAD, fd->name()); | |
773 Handle name; | |
774 if (intern_name) { | |
775 // intern_name is only true with UseNewReflection | |
776 oop name_oop = StringTable::intern(field_name(), CHECK_NULL); | |
777 name = Handle(THREAD, name_oop); | |
778 } else { | |
779 name = java_lang_String::create_from_symbol(field_name, CHECK_NULL); | |
780 } | |
781 symbolHandle signature (THREAD, fd->signature()); | |
782 KlassHandle holder (THREAD, fd->field_holder()); | |
783 Handle type = new_type(signature, holder, CHECK_NULL); | |
784 Handle rh = java_lang_reflect_Field::create(CHECK_NULL); | |
785 | |
786 java_lang_reflect_Field::set_clazz(rh(), Klass::cast(fd->field_holder())->java_mirror()); | |
787 java_lang_reflect_Field::set_slot(rh(), fd->index()); | |
788 java_lang_reflect_Field::set_name(rh(), name()); | |
789 java_lang_reflect_Field::set_type(rh(), type()); | |
790 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here. | |
791 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS); | |
792 java_lang_reflect_Field::set_override(rh(), false); | |
793 if (java_lang_reflect_Field::has_signature_field() && | |
794 fd->generic_signature() != NULL) { | |
795 symbolHandle gs(THREAD, fd->generic_signature()); | |
796 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL); | |
797 java_lang_reflect_Field::set_signature(rh(), sig()); | |
798 } | |
799 if (java_lang_reflect_Field::has_annotations_field()) { | |
800 java_lang_reflect_Field::set_annotations(rh(), fd->annotations()); | |
801 } | |
802 return rh(); | |
803 } | |
804 | |
805 | |
806 //--------------------------------------------------------------------------- | |
807 // | |
808 // Supporting routines for old native code-based reflection (pre-JDK 1.4). | |
809 // | |
810 // See reflection.hpp for details. | |
811 // | |
812 //--------------------------------------------------------------------------- | |
813 | |
814 #ifdef SUPPORT_OLD_REFLECTION | |
815 | |
816 methodHandle Reflection::resolve_interface_call(instanceKlassHandle klass, methodHandle method, | |
817 KlassHandle recv_klass, Handle receiver, TRAPS) { | |
818 assert(!method.is_null() , "method should not be null"); | |
819 | |
820 CallInfo info; | |
821 symbolHandle signature (THREAD, method->signature()); | |
822 symbolHandle name (THREAD, method->name()); | |
823 LinkResolver::resolve_interface_call(info, receiver, recv_klass, klass, | |
824 name, signature, | |
825 KlassHandle(), false, true, | |
826 CHECK_(methodHandle())); | |
827 return info.selected_method(); | |
828 } | |
829 | |
830 | |
831 oop Reflection::invoke(instanceKlassHandle klass, methodHandle reflected_method, | |
832 Handle receiver, bool override, objArrayHandle ptypes, | |
833 BasicType rtype, objArrayHandle args, bool is_method_invoke, TRAPS) { | |
834 ResourceMark rm(THREAD); | |
835 | |
836 methodHandle method; // actual method to invoke | |
837 KlassHandle target_klass; // target klass, receiver's klass for non-static | |
838 | |
839 // Ensure klass is initialized | |
840 klass->initialize(CHECK_NULL); | |
841 | |
842 bool is_static = reflected_method->is_static(); | |
843 if (is_static) { | |
844 // ignore receiver argument | |
845 method = reflected_method; | |
846 target_klass = klass; | |
847 } else { | |
848 // check for null receiver | |
849 if (receiver.is_null()) { | |
850 THROW_0(vmSymbols::java_lang_NullPointerException()); | |
851 } | |
852 // Check class of receiver against class declaring method | |
853 if (!receiver->is_a(klass())) { | |
854 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class"); | |
855 } | |
856 // target klass is receiver's klass | |
857 target_klass = KlassHandle(THREAD, receiver->klass()); | |
858 // no need to resolve if method is private or <init> | |
859 if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) { | |
860 method = reflected_method; | |
861 } else { | |
862 // resolve based on the receiver | |
863 if (instanceKlass::cast(reflected_method->method_holder())->is_interface()) { | |
864 // resolve interface call | |
865 if (ReflectionWrapResolutionErrors) { | |
866 // new default: 6531596 | |
867 // Match resolution errors with those thrown due to reflection inlining | |
868 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod() | |
869 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD); | |
870 if (HAS_PENDING_EXCEPTION) { | |
871 // Method resolution threw an exception; wrap it in an InvocationTargetException | |
872 oop resolution_exception = PENDING_EXCEPTION; | |
873 CLEAR_PENDING_EXCEPTION; | |
874 JavaCallArguments args(Handle(THREAD, resolution_exception)); | |
875 THROW_ARG_0(vmSymbolHandles::java_lang_reflect_InvocationTargetException(), | |
876 vmSymbolHandles::throwable_void_signature(), | |
877 &args); | |
878 } | |
879 } else { | |
880 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, CHECK_(NULL)); | |
881 } | |
882 } else { | |
883 // if the method can be overridden, we resolve using the vtable index. | |
884 int index = reflected_method->vtable_index(); | |
885 method = reflected_method; | |
886 if (index != methodOopDesc::nonvirtual_vtable_index) { | |
887 // target_klass might be an arrayKlassOop but all vtables start at | |
888 // the same place. The cast is to avoid virtual call and assertion. | |
889 instanceKlass* inst = (instanceKlass*)target_klass()->klass_part(); | |
890 method = methodHandle(THREAD, inst->method_at_vtable(index)); | |
891 } | |
892 if (!method.is_null()) { | |
893 // Check for abstract methods as well | |
894 if (method->is_abstract()) { | |
895 // new default: 6531596 | |
896 if (ReflectionWrapResolutionErrors) { | |
897 ResourceMark rm(THREAD); | |
898 Handle h_origexception = Exceptions::new_exception(THREAD, | |
899 vmSymbols::java_lang_AbstractMethodError(), | |
900 methodOopDesc::name_and_sig_as_C_string(Klass::cast(target_klass()), | |
901 method->name(), | |
902 method->signature())); | |
903 JavaCallArguments args(h_origexception); | |
904 THROW_ARG_0(vmSymbolHandles::java_lang_reflect_InvocationTargetException(), | |
905 vmSymbolHandles::throwable_void_signature(), | |
906 &args); | |
907 } else { | |
908 ResourceMark rm(THREAD); | |
909 THROW_MSG_0(vmSymbols::java_lang_AbstractMethodError(), | |
910 methodOopDesc::name_and_sig_as_C_string(Klass::cast(target_klass()), | |
911 method->name(), | |
912 method->signature())); | |
913 } | |
914 } | |
915 } | |
916 } | |
917 } | |
918 } | |
919 | |
920 // I believe this is a ShouldNotGetHere case which requires | |
921 // an internal vtable bug. If you ever get this please let Karen know. | |
922 if (method.is_null()) { | |
923 ResourceMark rm(THREAD); | |
924 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), | |
925 methodOopDesc::name_and_sig_as_C_string(Klass::cast(klass()), | |
926 reflected_method->name(), | |
927 reflected_method->signature())); | |
928 } | |
929 | |
930 // In the JDK 1.4 reflection implementation, the security check is | |
931 // done at the Java level | |
932 if (!(JDK_Version::is_gte_jdk14x_version() && UseNewReflection)) { | |
933 | |
934 // Access checking (unless overridden by Method) | |
935 if (!override) { | |
936 if (!(klass->is_public() && reflected_method->is_public())) { | |
937 bool access = Reflection::reflect_check_access(klass(), reflected_method->access_flags(), target_klass(), is_method_invoke, CHECK_NULL); | |
938 if (!access) { | |
939 return NULL; // exception | |
940 } | |
941 } | |
942 } | |
943 | |
944 } // !(Universe::is_gte_jdk14x_version() && UseNewReflection) | |
945 | |
946 assert(ptypes->is_objArray(), "just checking"); | |
947 int args_len = args.is_null() ? 0 : args->length(); | |
948 // Check number of arguments | |
949 if (ptypes->length() != args_len) { | |
950 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "wrong number of arguments"); | |
951 } | |
952 | |
953 // Create object to contain parameters for the JavaCall | |
954 JavaCallArguments java_args(method->size_of_parameters()); | |
955 | |
956 if (!is_static) { | |
957 java_args.push_oop(receiver); | |
958 } | |
959 | |
960 for (int i = 0; i < args_len; i++) { | |
961 oop type_mirror = ptypes->obj_at(i); | |
962 oop arg = args->obj_at(i); | |
963 if (java_lang_Class::is_primitive(type_mirror)) { | |
964 jvalue value; | |
965 BasicType ptype = basic_type_mirror_to_basic_type(type_mirror, CHECK_NULL); | |
966 BasicType atype = unbox_for_primitive(arg, &value, CHECK_NULL); | |
967 if (ptype != atype) { | |
968 widen(&value, atype, ptype, CHECK_NULL); | |
969 } | |
970 switch (ptype) { | |
971 case T_BOOLEAN: java_args.push_int(value.z); break; | |
972 case T_CHAR: java_args.push_int(value.c); break; | |
973 case T_BYTE: java_args.push_int(value.b); break; | |
974 case T_SHORT: java_args.push_int(value.s); break; | |
975 case T_INT: java_args.push_int(value.i); break; | |
976 case T_LONG: java_args.push_long(value.j); break; | |
977 case T_FLOAT: java_args.push_float(value.f); break; | |
978 case T_DOUBLE: java_args.push_double(value.d); break; | |
979 default: | |
980 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch"); | |
981 } | |
982 } else { | |
983 if (arg != NULL) { | |
984 klassOop k = java_lang_Class::as_klassOop(type_mirror); | |
985 if (!arg->is_a(k)) { | |
986 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch"); | |
987 } | |
988 } | |
989 Handle arg_handle(THREAD, arg); // Create handle for argument | |
990 java_args.push_oop(arg_handle); // Push handle | |
991 } | |
992 } | |
993 | |
994 assert(java_args.size_of_parameters() == method->size_of_parameters(), "just checking"); | |
995 | |
996 // All oops (including receiver) is passed in as Handles. An potential oop is returned as an | |
997 // oop (i.e., NOT as an handle) | |
998 JavaValue result(rtype); | |
999 JavaCalls::call(&result, method, &java_args, THREAD); | |
1000 | |
1001 if (HAS_PENDING_EXCEPTION) { | |
1002 // Method threw an exception; wrap it in an InvocationTargetException | |
1003 oop target_exception = PENDING_EXCEPTION; | |
1004 CLEAR_PENDING_EXCEPTION; | |
1005 JavaCallArguments args(Handle(THREAD, target_exception)); | |
1006 THROW_ARG_0(vmSymbolHandles::java_lang_reflect_InvocationTargetException(), | |
1007 vmSymbolHandles::throwable_void_signature(), | |
1008 &args); | |
1009 } else { | |
1010 if (rtype == T_BOOLEAN || rtype == T_BYTE || rtype == T_CHAR || rtype == T_SHORT) | |
1011 narrow((jvalue*) result.get_value_addr(), rtype, CHECK_NULL); | |
1012 return box((jvalue*) result.get_value_addr(), rtype, CHECK_NULL); | |
1013 } | |
1014 } | |
1015 | |
1016 | |
1017 void Reflection::narrow(jvalue* value, BasicType narrow_type, TRAPS) { | |
1018 switch (narrow_type) { | |
1019 case T_BOOLEAN: | |
1020 value->z = (jboolean) value->i; | |
1021 return; | |
1022 case T_BYTE: | |
1023 value->b = (jbyte) value->i; | |
1024 return; | |
1025 case T_CHAR: | |
1026 value->c = (jchar) value->i; | |
1027 return; | |
1028 case T_SHORT: | |
1029 value->s = (jshort) value->i; | |
1030 return; | |
1031 default: | |
1032 break; // fail | |
1033 } | |
1034 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch"); | |
1035 } | |
1036 | |
1037 | |
1038 BasicType Reflection::basic_type_mirror_to_basic_type(oop basic_type_mirror, TRAPS) { | |
1039 assert(java_lang_Class::is_primitive(basic_type_mirror), "just checking"); | |
1040 return java_lang_Class::primitive_type(basic_type_mirror); | |
1041 } | |
1042 | |
1043 | |
1044 bool Reflection::match_parameter_types(methodHandle method, objArrayHandle types, int parameter_count, TRAPS) { | |
1045 int types_len = types.is_null() ? 0 : types->length(); | |
1046 if (types_len != parameter_count) return false; | |
1047 if (parameter_count > 0) { | |
1048 objArrayHandle method_types = get_parameter_types(method, parameter_count, NULL, CHECK_false); | |
1049 for (int index = 0; index < parameter_count; index++) { | |
1050 if (types->obj_at(index) != method_types->obj_at(index)) { | |
1051 return false; | |
1052 } | |
1053 } | |
1054 } | |
1055 return true; | |
1056 } | |
1057 | |
1058 | |
1059 oop Reflection::new_field(FieldStream* st, TRAPS) { | |
1060 symbolHandle field_name(THREAD, st->name()); | |
1061 Handle name = java_lang_String::create_from_symbol(field_name, CHECK_NULL); | |
1062 symbolHandle signature(THREAD, st->signature()); | |
1063 Handle type = new_type(signature, st->klass(), CHECK_NULL); | |
1064 Handle rh = java_lang_reflect_Field::create(CHECK_NULL); | |
1065 oop result = rh(); | |
1066 | |
1067 java_lang_reflect_Field::set_clazz(result, st->klass()->java_mirror()); | |
1068 java_lang_reflect_Field::set_slot(result, st->index()); | |
1069 java_lang_reflect_Field::set_name(result, name()); | |
1070 java_lang_reflect_Field::set_type(result, type()); | |
1071 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here. | |
1072 java_lang_reflect_Field::set_modifiers(result, st->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS); | |
1073 java_lang_reflect_Field::set_override(result, false); | |
1074 return result; | |
1075 } | |
1076 | |
1077 | |
1078 bool Reflection::resolve_field(Handle field_mirror, Handle& receiver, fieldDescriptor* fd, bool check_final, TRAPS) { | |
1079 if (field_mirror.is_null()) { | |
1080 THROW_(vmSymbols::java_lang_NullPointerException(), false); | |
1081 } | |
1082 | |
1083 instanceKlassHandle klass (THREAD, java_lang_Class::as_klassOop(java_lang_reflect_Field::clazz(field_mirror()))); | |
1084 int slot = java_lang_reflect_Field::slot(field_mirror()); | |
1085 | |
1086 // Ensure klass is initialized | |
1087 klass->initialize(CHECK_false); | |
1088 fd->initialize(klass(), slot); | |
1089 | |
1090 bool is_static = fd->is_static(); | |
1091 KlassHandle receiver_klass; | |
1092 | |
1093 if (is_static) { | |
1094 receiver = KlassHandle(THREAD, klass()); | |
1095 receiver_klass = klass; | |
1096 } else { | |
1097 // Check object is a non-null instance of declaring class | |
1098 if (receiver.is_null()) { | |
1099 THROW_(vmSymbols::java_lang_NullPointerException(), false); | |
1100 } | |
1101 if (!receiver->is_a(klass())) { | |
1102 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class", false); | |
1103 } | |
1104 receiver_klass = KlassHandle(THREAD, receiver->klass()); | |
1105 } | |
1106 | |
1107 // Access checking (unless overridden by Field) | |
1108 if (!java_lang_reflect_Field::override(field_mirror())) { | |
1109 if (!(klass->is_public() && fd->is_public())) { | |
1110 bool access_check = reflect_check_access(klass(), fd->access_flags(), receiver_klass(), false, CHECK_false); | |
1111 if (!access_check) { | |
1112 return false; // exception | |
1113 } | |
1114 } | |
1115 } | |
1116 | |
1117 if (check_final && fd->is_final()) { | |
1118 // In 1.3 we always throw an error when attempting to set a final field. | |
1119 // In 1.2.x, this was allowed in the override bit was set by calling Field.setAccessible(true). | |
1120 // We currently maintain backwards compatibility. See bug 4250960. | |
1121 bool strict_final_check = !JDK_Version::is_jdk12x_version(); | |
1122 if (strict_final_check || !java_lang_reflect_Field::override(field_mirror())) { | |
1123 THROW_MSG_(vmSymbols::java_lang_IllegalAccessException(), "field is final", false); | |
1124 } | |
1125 } | |
1126 return true; | |
1127 } | |
1128 | |
1129 | |
1130 BasicType Reflection::field_get(jvalue* value, fieldDescriptor* fd, Handle receiver) { | |
1131 BasicType field_type = fd->field_type(); | |
1132 int offset = fd->offset(); | |
1133 switch (field_type) { | |
1134 case T_BOOLEAN: | |
1135 value->z = receiver->bool_field(offset); | |
1136 break; | |
1137 case T_CHAR: | |
1138 value->c = receiver->char_field(offset); | |
1139 break; | |
1140 case T_FLOAT: | |
1141 value->f = receiver->float_field(offset); | |
1142 break; | |
1143 case T_DOUBLE: | |
1144 value->d = receiver->double_field(offset); | |
1145 break; | |
1146 case T_BYTE: | |
1147 value->b = receiver->byte_field(offset); | |
1148 break; | |
1149 case T_SHORT: | |
1150 value->s = receiver->short_field(offset); | |
1151 break; | |
1152 case T_INT: | |
1153 value->i = receiver->int_field(offset); | |
1154 break; | |
1155 case T_LONG: | |
1156 value->j = receiver->long_field(offset); | |
1157 break; | |
1158 case T_OBJECT: | |
1159 case T_ARRAY: | |
1160 value->l = (jobject) receiver->obj_field(offset); | |
1161 break; | |
1162 default: | |
1163 return T_ILLEGAL; | |
1164 } | |
1165 return field_type; | |
1166 } | |
1167 | |
1168 | |
1169 void Reflection::field_set(jvalue* value, fieldDescriptor* fd, Handle receiver, BasicType value_type, TRAPS) { | |
1170 BasicType field_type = fd->field_type(); | |
1171 if (field_type != value_type) { | |
1172 widen(value, value_type, field_type, CHECK); | |
1173 } | |
1174 | |
1175 int offset = fd->offset(); | |
1176 switch (field_type) { | |
1177 case T_BOOLEAN: | |
1178 receiver->bool_field_put(offset, value->z); | |
1179 break; | |
1180 case T_CHAR: | |
1181 receiver->char_field_put(offset, value->c); | |
1182 break; | |
1183 case T_FLOAT: | |
1184 receiver->float_field_put(offset, value->f); | |
1185 break; | |
1186 case T_DOUBLE: | |
1187 receiver->double_field_put(offset, value->d); | |
1188 break; | |
1189 case T_BYTE: | |
1190 receiver->byte_field_put(offset, value->b); | |
1191 break; | |
1192 case T_SHORT: | |
1193 receiver->short_field_put(offset, value->s); | |
1194 break; | |
1195 case T_INT: | |
1196 receiver->int_field_put(offset, value->i); | |
1197 break; | |
1198 case T_LONG: | |
1199 receiver->long_field_put(offset, value->j); | |
1200 break; | |
1201 case T_OBJECT: | |
1202 case T_ARRAY: { | |
1203 Handle obj(THREAD, (oop) value->l); | |
1204 if (obj.not_null()) { | |
1205 symbolHandle signature(THREAD, fd->signature()); | |
1206 Handle loader (THREAD, fd->loader()); | |
1207 Handle protect (THREAD, Klass::cast(fd->field_holder())->protection_domain()); | |
1208 klassOop k = SystemDictionary::resolve_or_fail(signature, loader, protect, true, CHECK); // may block | |
1209 if (!obj->is_a(k)) { | |
1210 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "field type mismatch"); | |
1211 } | |
1212 } | |
1213 receiver->obj_field_put(offset, obj()); | |
1214 break; | |
1215 } | |
1216 default: | |
1217 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "field type mismatch"); | |
1218 } | |
1219 } | |
1220 | |
1221 | |
1222 oop Reflection::reflect_field(oop mirror, symbolOop field_name, jint which, TRAPS) { | |
1223 // Exclude primitive types and array types | |
1224 if (java_lang_Class::is_primitive(mirror)) return NULL; | |
1225 if (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) return NULL; | |
1226 | |
1227 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(mirror)); | |
1228 bool local_fields_only = (which == DECLARED); | |
1229 | |
1230 // Ensure class is linked | |
1231 k->link_class(CHECK_NULL); | |
1232 | |
1233 // Search class and interface fields | |
1234 for (FieldStream st(k, local_fields_only, false); !st.eos(); st.next()) { | |
1235 if (st.name() == field_name) { | |
1236 if (local_fields_only || st.access_flags().is_public()) { | |
1237 return new_field(&st, THREAD); | |
1238 } | |
1239 } | |
1240 } | |
1241 | |
1242 return NULL; | |
1243 } | |
1244 | |
1245 | |
1246 objArrayOop Reflection::reflect_fields(oop mirror, jint which, TRAPS) { | |
1247 // Exclude primitive types and array types | |
1248 if (java_lang_Class::is_primitive(mirror) | |
1249 || Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) { | |
1250 symbolHandle name = vmSymbolHandles::java_lang_reflect_Field(); | |
1251 klassOop klass = SystemDictionary::resolve_or_fail(name, true, CHECK_NULL); | |
1252 return oopFactory::new_objArray(klass, 0, CHECK_NULL); // Return empty array | |
1253 } | |
1254 | |
1255 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(mirror)); | |
1256 | |
1257 // Ensure class is linked | |
1258 k->link_class(CHECK_NULL); | |
1259 | |
1260 bool local_fields_only = (which == DECLARED); | |
1261 int count = 0; | |
1262 { // Compute fields count for class and interface fields | |
1263 for (FieldStream st(k, local_fields_only, false); !st.eos(); st.next()) { | |
1264 if (local_fields_only || st.access_flags().is_public()) { | |
1265 count++; | |
1266 } | |
1267 } | |
1268 } | |
1269 | |
1270 // Allocate result | |
1271 symbolHandle name = vmSymbolHandles::java_lang_reflect_Field(); | |
1272 klassOop klass = SystemDictionary::resolve_or_fail(name, true, CHECK_NULL); | |
1273 objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL); | |
1274 objArrayHandle result (THREAD, r); | |
1275 | |
1276 // Fill in results backwards | |
1277 { | |
1278 for (FieldStream st(k, local_fields_only, false); !st.eos(); st.next()) { | |
1279 if (local_fields_only || st.access_flags().is_public()) { | |
1280 oop field = new_field(&st, CHECK_NULL); | |
1281 result->obj_at_put(--count, field); | |
1282 } | |
1283 } | |
1284 assert(count == 0, "just checking"); | |
1285 } | |
1286 return result(); | |
1287 } | |
1288 | |
1289 | |
1290 oop Reflection::reflect_method(oop mirror, symbolHandle method_name, objArrayHandle types, jint which, TRAPS) { | |
1291 if (java_lang_Class::is_primitive(mirror)) return NULL; | |
1292 klassOop klass = java_lang_Class::as_klassOop(mirror); | |
1293 if (Klass::cast(klass)->oop_is_array() && which == MEMBER_DECLARED) return NULL; | |
1294 | |
1295 if (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) { | |
1296 klass = SystemDictionary::object_klass(); | |
1297 } | |
1298 instanceKlassHandle h_k(THREAD, klass); | |
1299 | |
1300 // Ensure klass is linked (need not be initialized) | |
1301 h_k->link_class(CHECK_NULL); | |
1302 | |
1303 // For interfaces include static initializers under jdk1.2.x (since classic does that) | |
1304 bool include_clinit = JDK_Version::is_jdk12x_version() && h_k->is_interface(); | |
1305 | |
1306 switch (which) { | |
1307 case MEMBER_PUBLIC: | |
1308 // First the public non-static methods (works if method holder is an interface) | |
1309 // Note that we can ignore checks for overridden methods, since we go up the hierarchy. | |
1310 { | |
1311 for (MethodStream st(h_k, false, false); !st.eos(); st.next()) { | |
1312 methodHandle m(THREAD, st.method()); | |
1313 // For interfaces include static initializers since classic does that! | |
1314 if (method_name() == m->name() && (include_clinit || (m->is_public() && !m->is_static() && !m->is_initializer()))) { | |
1315 symbolHandle signature(THREAD, m->signature()); | |
1316 bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL); | |
1317 if (parameter_match) { | |
1318 return new_method(m, false, false, THREAD); | |
1319 } | |
1320 } | |
1321 } | |
1322 } | |
1323 // Then the public static methods (works if method holder is an interface) | |
1324 { | |
1325 for (MethodStream st(h_k, false, false); !st.eos(); st.next()) { | |
1326 methodHandle m(THREAD, st.method()); | |
1327 if (method_name() == m->name() && m->is_public() && m->is_static() && !m->is_initializer()) { | |
1328 symbolHandle signature(THREAD, m->signature()); | |
1329 bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL); | |
1330 if (parameter_match) { | |
1331 return new_method(m, false, false, THREAD); | |
1332 } | |
1333 } | |
1334 } | |
1335 } | |
1336 break; | |
1337 case MEMBER_DECLARED: | |
1338 // All local methods | |
1339 { | |
1340 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1341 methodHandle m(THREAD, st.method()); | |
1342 if (method_name() == m->name() && !m->is_initializer()) { | |
1343 symbolHandle signature(THREAD, m->signature()); | |
1344 bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL); | |
1345 if (parameter_match) { | |
1346 return new_method(m, false, false, THREAD); | |
1347 } | |
1348 } | |
1349 } | |
1350 } | |
1351 break; | |
1352 default: | |
1353 break; | |
1354 } | |
1355 return NULL; | |
1356 } | |
1357 | |
1358 | |
1359 objArrayOop Reflection::reflect_methods(oop mirror, jint which, TRAPS) { | |
1360 // Exclude primitive types | |
1361 if (java_lang_Class::is_primitive(mirror) || | |
1362 (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array() && (which == MEMBER_DECLARED))) { | |
1363 klassOop klass = SystemDictionary::reflect_method_klass(); | |
1364 return oopFactory::new_objArray(klass, 0, CHECK_NULL); // Return empty array | |
1365 } | |
1366 | |
1367 klassOop klass = java_lang_Class::as_klassOop(mirror); | |
1368 if (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) { | |
1369 klass = SystemDictionary::object_klass(); | |
1370 } | |
1371 instanceKlassHandle h_k(THREAD, klass); | |
1372 | |
1373 // Ensure klass is linked (need not be initialized) | |
1374 h_k->link_class(CHECK_NULL); | |
1375 | |
1376 // We search the (super)interfaces only if h_k is an interface itself | |
1377 bool is_interface = h_k->is_interface(); | |
1378 | |
1379 // For interfaces include static initializers under jdk1.2.x (since classic does that) | |
1380 bool include_clinit = JDK_Version::is_jdk12x_version() && is_interface; | |
1381 | |
1382 switch (which) { | |
1383 case MEMBER_PUBLIC: | |
1384 { | |
1385 | |
1386 // Count public methods (non-static and static) | |
1387 int count = 0; | |
1388 { | |
1389 for (MethodStream st(h_k, false, false); !st.eos(); st.next()) { | |
1390 methodOop m = st.method(); | |
1391 // For interfaces include static initializers since classic does that! | |
1392 if (include_clinit || (!m->is_initializer() && m->is_public() && !m->is_overridden_in(h_k()))) { | |
1393 count++; | |
1394 } | |
1395 } | |
1396 } | |
1397 | |
1398 // Allocate result | |
1399 klassOop klass = SystemDictionary::reflect_method_klass(); | |
1400 objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL); | |
1401 objArrayHandle h_result (THREAD, r); | |
1402 | |
1403 // Fill in results backwards | |
1404 { | |
1405 // First the non-static public methods | |
1406 for (MethodStream st(h_k, false, false); !st.eos(); st.next()) { | |
1407 methodHandle m (THREAD, st.method()); | |
1408 if (!m->is_static() && !m->is_initializer() && m->is_public() && !m->is_overridden_in(h_k())) { | |
1409 oop method = new_method(m, false, false, CHECK_NULL); | |
1410 if (method == NULL) { | |
1411 return NULL; | |
1412 } else { | |
1413 h_result->obj_at_put(--count, method); | |
1414 } | |
1415 } | |
1416 } | |
1417 } | |
1418 { | |
1419 // Then the static public methods | |
1420 for (MethodStream st(h_k, false, !is_interface); !st.eos(); st.next()) { | |
1421 methodHandle m (THREAD, st.method()); | |
1422 if (m->is_static() && (include_clinit || (!m->is_initializer()) && m->is_public() && !m->is_overridden_in(h_k()))) { | |
1423 oop method = new_method(m, false, false, CHECK_NULL); | |
1424 if (method == NULL) { | |
1425 return NULL; | |
1426 } else { | |
1427 h_result->obj_at_put(--count, method); | |
1428 } | |
1429 } | |
1430 } | |
1431 } | |
1432 | |
1433 assert(count == 0, "just checking"); | |
1434 return h_result(); | |
1435 } | |
1436 | |
1437 case MEMBER_DECLARED: | |
1438 { | |
1439 // Count all methods | |
1440 int count = 0; | |
1441 { | |
1442 for (MethodStream st(h_k, true, !is_interface); !st.eos(); st.next()) { | |
1443 methodOop m = st.method(); | |
1444 if (!m->is_initializer()) { | |
1445 count++; | |
1446 } | |
1447 } | |
1448 } | |
1449 // Allocate result | |
1450 klassOop klass = SystemDictionary::reflect_method_klass(); | |
1451 objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL); | |
1452 objArrayHandle h_result (THREAD, r); | |
1453 | |
1454 // Fill in results backwards | |
1455 { | |
1456 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1457 methodHandle m (THREAD, st.method()); | |
1458 if (!m->is_initializer()) { | |
1459 oop method = new_method(m, false, false, CHECK_NULL); | |
1460 if (method == NULL) { | |
1461 return NULL; | |
1462 } else { | |
1463 h_result->obj_at_put(--count, method); | |
1464 } | |
1465 } | |
1466 } | |
1467 } | |
1468 assert(count == 0, "just checking"); | |
1469 return h_result(); | |
1470 } | |
1471 } | |
1472 ShouldNotReachHere(); | |
1473 return NULL; | |
1474 } | |
1475 | |
1476 | |
1477 oop Reflection::reflect_constructor(oop mirror, objArrayHandle types, jint which, TRAPS) { | |
1478 | |
1479 // Exclude primitive, interface and array types | |
1480 bool prim = java_lang_Class::is_primitive(mirror); | |
1481 Klass* klass = prim ? NULL : Klass::cast(java_lang_Class::as_klassOop(mirror)); | |
1482 if (prim || klass->is_interface() || klass->oop_is_array()) return NULL; | |
1483 | |
1484 // Must be instance klass | |
1485 instanceKlassHandle h_k(THREAD, java_lang_Class::as_klassOop(mirror)); | |
1486 | |
1487 // Ensure klass is linked (need not be initialized) | |
1488 h_k->link_class(CHECK_NULL); | |
1489 | |
1490 bool local_only = (which == MEMBER_DECLARED); | |
1491 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1492 methodHandle m(THREAD, st.method()); | |
1493 if (m->name() == vmSymbols::object_initializer_name() && (local_only || m->is_public())) { | |
1494 symbolHandle signature(THREAD, m->signature()); | |
1495 bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL); | |
1496 if (parameter_match) { | |
1497 return new_constructor(m, THREAD); | |
1498 } | |
1499 } | |
1500 } | |
1501 | |
1502 return NULL; | |
1503 } | |
1504 | |
1505 | |
1506 objArrayOop Reflection::reflect_constructors(oop mirror, jint which, TRAPS) { | |
1507 // Exclude primitive, interface and array types | |
1508 bool prim = java_lang_Class::is_primitive(mirror); | |
1509 Klass* k = prim ? NULL : Klass::cast(java_lang_Class::as_klassOop(mirror)); | |
1510 if (prim || k->is_interface() || k->oop_is_array()) { | |
1511 return oopFactory::new_objArray(SystemDictionary::reflect_constructor_klass(), 0, CHECK_NULL); // Return empty array | |
1512 } | |
1513 | |
1514 // Must be instanceKlass at this point | |
1515 instanceKlassHandle h_k(THREAD, java_lang_Class::as_klassOop(mirror)); | |
1516 | |
1517 // Ensure klass is linked (need not be initialized) | |
1518 h_k->link_class(CHECK_NULL); | |
1519 | |
1520 bool local_only = (which == MEMBER_DECLARED); | |
1521 int count = 0; | |
1522 { | |
1523 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1524 methodOop m = st.method(); | |
1525 if (m->name() == vmSymbols::object_initializer_name() && (local_only || m->is_public())) { | |
1526 count++; | |
1527 } | |
1528 } | |
1529 } | |
1530 | |
1531 // Allocate result | |
1532 symbolHandle name = vmSymbolHandles::java_lang_reflect_Constructor(); | |
1533 klassOop klass = SystemDictionary::resolve_or_fail(name, true, CHECK_NULL); | |
1534 objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL); | |
1535 objArrayHandle h_result (THREAD, r); | |
1536 | |
1537 // Fill in results backwards | |
1538 { | |
1539 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1540 methodHandle m (THREAD, st.method()); | |
1541 if (m->name() == vmSymbols::object_initializer_name() && (local_only || m->is_public())) { | |
1542 oop constr = new_constructor(m, CHECK_NULL); | |
1543 if (constr == NULL) { | |
1544 return NULL; | |
1545 } else { | |
1546 h_result->obj_at_put(--count, constr); | |
1547 } | |
1548 } | |
1549 } | |
1550 assert(count == 0, "just checking"); | |
1551 } | |
1552 return h_result(); | |
1553 } | |
1554 | |
1555 | |
1556 // This would be nicer if, say, java.lang.reflect.Method was a subclass | |
1557 // of java.lang.reflect.Constructor | |
1558 | |
1559 oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) { | |
1560 oop mirror = java_lang_reflect_Method::clazz(method_mirror); | |
1561 int slot = java_lang_reflect_Method::slot(method_mirror); | |
1562 bool override = java_lang_reflect_Method::override(method_mirror) != 0; | |
1563 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror))); | |
1564 | |
1565 oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror); | |
1566 BasicType rtype; | |
1567 if (java_lang_Class::is_primitive(return_type_mirror)) { | |
1568 rtype = basic_type_mirror_to_basic_type(return_type_mirror, CHECK_NULL); | |
1569 } else { | |
1570 rtype = T_OBJECT; | |
1571 } | |
1572 | |
1573 instanceKlassHandle klass(THREAD, java_lang_Class::as_klassOop(mirror)); | |
51
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1574 methodOop m = klass->method_with_idnum(slot); |
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1575 if (m == NULL) { |
0 | 1576 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke"); |
1577 } | |
51
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1578 methodHandle method(THREAD, m); |
0 | 1579 |
1580 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD); | |
1581 } | |
1582 | |
1583 | |
1584 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) { | |
1585 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror); | |
1586 int slot = java_lang_reflect_Constructor::slot(constructor_mirror); | |
1587 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0; | |
1588 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror))); | |
1589 | |
1590 instanceKlassHandle klass(THREAD, java_lang_Class::as_klassOop(mirror)); | |
51
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1591 methodOop m = klass->method_with_idnum(slot); |
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1592 if (m == NULL) { |
0 | 1593 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke"); |
1594 } | |
51
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1595 methodHandle method(THREAD, m); |
0 | 1596 assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor"); |
1597 | |
1598 // Make sure klass gets initialize | |
1599 klass->initialize(CHECK_NULL); | |
1600 | |
1601 // Create new instance (the receiver) | |
1602 klass->check_valid_for_instantiation(false, CHECK_NULL); | |
1603 Handle receiver = klass->allocate_instance_handle(CHECK_NULL); | |
1604 | |
1605 // Ignore result from call and return receiver | |
1606 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL); | |
1607 return receiver(); | |
1608 } | |
1609 | |
1610 | |
1611 #endif /* SUPPORT_OLD_REFLECTION */ |