Mercurial > hg > truffle
annotate src/share/vm/runtime/reflection.cpp @ 991:3a2aa26bdc58
6854812: 6.0_14-b08 crashes with a SIGSEGV
Reviewed-by: kvn, twisti
author | never |
---|---|
date | Wed, 16 Sep 2009 11:06:10 -0700 |
parents | c89f86385056 |
children | 4ce7240d622c |
rev | line source |
---|---|
0 | 1 /* |
665
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
2 * Copyright 1997-2009 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 | |
665
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
557 bool Reflection::is_same_package_member(klassOop class1, klassOop class2, TRAPS) { |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
558 return instanceKlass::cast(class1)->is_same_package_member(class2, THREAD); |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
559 } |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
560 |
0 | 561 |
562 // Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not, | |
563 // throw an incompatible class change exception | |
665
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
564 // If inner_is_member, require the inner to be a member of the outer. |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
565 // If !inner_is_member, require the inner to be anonymous (a non-member). |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
566 // Caller is responsible for figuring out in advance which case must be true. |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
567 void Reflection::check_for_inner_class(instanceKlassHandle outer, instanceKlassHandle inner, |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
568 bool inner_is_member, TRAPS) { |
0 | 569 const int inner_class_info_index = 0; |
570 const int outer_class_info_index = 1; | |
571 | |
572 typeArrayHandle icls (THREAD, outer->inner_classes()); | |
573 constantPoolHandle cp (THREAD, outer->constants()); | |
574 for(int i = 0; i < icls->length(); i += 4) { | |
575 int ioff = icls->ushort_at(i + inner_class_info_index); | |
576 int ooff = icls->ushort_at(i + outer_class_info_index); | |
577 | |
665
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
578 if (inner_is_member && ioff != 0 && ooff != 0) { |
0 | 579 klassOop o = cp->klass_at(ooff, CHECK); |
580 if (o == outer()) { | |
581 klassOop i = cp->klass_at(ioff, CHECK); | |
582 if (i == inner()) { | |
583 return; | |
584 } | |
585 } | |
586 } | |
665
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
587 if (!inner_is_member && ioff != 0 && ooff == 0 && |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
588 cp->klass_name_at_matches(inner, ioff)) { |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
589 klassOop i = cp->klass_at(ioff, CHECK); |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
590 if (i == inner()) { |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
591 return; |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
592 } |
c89f86385056
6814659: separable cleanups and subroutines for 6655638
jrose
parents:
431
diff
changeset
|
593 } |
0 | 594 } |
595 | |
596 // 'inner' not declared as an inner klass in outer | |
597 ResourceMark rm(THREAD); | |
598 Exceptions::fthrow( | |
599 THREAD_AND_LOCATION, | |
600 vmSymbolHandles::java_lang_IncompatibleClassChangeError(), | |
601 "%s and %s disagree on InnerClasses attribute", | |
602 outer->external_name(), | |
603 inner->external_name() | |
604 ); | |
605 } | |
606 | |
607 // Utility method converting a single SignatureStream element into java.lang.Class instance | |
608 | |
609 oop get_mirror_from_signature(methodHandle method, SignatureStream* ss, TRAPS) { | |
610 switch (ss->type()) { | |
611 default: | |
612 assert(ss->type() != T_VOID || ss->at_return_type(), "T_VOID should only appear as return type"); | |
613 return java_lang_Class::primitive_mirror(ss->type()); | |
614 case T_OBJECT: | |
615 case T_ARRAY: | |
616 symbolOop name = ss->as_symbol(CHECK_NULL); | |
617 oop loader = instanceKlass::cast(method->method_holder())->class_loader(); | |
618 oop protection_domain = instanceKlass::cast(method->method_holder())->protection_domain(); | |
619 klassOop k = SystemDictionary::resolve_or_fail( | |
620 symbolHandle(THREAD, name), | |
621 Handle(THREAD, loader), | |
622 Handle(THREAD, protection_domain), | |
623 true, CHECK_NULL); | |
624 if (TraceClassResolution) { | |
625 trace_class_resolution(k); | |
626 } | |
627 return k->klass_part()->java_mirror(); | |
628 }; | |
629 } | |
630 | |
631 | |
632 objArrayHandle Reflection::get_parameter_types(methodHandle method, int parameter_count, oop* return_type, TRAPS) { | |
633 // Allocate array holding parameter types (java.lang.Class instances) | |
634 objArrayOop m = oopFactory::new_objArray(SystemDictionary::class_klass(), parameter_count, CHECK_(objArrayHandle())); | |
635 objArrayHandle mirrors (THREAD, m); | |
636 int index = 0; | |
637 // Collect parameter types | |
638 symbolHandle signature (THREAD, method->signature()); | |
639 SignatureStream ss(signature); | |
640 while (!ss.at_return_type()) { | |
641 oop mirror = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle())); | |
642 mirrors->obj_at_put(index++, mirror); | |
643 ss.next(); | |
644 } | |
645 assert(index == parameter_count, "invalid parameter count"); | |
646 if (return_type != NULL) { | |
647 // Collect return type as well | |
648 assert(ss.at_return_type(), "return type should be present"); | |
649 *return_type = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle())); | |
650 } | |
651 return mirrors; | |
652 } | |
653 | |
654 objArrayHandle Reflection::get_exception_types(methodHandle method, TRAPS) { | |
655 return method->resolved_checked_exceptions(CHECK_(objArrayHandle())); | |
656 } | |
657 | |
658 | |
659 Handle Reflection::new_type(symbolHandle signature, KlassHandle k, TRAPS) { | |
660 // Basic types | |
661 BasicType type = vmSymbols::signature_type(signature()); | |
662 if (type != T_OBJECT) { | |
663 return Handle(THREAD, Universe::java_mirror(type)); | |
664 } | |
665 | |
666 oop loader = instanceKlass::cast(k())->class_loader(); | |
667 oop protection_domain = Klass::cast(k())->protection_domain(); | |
668 klassOop result = SystemDictionary::resolve_or_fail(signature, | |
669 Handle(THREAD, loader), | |
670 Handle(THREAD, protection_domain), | |
671 true, CHECK_(Handle())); | |
672 | |
673 if (TraceClassResolution) { | |
674 trace_class_resolution(result); | |
675 } | |
676 | |
677 oop nt = Klass::cast(result)->java_mirror(); | |
678 return Handle(THREAD, nt); | |
679 } | |
680 | |
681 | |
682 oop Reflection::new_method(methodHandle method, bool intern_name, bool for_constant_pool_access, TRAPS) { | |
683 // In jdk1.2.x, getMethods on an interface erroneously includes <clinit>, thus the complicated assert. | |
684 // Also allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods. | |
685 assert(!method()->is_initializer() || | |
686 (for_constant_pool_access && method()->is_static()) || | |
687 (method()->name() == vmSymbols::class_initializer_name() | |
688 && Klass::cast(method()->method_holder())->is_interface() && JDK_Version::is_jdk12x_version()), "should call new_constructor instead"); | |
689 instanceKlassHandle holder (THREAD, method->method_holder()); | |
690 int slot = method->method_idnum(); | |
691 | |
692 symbolHandle signature (THREAD, method->signature()); | |
693 int parameter_count = ArgumentCount(signature).size(); | |
694 oop return_type_oop = NULL; | |
695 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL); | |
696 if (parameter_types.is_null() || return_type_oop == NULL) return NULL; | |
697 | |
698 Handle return_type(THREAD, return_type_oop); | |
699 | |
700 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL); | |
701 | |
702 if (exception_types.is_null()) return NULL; | |
703 | |
704 symbolHandle method_name(THREAD, method->name()); | |
705 Handle name; | |
706 if (intern_name) { | |
707 // intern_name is only true with UseNewReflection | |
708 oop name_oop = StringTable::intern(method_name(), CHECK_NULL); | |
709 name = Handle(THREAD, name_oop); | |
710 } else { | |
711 name = java_lang_String::create_from_symbol(method_name, CHECK_NULL); | |
712 } | |
713 if (name.is_null()) return NULL; | |
714 | |
715 int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; | |
716 | |
717 Handle mh = java_lang_reflect_Method::create(CHECK_NULL); | |
718 | |
719 java_lang_reflect_Method::set_clazz(mh(), holder->java_mirror()); | |
720 java_lang_reflect_Method::set_slot(mh(), slot); | |
721 java_lang_reflect_Method::set_name(mh(), name()); | |
722 java_lang_reflect_Method::set_return_type(mh(), return_type()); | |
723 java_lang_reflect_Method::set_parameter_types(mh(), parameter_types()); | |
724 java_lang_reflect_Method::set_exception_types(mh(), exception_types()); | |
725 java_lang_reflect_Method::set_modifiers(mh(), modifiers); | |
726 java_lang_reflect_Method::set_override(mh(), false); | |
727 if (java_lang_reflect_Method::has_signature_field() && | |
728 method->generic_signature() != NULL) { | |
729 symbolHandle gs(THREAD, method->generic_signature()); | |
730 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL); | |
731 java_lang_reflect_Method::set_signature(mh(), sig()); | |
732 } | |
733 if (java_lang_reflect_Method::has_annotations_field()) { | |
734 java_lang_reflect_Method::set_annotations(mh(), method->annotations()); | |
735 } | |
736 if (java_lang_reflect_Method::has_parameter_annotations_field()) { | |
737 java_lang_reflect_Method::set_parameter_annotations(mh(), method->parameter_annotations()); | |
738 } | |
739 if (java_lang_reflect_Method::has_annotation_default_field()) { | |
740 java_lang_reflect_Method::set_annotation_default(mh(), method->annotation_default()); | |
741 } | |
742 return mh(); | |
743 } | |
744 | |
745 | |
746 oop Reflection::new_constructor(methodHandle method, TRAPS) { | |
747 assert(method()->is_initializer(), "should call new_method instead"); | |
748 | |
749 instanceKlassHandle holder (THREAD, method->method_holder()); | |
750 int slot = method->method_idnum(); | |
751 | |
752 symbolHandle signature (THREAD, method->signature()); | |
753 int parameter_count = ArgumentCount(signature).size(); | |
754 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, NULL, CHECK_NULL); | |
755 if (parameter_types.is_null()) return NULL; | |
756 | |
757 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL); | |
758 if (exception_types.is_null()) return NULL; | |
759 | |
760 int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; | |
761 | |
762 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL); | |
763 | |
764 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror()); | |
765 java_lang_reflect_Constructor::set_slot(ch(), slot); | |
766 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types()); | |
767 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types()); | |
768 java_lang_reflect_Constructor::set_modifiers(ch(), modifiers); | |
769 java_lang_reflect_Constructor::set_override(ch(), false); | |
770 if (java_lang_reflect_Constructor::has_signature_field() && | |
771 method->generic_signature() != NULL) { | |
772 symbolHandle gs(THREAD, method->generic_signature()); | |
773 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL); | |
774 java_lang_reflect_Constructor::set_signature(ch(), sig()); | |
775 } | |
776 if (java_lang_reflect_Constructor::has_annotations_field()) { | |
777 java_lang_reflect_Constructor::set_annotations(ch(), method->annotations()); | |
778 } | |
779 if (java_lang_reflect_Constructor::has_parameter_annotations_field()) { | |
780 java_lang_reflect_Constructor::set_parameter_annotations(ch(), method->parameter_annotations()); | |
781 } | |
782 return ch(); | |
783 } | |
784 | |
785 | |
786 oop Reflection::new_field(fieldDescriptor* fd, bool intern_name, TRAPS) { | |
787 symbolHandle field_name(THREAD, fd->name()); | |
788 Handle name; | |
789 if (intern_name) { | |
790 // intern_name is only true with UseNewReflection | |
791 oop name_oop = StringTable::intern(field_name(), CHECK_NULL); | |
792 name = Handle(THREAD, name_oop); | |
793 } else { | |
794 name = java_lang_String::create_from_symbol(field_name, CHECK_NULL); | |
795 } | |
796 symbolHandle signature (THREAD, fd->signature()); | |
797 KlassHandle holder (THREAD, fd->field_holder()); | |
798 Handle type = new_type(signature, holder, CHECK_NULL); | |
799 Handle rh = java_lang_reflect_Field::create(CHECK_NULL); | |
800 | |
801 java_lang_reflect_Field::set_clazz(rh(), Klass::cast(fd->field_holder())->java_mirror()); | |
802 java_lang_reflect_Field::set_slot(rh(), fd->index()); | |
803 java_lang_reflect_Field::set_name(rh(), name()); | |
804 java_lang_reflect_Field::set_type(rh(), type()); | |
805 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here. | |
806 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS); | |
807 java_lang_reflect_Field::set_override(rh(), false); | |
808 if (java_lang_reflect_Field::has_signature_field() && | |
809 fd->generic_signature() != NULL) { | |
810 symbolHandle gs(THREAD, fd->generic_signature()); | |
811 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL); | |
812 java_lang_reflect_Field::set_signature(rh(), sig()); | |
813 } | |
814 if (java_lang_reflect_Field::has_annotations_field()) { | |
815 java_lang_reflect_Field::set_annotations(rh(), fd->annotations()); | |
816 } | |
817 return rh(); | |
818 } | |
819 | |
820 | |
821 //--------------------------------------------------------------------------- | |
822 // | |
823 // Supporting routines for old native code-based reflection (pre-JDK 1.4). | |
824 // | |
825 // See reflection.hpp for details. | |
826 // | |
827 //--------------------------------------------------------------------------- | |
828 | |
829 #ifdef SUPPORT_OLD_REFLECTION | |
830 | |
831 methodHandle Reflection::resolve_interface_call(instanceKlassHandle klass, methodHandle method, | |
832 KlassHandle recv_klass, Handle receiver, TRAPS) { | |
833 assert(!method.is_null() , "method should not be null"); | |
834 | |
835 CallInfo info; | |
836 symbolHandle signature (THREAD, method->signature()); | |
837 symbolHandle name (THREAD, method->name()); | |
838 LinkResolver::resolve_interface_call(info, receiver, recv_klass, klass, | |
839 name, signature, | |
840 KlassHandle(), false, true, | |
841 CHECK_(methodHandle())); | |
842 return info.selected_method(); | |
843 } | |
844 | |
845 | |
846 oop Reflection::invoke(instanceKlassHandle klass, methodHandle reflected_method, | |
847 Handle receiver, bool override, objArrayHandle ptypes, | |
848 BasicType rtype, objArrayHandle args, bool is_method_invoke, TRAPS) { | |
849 ResourceMark rm(THREAD); | |
850 | |
851 methodHandle method; // actual method to invoke | |
852 KlassHandle target_klass; // target klass, receiver's klass for non-static | |
853 | |
854 // Ensure klass is initialized | |
855 klass->initialize(CHECK_NULL); | |
856 | |
857 bool is_static = reflected_method->is_static(); | |
858 if (is_static) { | |
859 // ignore receiver argument | |
860 method = reflected_method; | |
861 target_klass = klass; | |
862 } else { | |
863 // check for null receiver | |
864 if (receiver.is_null()) { | |
865 THROW_0(vmSymbols::java_lang_NullPointerException()); | |
866 } | |
867 // Check class of receiver against class declaring method | |
868 if (!receiver->is_a(klass())) { | |
869 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class"); | |
870 } | |
871 // target klass is receiver's klass | |
872 target_klass = KlassHandle(THREAD, receiver->klass()); | |
873 // no need to resolve if method is private or <init> | |
874 if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) { | |
875 method = reflected_method; | |
876 } else { | |
877 // resolve based on the receiver | |
878 if (instanceKlass::cast(reflected_method->method_holder())->is_interface()) { | |
879 // resolve interface call | |
880 if (ReflectionWrapResolutionErrors) { | |
881 // new default: 6531596 | |
882 // Match resolution errors with those thrown due to reflection inlining | |
883 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod() | |
884 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD); | |
885 if (HAS_PENDING_EXCEPTION) { | |
886 // Method resolution threw an exception; wrap it in an InvocationTargetException | |
887 oop resolution_exception = PENDING_EXCEPTION; | |
888 CLEAR_PENDING_EXCEPTION; | |
889 JavaCallArguments args(Handle(THREAD, resolution_exception)); | |
890 THROW_ARG_0(vmSymbolHandles::java_lang_reflect_InvocationTargetException(), | |
891 vmSymbolHandles::throwable_void_signature(), | |
892 &args); | |
893 } | |
894 } else { | |
895 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, CHECK_(NULL)); | |
896 } | |
897 } else { | |
898 // if the method can be overridden, we resolve using the vtable index. | |
899 int index = reflected_method->vtable_index(); | |
900 method = reflected_method; | |
901 if (index != methodOopDesc::nonvirtual_vtable_index) { | |
902 // target_klass might be an arrayKlassOop but all vtables start at | |
903 // the same place. The cast is to avoid virtual call and assertion. | |
904 instanceKlass* inst = (instanceKlass*)target_klass()->klass_part(); | |
905 method = methodHandle(THREAD, inst->method_at_vtable(index)); | |
906 } | |
907 if (!method.is_null()) { | |
908 // Check for abstract methods as well | |
909 if (method->is_abstract()) { | |
910 // new default: 6531596 | |
911 if (ReflectionWrapResolutionErrors) { | |
912 ResourceMark rm(THREAD); | |
913 Handle h_origexception = Exceptions::new_exception(THREAD, | |
914 vmSymbols::java_lang_AbstractMethodError(), | |
915 methodOopDesc::name_and_sig_as_C_string(Klass::cast(target_klass()), | |
916 method->name(), | |
917 method->signature())); | |
918 JavaCallArguments args(h_origexception); | |
919 THROW_ARG_0(vmSymbolHandles::java_lang_reflect_InvocationTargetException(), | |
920 vmSymbolHandles::throwable_void_signature(), | |
921 &args); | |
922 } else { | |
923 ResourceMark rm(THREAD); | |
924 THROW_MSG_0(vmSymbols::java_lang_AbstractMethodError(), | |
925 methodOopDesc::name_and_sig_as_C_string(Klass::cast(target_klass()), | |
926 method->name(), | |
927 method->signature())); | |
928 } | |
929 } | |
930 } | |
931 } | |
932 } | |
933 } | |
934 | |
935 // I believe this is a ShouldNotGetHere case which requires | |
936 // an internal vtable bug. If you ever get this please let Karen know. | |
937 if (method.is_null()) { | |
938 ResourceMark rm(THREAD); | |
939 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), | |
940 methodOopDesc::name_and_sig_as_C_string(Klass::cast(klass()), | |
941 reflected_method->name(), | |
942 reflected_method->signature())); | |
943 } | |
944 | |
945 // In the JDK 1.4 reflection implementation, the security check is | |
946 // done at the Java level | |
947 if (!(JDK_Version::is_gte_jdk14x_version() && UseNewReflection)) { | |
948 | |
949 // Access checking (unless overridden by Method) | |
950 if (!override) { | |
951 if (!(klass->is_public() && reflected_method->is_public())) { | |
952 bool access = Reflection::reflect_check_access(klass(), reflected_method->access_flags(), target_klass(), is_method_invoke, CHECK_NULL); | |
953 if (!access) { | |
954 return NULL; // exception | |
955 } | |
956 } | |
957 } | |
958 | |
959 } // !(Universe::is_gte_jdk14x_version() && UseNewReflection) | |
960 | |
961 assert(ptypes->is_objArray(), "just checking"); | |
962 int args_len = args.is_null() ? 0 : args->length(); | |
963 // Check number of arguments | |
964 if (ptypes->length() != args_len) { | |
965 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "wrong number of arguments"); | |
966 } | |
967 | |
968 // Create object to contain parameters for the JavaCall | |
969 JavaCallArguments java_args(method->size_of_parameters()); | |
970 | |
971 if (!is_static) { | |
972 java_args.push_oop(receiver); | |
973 } | |
974 | |
975 for (int i = 0; i < args_len; i++) { | |
976 oop type_mirror = ptypes->obj_at(i); | |
977 oop arg = args->obj_at(i); | |
978 if (java_lang_Class::is_primitive(type_mirror)) { | |
979 jvalue value; | |
980 BasicType ptype = basic_type_mirror_to_basic_type(type_mirror, CHECK_NULL); | |
981 BasicType atype = unbox_for_primitive(arg, &value, CHECK_NULL); | |
982 if (ptype != atype) { | |
983 widen(&value, atype, ptype, CHECK_NULL); | |
984 } | |
985 switch (ptype) { | |
986 case T_BOOLEAN: java_args.push_int(value.z); break; | |
987 case T_CHAR: java_args.push_int(value.c); break; | |
988 case T_BYTE: java_args.push_int(value.b); break; | |
989 case T_SHORT: java_args.push_int(value.s); break; | |
990 case T_INT: java_args.push_int(value.i); break; | |
991 case T_LONG: java_args.push_long(value.j); break; | |
992 case T_FLOAT: java_args.push_float(value.f); break; | |
993 case T_DOUBLE: java_args.push_double(value.d); break; | |
994 default: | |
995 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch"); | |
996 } | |
997 } else { | |
998 if (arg != NULL) { | |
999 klassOop k = java_lang_Class::as_klassOop(type_mirror); | |
1000 if (!arg->is_a(k)) { | |
1001 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch"); | |
1002 } | |
1003 } | |
1004 Handle arg_handle(THREAD, arg); // Create handle for argument | |
1005 java_args.push_oop(arg_handle); // Push handle | |
1006 } | |
1007 } | |
1008 | |
1009 assert(java_args.size_of_parameters() == method->size_of_parameters(), "just checking"); | |
1010 | |
1011 // All oops (including receiver) is passed in as Handles. An potential oop is returned as an | |
1012 // oop (i.e., NOT as an handle) | |
1013 JavaValue result(rtype); | |
1014 JavaCalls::call(&result, method, &java_args, THREAD); | |
1015 | |
1016 if (HAS_PENDING_EXCEPTION) { | |
1017 // Method threw an exception; wrap it in an InvocationTargetException | |
1018 oop target_exception = PENDING_EXCEPTION; | |
1019 CLEAR_PENDING_EXCEPTION; | |
1020 JavaCallArguments args(Handle(THREAD, target_exception)); | |
1021 THROW_ARG_0(vmSymbolHandles::java_lang_reflect_InvocationTargetException(), | |
1022 vmSymbolHandles::throwable_void_signature(), | |
1023 &args); | |
1024 } else { | |
1025 if (rtype == T_BOOLEAN || rtype == T_BYTE || rtype == T_CHAR || rtype == T_SHORT) | |
1026 narrow((jvalue*) result.get_value_addr(), rtype, CHECK_NULL); | |
1027 return box((jvalue*) result.get_value_addr(), rtype, CHECK_NULL); | |
1028 } | |
1029 } | |
1030 | |
1031 | |
1032 void Reflection::narrow(jvalue* value, BasicType narrow_type, TRAPS) { | |
1033 switch (narrow_type) { | |
1034 case T_BOOLEAN: | |
1035 value->z = (jboolean) value->i; | |
1036 return; | |
1037 case T_BYTE: | |
1038 value->b = (jbyte) value->i; | |
1039 return; | |
1040 case T_CHAR: | |
1041 value->c = (jchar) value->i; | |
1042 return; | |
1043 case T_SHORT: | |
1044 value->s = (jshort) value->i; | |
1045 return; | |
1046 default: | |
1047 break; // fail | |
1048 } | |
1049 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch"); | |
1050 } | |
1051 | |
1052 | |
1053 BasicType Reflection::basic_type_mirror_to_basic_type(oop basic_type_mirror, TRAPS) { | |
1054 assert(java_lang_Class::is_primitive(basic_type_mirror), "just checking"); | |
1055 return java_lang_Class::primitive_type(basic_type_mirror); | |
1056 } | |
1057 | |
1058 | |
1059 bool Reflection::match_parameter_types(methodHandle method, objArrayHandle types, int parameter_count, TRAPS) { | |
1060 int types_len = types.is_null() ? 0 : types->length(); | |
1061 if (types_len != parameter_count) return false; | |
1062 if (parameter_count > 0) { | |
1063 objArrayHandle method_types = get_parameter_types(method, parameter_count, NULL, CHECK_false); | |
1064 for (int index = 0; index < parameter_count; index++) { | |
1065 if (types->obj_at(index) != method_types->obj_at(index)) { | |
1066 return false; | |
1067 } | |
1068 } | |
1069 } | |
1070 return true; | |
1071 } | |
1072 | |
1073 | |
1074 oop Reflection::new_field(FieldStream* st, TRAPS) { | |
1075 symbolHandle field_name(THREAD, st->name()); | |
1076 Handle name = java_lang_String::create_from_symbol(field_name, CHECK_NULL); | |
1077 symbolHandle signature(THREAD, st->signature()); | |
1078 Handle type = new_type(signature, st->klass(), CHECK_NULL); | |
1079 Handle rh = java_lang_reflect_Field::create(CHECK_NULL); | |
1080 oop result = rh(); | |
1081 | |
1082 java_lang_reflect_Field::set_clazz(result, st->klass()->java_mirror()); | |
1083 java_lang_reflect_Field::set_slot(result, st->index()); | |
1084 java_lang_reflect_Field::set_name(result, name()); | |
1085 java_lang_reflect_Field::set_type(result, type()); | |
1086 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here. | |
1087 java_lang_reflect_Field::set_modifiers(result, st->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS); | |
1088 java_lang_reflect_Field::set_override(result, false); | |
1089 return result; | |
1090 } | |
1091 | |
1092 | |
1093 bool Reflection::resolve_field(Handle field_mirror, Handle& receiver, fieldDescriptor* fd, bool check_final, TRAPS) { | |
1094 if (field_mirror.is_null()) { | |
1095 THROW_(vmSymbols::java_lang_NullPointerException(), false); | |
1096 } | |
1097 | |
1098 instanceKlassHandle klass (THREAD, java_lang_Class::as_klassOop(java_lang_reflect_Field::clazz(field_mirror()))); | |
1099 int slot = java_lang_reflect_Field::slot(field_mirror()); | |
1100 | |
1101 // Ensure klass is initialized | |
1102 klass->initialize(CHECK_false); | |
1103 fd->initialize(klass(), slot); | |
1104 | |
1105 bool is_static = fd->is_static(); | |
1106 KlassHandle receiver_klass; | |
1107 | |
1108 if (is_static) { | |
1109 receiver = KlassHandle(THREAD, klass()); | |
1110 receiver_klass = klass; | |
1111 } else { | |
1112 // Check object is a non-null instance of declaring class | |
1113 if (receiver.is_null()) { | |
1114 THROW_(vmSymbols::java_lang_NullPointerException(), false); | |
1115 } | |
1116 if (!receiver->is_a(klass())) { | |
1117 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class", false); | |
1118 } | |
1119 receiver_klass = KlassHandle(THREAD, receiver->klass()); | |
1120 } | |
1121 | |
1122 // Access checking (unless overridden by Field) | |
1123 if (!java_lang_reflect_Field::override(field_mirror())) { | |
1124 if (!(klass->is_public() && fd->is_public())) { | |
1125 bool access_check = reflect_check_access(klass(), fd->access_flags(), receiver_klass(), false, CHECK_false); | |
1126 if (!access_check) { | |
1127 return false; // exception | |
1128 } | |
1129 } | |
1130 } | |
1131 | |
1132 if (check_final && fd->is_final()) { | |
1133 // In 1.3 we always throw an error when attempting to set a final field. | |
1134 // In 1.2.x, this was allowed in the override bit was set by calling Field.setAccessible(true). | |
1135 // We currently maintain backwards compatibility. See bug 4250960. | |
1136 bool strict_final_check = !JDK_Version::is_jdk12x_version(); | |
1137 if (strict_final_check || !java_lang_reflect_Field::override(field_mirror())) { | |
1138 THROW_MSG_(vmSymbols::java_lang_IllegalAccessException(), "field is final", false); | |
1139 } | |
1140 } | |
1141 return true; | |
1142 } | |
1143 | |
1144 | |
1145 BasicType Reflection::field_get(jvalue* value, fieldDescriptor* fd, Handle receiver) { | |
1146 BasicType field_type = fd->field_type(); | |
1147 int offset = fd->offset(); | |
1148 switch (field_type) { | |
1149 case T_BOOLEAN: | |
1150 value->z = receiver->bool_field(offset); | |
1151 break; | |
1152 case T_CHAR: | |
1153 value->c = receiver->char_field(offset); | |
1154 break; | |
1155 case T_FLOAT: | |
1156 value->f = receiver->float_field(offset); | |
1157 break; | |
1158 case T_DOUBLE: | |
1159 value->d = receiver->double_field(offset); | |
1160 break; | |
1161 case T_BYTE: | |
1162 value->b = receiver->byte_field(offset); | |
1163 break; | |
1164 case T_SHORT: | |
1165 value->s = receiver->short_field(offset); | |
1166 break; | |
1167 case T_INT: | |
1168 value->i = receiver->int_field(offset); | |
1169 break; | |
1170 case T_LONG: | |
1171 value->j = receiver->long_field(offset); | |
1172 break; | |
1173 case T_OBJECT: | |
1174 case T_ARRAY: | |
1175 value->l = (jobject) receiver->obj_field(offset); | |
1176 break; | |
1177 default: | |
1178 return T_ILLEGAL; | |
1179 } | |
1180 return field_type; | |
1181 } | |
1182 | |
1183 | |
1184 void Reflection::field_set(jvalue* value, fieldDescriptor* fd, Handle receiver, BasicType value_type, TRAPS) { | |
1185 BasicType field_type = fd->field_type(); | |
1186 if (field_type != value_type) { | |
1187 widen(value, value_type, field_type, CHECK); | |
1188 } | |
1189 | |
1190 int offset = fd->offset(); | |
1191 switch (field_type) { | |
1192 case T_BOOLEAN: | |
1193 receiver->bool_field_put(offset, value->z); | |
1194 break; | |
1195 case T_CHAR: | |
1196 receiver->char_field_put(offset, value->c); | |
1197 break; | |
1198 case T_FLOAT: | |
1199 receiver->float_field_put(offset, value->f); | |
1200 break; | |
1201 case T_DOUBLE: | |
1202 receiver->double_field_put(offset, value->d); | |
1203 break; | |
1204 case T_BYTE: | |
1205 receiver->byte_field_put(offset, value->b); | |
1206 break; | |
1207 case T_SHORT: | |
1208 receiver->short_field_put(offset, value->s); | |
1209 break; | |
1210 case T_INT: | |
1211 receiver->int_field_put(offset, value->i); | |
1212 break; | |
1213 case T_LONG: | |
1214 receiver->long_field_put(offset, value->j); | |
1215 break; | |
1216 case T_OBJECT: | |
1217 case T_ARRAY: { | |
1218 Handle obj(THREAD, (oop) value->l); | |
1219 if (obj.not_null()) { | |
1220 symbolHandle signature(THREAD, fd->signature()); | |
1221 Handle loader (THREAD, fd->loader()); | |
1222 Handle protect (THREAD, Klass::cast(fd->field_holder())->protection_domain()); | |
1223 klassOop k = SystemDictionary::resolve_or_fail(signature, loader, protect, true, CHECK); // may block | |
1224 if (!obj->is_a(k)) { | |
1225 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "field type mismatch"); | |
1226 } | |
1227 } | |
1228 receiver->obj_field_put(offset, obj()); | |
1229 break; | |
1230 } | |
1231 default: | |
1232 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "field type mismatch"); | |
1233 } | |
1234 } | |
1235 | |
1236 | |
1237 oop Reflection::reflect_field(oop mirror, symbolOop field_name, jint which, TRAPS) { | |
1238 // Exclude primitive types and array types | |
1239 if (java_lang_Class::is_primitive(mirror)) return NULL; | |
1240 if (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) return NULL; | |
1241 | |
1242 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(mirror)); | |
1243 bool local_fields_only = (which == DECLARED); | |
1244 | |
1245 // Ensure class is linked | |
1246 k->link_class(CHECK_NULL); | |
1247 | |
1248 // Search class and interface fields | |
1249 for (FieldStream st(k, local_fields_only, false); !st.eos(); st.next()) { | |
1250 if (st.name() == field_name) { | |
1251 if (local_fields_only || st.access_flags().is_public()) { | |
1252 return new_field(&st, THREAD); | |
1253 } | |
1254 } | |
1255 } | |
1256 | |
1257 return NULL; | |
1258 } | |
1259 | |
1260 | |
1261 objArrayOop Reflection::reflect_fields(oop mirror, jint which, TRAPS) { | |
1262 // Exclude primitive types and array types | |
1263 if (java_lang_Class::is_primitive(mirror) | |
1264 || Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) { | |
1265 symbolHandle name = vmSymbolHandles::java_lang_reflect_Field(); | |
1266 klassOop klass = SystemDictionary::resolve_or_fail(name, true, CHECK_NULL); | |
1267 return oopFactory::new_objArray(klass, 0, CHECK_NULL); // Return empty array | |
1268 } | |
1269 | |
1270 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(mirror)); | |
1271 | |
1272 // Ensure class is linked | |
1273 k->link_class(CHECK_NULL); | |
1274 | |
1275 bool local_fields_only = (which == DECLARED); | |
1276 int count = 0; | |
1277 { // Compute fields count for class and interface fields | |
1278 for (FieldStream st(k, local_fields_only, false); !st.eos(); st.next()) { | |
1279 if (local_fields_only || st.access_flags().is_public()) { | |
1280 count++; | |
1281 } | |
1282 } | |
1283 } | |
1284 | |
1285 // Allocate result | |
1286 symbolHandle name = vmSymbolHandles::java_lang_reflect_Field(); | |
1287 klassOop klass = SystemDictionary::resolve_or_fail(name, true, CHECK_NULL); | |
1288 objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL); | |
1289 objArrayHandle result (THREAD, r); | |
1290 | |
1291 // Fill in results backwards | |
1292 { | |
1293 for (FieldStream st(k, local_fields_only, false); !st.eos(); st.next()) { | |
1294 if (local_fields_only || st.access_flags().is_public()) { | |
1295 oop field = new_field(&st, CHECK_NULL); | |
1296 result->obj_at_put(--count, field); | |
1297 } | |
1298 } | |
1299 assert(count == 0, "just checking"); | |
1300 } | |
1301 return result(); | |
1302 } | |
1303 | |
1304 | |
1305 oop Reflection::reflect_method(oop mirror, symbolHandle method_name, objArrayHandle types, jint which, TRAPS) { | |
1306 if (java_lang_Class::is_primitive(mirror)) return NULL; | |
1307 klassOop klass = java_lang_Class::as_klassOop(mirror); | |
1308 if (Klass::cast(klass)->oop_is_array() && which == MEMBER_DECLARED) return NULL; | |
1309 | |
1310 if (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) { | |
1311 klass = SystemDictionary::object_klass(); | |
1312 } | |
1313 instanceKlassHandle h_k(THREAD, klass); | |
1314 | |
1315 // Ensure klass is linked (need not be initialized) | |
1316 h_k->link_class(CHECK_NULL); | |
1317 | |
1318 // For interfaces include static initializers under jdk1.2.x (since classic does that) | |
1319 bool include_clinit = JDK_Version::is_jdk12x_version() && h_k->is_interface(); | |
1320 | |
1321 switch (which) { | |
1322 case MEMBER_PUBLIC: | |
1323 // First the public non-static methods (works if method holder is an interface) | |
1324 // Note that we can ignore checks for overridden methods, since we go up the hierarchy. | |
1325 { | |
1326 for (MethodStream st(h_k, false, false); !st.eos(); st.next()) { | |
1327 methodHandle m(THREAD, st.method()); | |
1328 // For interfaces include static initializers since classic does that! | |
1329 if (method_name() == m->name() && (include_clinit || (m->is_public() && !m->is_static() && !m->is_initializer()))) { | |
1330 symbolHandle signature(THREAD, m->signature()); | |
1331 bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL); | |
1332 if (parameter_match) { | |
1333 return new_method(m, false, false, THREAD); | |
1334 } | |
1335 } | |
1336 } | |
1337 } | |
1338 // Then the public static methods (works if method holder is an interface) | |
1339 { | |
1340 for (MethodStream st(h_k, false, false); !st.eos(); st.next()) { | |
1341 methodHandle m(THREAD, st.method()); | |
1342 if (method_name() == m->name() && m->is_public() && m->is_static() && !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 case MEMBER_DECLARED: | |
1353 // All local methods | |
1354 { | |
1355 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1356 methodHandle m(THREAD, st.method()); | |
1357 if (method_name() == m->name() && !m->is_initializer()) { | |
1358 symbolHandle signature(THREAD, m->signature()); | |
1359 bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL); | |
1360 if (parameter_match) { | |
1361 return new_method(m, false, false, THREAD); | |
1362 } | |
1363 } | |
1364 } | |
1365 } | |
1366 break; | |
1367 default: | |
1368 break; | |
1369 } | |
1370 return NULL; | |
1371 } | |
1372 | |
1373 | |
1374 objArrayOop Reflection::reflect_methods(oop mirror, jint which, TRAPS) { | |
1375 // Exclude primitive types | |
1376 if (java_lang_Class::is_primitive(mirror) || | |
1377 (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array() && (which == MEMBER_DECLARED))) { | |
1378 klassOop klass = SystemDictionary::reflect_method_klass(); | |
1379 return oopFactory::new_objArray(klass, 0, CHECK_NULL); // Return empty array | |
1380 } | |
1381 | |
1382 klassOop klass = java_lang_Class::as_klassOop(mirror); | |
1383 if (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) { | |
1384 klass = SystemDictionary::object_klass(); | |
1385 } | |
1386 instanceKlassHandle h_k(THREAD, klass); | |
1387 | |
1388 // Ensure klass is linked (need not be initialized) | |
1389 h_k->link_class(CHECK_NULL); | |
1390 | |
1391 // We search the (super)interfaces only if h_k is an interface itself | |
1392 bool is_interface = h_k->is_interface(); | |
1393 | |
1394 // For interfaces include static initializers under jdk1.2.x (since classic does that) | |
1395 bool include_clinit = JDK_Version::is_jdk12x_version() && is_interface; | |
1396 | |
1397 switch (which) { | |
1398 case MEMBER_PUBLIC: | |
1399 { | |
1400 | |
1401 // Count public methods (non-static and static) | |
1402 int count = 0; | |
1403 { | |
1404 for (MethodStream st(h_k, false, false); !st.eos(); st.next()) { | |
1405 methodOop m = st.method(); | |
1406 // For interfaces include static initializers since classic does that! | |
1407 if (include_clinit || (!m->is_initializer() && m->is_public() && !m->is_overridden_in(h_k()))) { | |
1408 count++; | |
1409 } | |
1410 } | |
1411 } | |
1412 | |
1413 // Allocate result | |
1414 klassOop klass = SystemDictionary::reflect_method_klass(); | |
1415 objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL); | |
1416 objArrayHandle h_result (THREAD, r); | |
1417 | |
1418 // Fill in results backwards | |
1419 { | |
1420 // First the non-static public methods | |
1421 for (MethodStream st(h_k, false, false); !st.eos(); st.next()) { | |
1422 methodHandle m (THREAD, st.method()); | |
1423 if (!m->is_static() && !m->is_initializer() && m->is_public() && !m->is_overridden_in(h_k())) { | |
1424 oop method = new_method(m, false, false, CHECK_NULL); | |
1425 if (method == NULL) { | |
1426 return NULL; | |
1427 } else { | |
1428 h_result->obj_at_put(--count, method); | |
1429 } | |
1430 } | |
1431 } | |
1432 } | |
1433 { | |
1434 // Then the static public methods | |
1435 for (MethodStream st(h_k, false, !is_interface); !st.eos(); st.next()) { | |
1436 methodHandle m (THREAD, st.method()); | |
1437 if (m->is_static() && (include_clinit || (!m->is_initializer()) && m->is_public() && !m->is_overridden_in(h_k()))) { | |
1438 oop method = new_method(m, false, false, CHECK_NULL); | |
1439 if (method == NULL) { | |
1440 return NULL; | |
1441 } else { | |
1442 h_result->obj_at_put(--count, method); | |
1443 } | |
1444 } | |
1445 } | |
1446 } | |
1447 | |
1448 assert(count == 0, "just checking"); | |
1449 return h_result(); | |
1450 } | |
1451 | |
1452 case MEMBER_DECLARED: | |
1453 { | |
1454 // Count all methods | |
1455 int count = 0; | |
1456 { | |
1457 for (MethodStream st(h_k, true, !is_interface); !st.eos(); st.next()) { | |
1458 methodOop m = st.method(); | |
1459 if (!m->is_initializer()) { | |
1460 count++; | |
1461 } | |
1462 } | |
1463 } | |
1464 // Allocate result | |
1465 klassOop klass = SystemDictionary::reflect_method_klass(); | |
1466 objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL); | |
1467 objArrayHandle h_result (THREAD, r); | |
1468 | |
1469 // Fill in results backwards | |
1470 { | |
1471 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1472 methodHandle m (THREAD, st.method()); | |
1473 if (!m->is_initializer()) { | |
1474 oop method = new_method(m, false, false, CHECK_NULL); | |
1475 if (method == NULL) { | |
1476 return NULL; | |
1477 } else { | |
1478 h_result->obj_at_put(--count, method); | |
1479 } | |
1480 } | |
1481 } | |
1482 } | |
1483 assert(count == 0, "just checking"); | |
1484 return h_result(); | |
1485 } | |
1486 } | |
1487 ShouldNotReachHere(); | |
1488 return NULL; | |
1489 } | |
1490 | |
1491 | |
1492 oop Reflection::reflect_constructor(oop mirror, objArrayHandle types, jint which, TRAPS) { | |
1493 | |
1494 // Exclude primitive, interface and array types | |
1495 bool prim = java_lang_Class::is_primitive(mirror); | |
1496 Klass* klass = prim ? NULL : Klass::cast(java_lang_Class::as_klassOop(mirror)); | |
1497 if (prim || klass->is_interface() || klass->oop_is_array()) return NULL; | |
1498 | |
1499 // Must be instance klass | |
1500 instanceKlassHandle h_k(THREAD, java_lang_Class::as_klassOop(mirror)); | |
1501 | |
1502 // Ensure klass is linked (need not be initialized) | |
1503 h_k->link_class(CHECK_NULL); | |
1504 | |
1505 bool local_only = (which == MEMBER_DECLARED); | |
1506 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1507 methodHandle m(THREAD, st.method()); | |
1508 if (m->name() == vmSymbols::object_initializer_name() && (local_only || m->is_public())) { | |
1509 symbolHandle signature(THREAD, m->signature()); | |
1510 bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL); | |
1511 if (parameter_match) { | |
1512 return new_constructor(m, THREAD); | |
1513 } | |
1514 } | |
1515 } | |
1516 | |
1517 return NULL; | |
1518 } | |
1519 | |
1520 | |
1521 objArrayOop Reflection::reflect_constructors(oop mirror, jint which, TRAPS) { | |
1522 // Exclude primitive, interface and array types | |
1523 bool prim = java_lang_Class::is_primitive(mirror); | |
1524 Klass* k = prim ? NULL : Klass::cast(java_lang_Class::as_klassOop(mirror)); | |
1525 if (prim || k->is_interface() || k->oop_is_array()) { | |
1526 return oopFactory::new_objArray(SystemDictionary::reflect_constructor_klass(), 0, CHECK_NULL); // Return empty array | |
1527 } | |
1528 | |
1529 // Must be instanceKlass at this point | |
1530 instanceKlassHandle h_k(THREAD, java_lang_Class::as_klassOop(mirror)); | |
1531 | |
1532 // Ensure klass is linked (need not be initialized) | |
1533 h_k->link_class(CHECK_NULL); | |
1534 | |
1535 bool local_only = (which == MEMBER_DECLARED); | |
1536 int count = 0; | |
1537 { | |
1538 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1539 methodOop m = st.method(); | |
1540 if (m->name() == vmSymbols::object_initializer_name() && (local_only || m->is_public())) { | |
1541 count++; | |
1542 } | |
1543 } | |
1544 } | |
1545 | |
1546 // Allocate result | |
1547 symbolHandle name = vmSymbolHandles::java_lang_reflect_Constructor(); | |
1548 klassOop klass = SystemDictionary::resolve_or_fail(name, true, CHECK_NULL); | |
1549 objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL); | |
1550 objArrayHandle h_result (THREAD, r); | |
1551 | |
1552 // Fill in results backwards | |
1553 { | |
1554 for (MethodStream st(h_k, true, true); !st.eos(); st.next()) { | |
1555 methodHandle m (THREAD, st.method()); | |
1556 if (m->name() == vmSymbols::object_initializer_name() && (local_only || m->is_public())) { | |
1557 oop constr = new_constructor(m, CHECK_NULL); | |
1558 if (constr == NULL) { | |
1559 return NULL; | |
1560 } else { | |
1561 h_result->obj_at_put(--count, constr); | |
1562 } | |
1563 } | |
1564 } | |
1565 assert(count == 0, "just checking"); | |
1566 } | |
1567 return h_result(); | |
1568 } | |
1569 | |
1570 | |
1571 // This would be nicer if, say, java.lang.reflect.Method was a subclass | |
1572 // of java.lang.reflect.Constructor | |
1573 | |
1574 oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) { | |
1575 oop mirror = java_lang_reflect_Method::clazz(method_mirror); | |
1576 int slot = java_lang_reflect_Method::slot(method_mirror); | |
1577 bool override = java_lang_reflect_Method::override(method_mirror) != 0; | |
1578 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror))); | |
1579 | |
1580 oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror); | |
1581 BasicType rtype; | |
1582 if (java_lang_Class::is_primitive(return_type_mirror)) { | |
1583 rtype = basic_type_mirror_to_basic_type(return_type_mirror, CHECK_NULL); | |
1584 } else { | |
1585 rtype = T_OBJECT; | |
1586 } | |
1587 | |
1588 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
|
1589 methodOop m = klass->method_with_idnum(slot); |
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1590 if (m == NULL) { |
0 | 1591 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke"); |
1592 } | |
51
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1593 methodHandle method(THREAD, m); |
0 | 1594 |
1595 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD); | |
1596 } | |
1597 | |
1598 | |
1599 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) { | |
1600 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror); | |
1601 int slot = java_lang_reflect_Constructor::slot(constructor_mirror); | |
1602 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0; | |
1603 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror))); | |
1604 | |
1605 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
|
1606 methodOop m = klass->method_with_idnum(slot); |
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1607 if (m == NULL) { |
0 | 1608 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke"); |
1609 } | |
51
1ffa5cdd0b7e
6667089: 3/3 multiple redefinitions of a class break reflection
dcubed
parents:
0
diff
changeset
|
1610 methodHandle method(THREAD, m); |
0 | 1611 assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor"); |
1612 | |
1613 // Make sure klass gets initialize | |
1614 klass->initialize(CHECK_NULL); | |
1615 | |
1616 // Create new instance (the receiver) | |
1617 klass->check_valid_for_instantiation(false, CHECK_NULL); | |
1618 Handle receiver = klass->allocate_instance_handle(CHECK_NULL); | |
1619 | |
1620 // Ignore result from call and return receiver | |
1621 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL); | |
1622 return receiver(); | |
1623 } | |
1624 | |
1625 | |
1626 #endif /* SUPPORT_OLD_REFLECTION */ |