1/*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "jvm.h"
27#include "classfile/javaClasses.inline.hpp"
28#include "classfile/moduleEntry.hpp"
29#include "classfile/packageEntry.hpp"
30#include "classfile/stringTable.hpp"
31#include "classfile/systemDictionary.hpp"
32#include "classfile/verifier.hpp"
33#include "classfile/vmSymbols.hpp"
34#include "interpreter/linkResolver.hpp"
35#include "logging/log.hpp"
36#include "memory/oopFactory.hpp"
37#include "memory/resourceArea.hpp"
38#include "memory/universe.hpp"
39#include "oops/instanceKlass.hpp"
40#include "oops/objArrayKlass.hpp"
41#include "oops/objArrayOop.inline.hpp"
42#include "oops/oop.inline.hpp"
43#include "oops/typeArrayOop.inline.hpp"
44#include "prims/jvmtiExport.hpp"
45#include "runtime/arguments.hpp"
46#include "runtime/fieldDescriptor.inline.hpp"
47#include "runtime/handles.inline.hpp"
48#include "runtime/javaCalls.hpp"
49#include "runtime/reflection.hpp"
50#include "runtime/reflectionUtils.hpp"
51#include "runtime/signature.hpp"
52#include "runtime/thread.inline.hpp"
53#include "runtime/vframe.inline.hpp"
54
55static void trace_class_resolution(const Klass* to_class) {
56 ResourceMark rm;
57 int line_number = -1;
58 const char * source_file = NULL;
59 Klass* caller = NULL;
60 JavaThread* jthread = JavaThread::current();
61 if (jthread->has_last_Java_frame()) {
62 vframeStream vfst(jthread);
63 // skip over any frames belonging to java.lang.Class
64 while (!vfst.at_end() &&
65 vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class()) {
66 vfst.next();
67 }
68 if (!vfst.at_end()) {
69 // this frame is a likely suspect
70 caller = vfst.method()->method_holder();
71 line_number = vfst.method()->line_number_from_bci(vfst.bci());
72 Symbol* s = vfst.method()->method_holder()->source_file_name();
73 if (s != NULL) {
74 source_file = s->as_C_string();
75 }
76 }
77 }
78 if (caller != NULL) {
79 const char * from = caller->external_name();
80 const char * to = to_class->external_name();
81 // print in a single call to reduce interleaving between threads
82 if (source_file != NULL) {
83 log_debug(class, resolve)("%s %s %s:%d (reflection)", from, to, source_file, line_number);
84 } else {
85 log_debug(class, resolve)("%s %s (reflection)", from, to);
86 }
87 }
88}
89
90
91oop Reflection::box(jvalue* value, BasicType type, TRAPS) {
92 if (type == T_VOID) {
93 return NULL;
94 }
95 if (type == T_OBJECT || type == T_ARRAY) {
96 // regular objects are not boxed
97 return (oop) value->l;
98 }
99 oop result = java_lang_boxing_object::create(type, value, CHECK_NULL);
100 if (result == NULL) {
101 THROW_(vmSymbols::java_lang_IllegalArgumentException(), result);
102 }
103 return result;
104}
105
106
107BasicType Reflection::unbox_for_primitive(oop box, jvalue* value, TRAPS) {
108 if (box == NULL) {
109 THROW_(vmSymbols::java_lang_IllegalArgumentException(), T_ILLEGAL);
110 }
111 return java_lang_boxing_object::get_value(box, value);
112}
113
114BasicType Reflection::unbox_for_regular_object(oop box, jvalue* value) {
115 // Note: box is really the unboxed oop. It might even be a Short, etc.!
116 value->l = (jobject) box;
117 return T_OBJECT;
118}
119
120
121void Reflection::widen(jvalue* value, BasicType current_type, BasicType wide_type, TRAPS) {
122 assert(wide_type != current_type, "widen should not be called with identical types");
123 switch (wide_type) {
124 case T_BOOLEAN:
125 case T_BYTE:
126 case T_CHAR:
127 break; // fail
128 case T_SHORT:
129 switch (current_type) {
130 case T_BYTE:
131 value->s = (jshort) value->b;
132 return;
133 default:
134 break;
135 }
136 break; // fail
137 case T_INT:
138 switch (current_type) {
139 case T_BYTE:
140 value->i = (jint) value->b;
141 return;
142 case T_CHAR:
143 value->i = (jint) value->c;
144 return;
145 case T_SHORT:
146 value->i = (jint) value->s;
147 return;
148 default:
149 break;
150 }
151 break; // fail
152 case T_LONG:
153 switch (current_type) {
154 case T_BYTE:
155 value->j = (jlong) value->b;
156 return;
157 case T_CHAR:
158 value->j = (jlong) value->c;
159 return;
160 case T_SHORT:
161 value->j = (jlong) value->s;
162 return;
163 case T_INT:
164 value->j = (jlong) value->i;
165 return;
166 default:
167 break;
168 }
169 break; // fail
170 case T_FLOAT:
171 switch (current_type) {
172 case T_BYTE:
173 value->f = (jfloat) value->b;
174 return;
175 case T_CHAR:
176 value->f = (jfloat) value->c;
177 return;
178 case T_SHORT:
179 value->f = (jfloat) value->s;
180 return;
181 case T_INT:
182 value->f = (jfloat) value->i;
183 return;
184 case T_LONG:
185 value->f = (jfloat) value->j;
186 return;
187 default:
188 break;
189 }
190 break; // fail
191 case T_DOUBLE:
192 switch (current_type) {
193 case T_BYTE:
194 value->d = (jdouble) value->b;
195 return;
196 case T_CHAR:
197 value->d = (jdouble) value->c;
198 return;
199 case T_SHORT:
200 value->d = (jdouble) value->s;
201 return;
202 case T_INT:
203 value->d = (jdouble) value->i;
204 return;
205 case T_FLOAT:
206 value->d = (jdouble) value->f;
207 return;
208 case T_LONG:
209 value->d = (jdouble) value->j;
210 return;
211 default:
212 break;
213 }
214 break; // fail
215 default:
216 break; // fail
217 }
218 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
219}
220
221
222BasicType Reflection::array_get(jvalue* value, arrayOop a, int index, TRAPS) {
223 if (!a->is_within_bounds(index)) {
224 THROW_(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), T_ILLEGAL);
225 }
226 if (a->is_objArray()) {
227 value->l = (jobject) objArrayOop(a)->obj_at(index);
228 return T_OBJECT;
229 } else {
230 assert(a->is_typeArray(), "just checking");
231 BasicType type = TypeArrayKlass::cast(a->klass())->element_type();
232 switch (type) {
233 case T_BOOLEAN:
234 value->z = typeArrayOop(a)->bool_at(index);
235 break;
236 case T_CHAR:
237 value->c = typeArrayOop(a)->char_at(index);
238 break;
239 case T_FLOAT:
240 value->f = typeArrayOop(a)->float_at(index);
241 break;
242 case T_DOUBLE:
243 value->d = typeArrayOop(a)->double_at(index);
244 break;
245 case T_BYTE:
246 value->b = typeArrayOop(a)->byte_at(index);
247 break;
248 case T_SHORT:
249 value->s = typeArrayOop(a)->short_at(index);
250 break;
251 case T_INT:
252 value->i = typeArrayOop(a)->int_at(index);
253 break;
254 case T_LONG:
255 value->j = typeArrayOop(a)->long_at(index);
256 break;
257 default:
258 return T_ILLEGAL;
259 }
260 return type;
261 }
262}
263
264
265void Reflection::array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS) {
266 if (!a->is_within_bounds(index)) {
267 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
268 }
269 if (a->is_objArray()) {
270 if (value_type == T_OBJECT) {
271 oop obj = (oop) value->l;
272 if (obj != NULL) {
273 Klass* element_klass = ObjArrayKlass::cast(a->klass())->element_klass();
274 if (!obj->is_a(element_klass)) {
275 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
276 }
277 }
278 objArrayOop(a)->obj_at_put(index, obj);
279 }
280 } else {
281 assert(a->is_typeArray(), "just checking");
282 BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
283 if (array_type != value_type) {
284 // The widen operation can potentially throw an exception, but cannot block,
285 // so typeArrayOop a is safe if the call succeeds.
286 widen(value, value_type, array_type, CHECK);
287 }
288 switch (array_type) {
289 case T_BOOLEAN:
290 typeArrayOop(a)->bool_at_put(index, value->z);
291 break;
292 case T_CHAR:
293 typeArrayOop(a)->char_at_put(index, value->c);
294 break;
295 case T_FLOAT:
296 typeArrayOop(a)->float_at_put(index, value->f);
297 break;
298 case T_DOUBLE:
299 typeArrayOop(a)->double_at_put(index, value->d);
300 break;
301 case T_BYTE:
302 typeArrayOop(a)->byte_at_put(index, value->b);
303 break;
304 case T_SHORT:
305 typeArrayOop(a)->short_at_put(index, value->s);
306 break;
307 case T_INT:
308 typeArrayOop(a)->int_at_put(index, value->i);
309 break;
310 case T_LONG:
311 typeArrayOop(a)->long_at_put(index, value->j);
312 break;
313 default:
314 THROW(vmSymbols::java_lang_IllegalArgumentException());
315 }
316 }
317}
318
319static Klass* basic_type_mirror_to_arrayklass(oop basic_type_mirror, TRAPS) {
320 assert(java_lang_Class::is_primitive(basic_type_mirror), "just checking");
321 BasicType type = java_lang_Class::primitive_type(basic_type_mirror);
322 if (type == T_VOID) {
323 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
324 }
325 else {
326 return Universe::typeArrayKlassObj(type);
327 }
328}
329
330arrayOop Reflection::reflect_new_array(oop element_mirror, jint length, TRAPS) {
331 if (element_mirror == NULL) {
332 THROW_0(vmSymbols::java_lang_NullPointerException());
333 }
334 if (length < 0) {
335 THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", length));
336 }
337 if (java_lang_Class::is_primitive(element_mirror)) {
338 Klass* tak = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
339 return TypeArrayKlass::cast(tak)->allocate(length, THREAD);
340 } else {
341 Klass* k = java_lang_Class::as_Klass(element_mirror);
342 if (k->is_array_klass() && ArrayKlass::cast(k)->dimension() >= MAX_DIM) {
343 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
344 }
345 return oopFactory::new_objArray(k, length, THREAD);
346 }
347}
348
349
350arrayOop Reflection::reflect_new_multi_array(oop element_mirror, typeArrayOop dim_array, TRAPS) {
351 assert(dim_array->is_typeArray(), "just checking");
352 assert(TypeArrayKlass::cast(dim_array->klass())->element_type() == T_INT, "just checking");
353
354 if (element_mirror == NULL) {
355 THROW_0(vmSymbols::java_lang_NullPointerException());
356 }
357
358 int len = dim_array->length();
359 if (len <= 0 || len > MAX_DIM) {
360 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
361 }
362
363 jint dimensions[MAX_DIM]; // C array copy of intArrayOop
364 for (int i = 0; i < len; i++) {
365 int d = dim_array->int_at(i);
366 if (d < 0) {
367 THROW_MSG_0(vmSymbols::java_lang_NegativeArraySizeException(), err_msg("%d", d));
368 }
369 dimensions[i] = d;
370 }
371
372 Klass* klass;
373 int dim = len;
374 if (java_lang_Class::is_primitive(element_mirror)) {
375 klass = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
376 } else {
377 klass = java_lang_Class::as_Klass(element_mirror);
378 if (klass->is_array_klass()) {
379 int k_dim = ArrayKlass::cast(klass)->dimension();
380 if (k_dim + len > MAX_DIM) {
381 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
382 }
383 dim += k_dim;
384 }
385 }
386 klass = klass->array_klass(dim, CHECK_NULL);
387 oop obj = ArrayKlass::cast(klass)->multi_allocate(len, dimensions, CHECK_NULL);
388 assert(obj->is_array(), "just checking");
389 return arrayOop(obj);
390}
391
392
393static bool under_unsafe_anonymous_host(const InstanceKlass* ik, const InstanceKlass* unsafe_anonymous_host) {
394 DEBUG_ONLY(int inf_loop_check = 1000 * 1000 * 1000);
395 for (;;) {
396 const InstanceKlass* hc = ik->unsafe_anonymous_host();
397 if (hc == NULL) return false;
398 if (hc == unsafe_anonymous_host) return true;
399 ik = hc;
400
401 // There's no way to make a host class loop short of patching memory.
402 // Therefore there cannot be a loop here unless there's another bug.
403 // Still, let's check for it.
404 assert(--inf_loop_check > 0, "no unsafe_anonymous_host loop");
405 }
406}
407
408static bool can_relax_access_check_for(const Klass* accessor,
409 const Klass* accessee,
410 bool classloader_only) {
411
412 const InstanceKlass* accessor_ik = InstanceKlass::cast(accessor);
413 const InstanceKlass* accessee_ik = InstanceKlass::cast(accessee);
414
415 // If either is on the other's unsafe_anonymous_host chain, access is OK,
416 // because one is inside the other.
417 if (under_unsafe_anonymous_host(accessor_ik, accessee_ik) ||
418 under_unsafe_anonymous_host(accessee_ik, accessor_ik))
419 return true;
420
421 if (RelaxAccessControlCheck &&
422 accessor_ik->major_version() < Verifier::NO_RELAX_ACCESS_CTRL_CHECK_VERSION &&
423 accessee_ik->major_version() < Verifier::NO_RELAX_ACCESS_CTRL_CHECK_VERSION) {
424 return classloader_only &&
425 Verifier::relax_access_for(accessor_ik->class_loader()) &&
426 accessor_ik->protection_domain() == accessee_ik->protection_domain() &&
427 accessor_ik->class_loader() == accessee_ik->class_loader();
428 }
429
430 return false;
431}
432
433/*
434 Type Accessibility check for public types: Callee Type T is accessible to Caller Type S if:
435
436 Callee T in Callee T in package PT,
437 unnamed module runtime module MT
438 ------------------------------------------------------------------------------------------------
439
440 Caller S in package If MS is loose: YES If same classloader/package (PS == PT): YES
441 PS, runtime module MS If MS can read T's If same runtime module: (MS == MT): YES
442 unnamed module: YES
443 Else if (MS can read MT (establish readability) &&
444 ((MT exports PT to MS or to all modules) ||
445 (MT is open))): YES
446
447 ------------------------------------------------------------------------------------------------
448 Caller S in unnamed YES Readability exists because unnamed module
449 module UM "reads" all modules
450 if (MT exports PT to UM or to all modules): YES
451
452 ------------------------------------------------------------------------------------------------
453
454 Note: a loose module is a module that can read all current and future unnamed modules.
455*/
456Reflection::VerifyClassAccessResults Reflection::verify_class_access(
457 const Klass* current_class, const InstanceKlass* new_class, bool classloader_only) {
458
459 // Verify that current_class can access new_class. If the classloader_only
460 // flag is set, we automatically allow any accesses in which current_class
461 // doesn't have a classloader.
462 if ((current_class == NULL) ||
463 (current_class == new_class) ||
464 is_same_class_package(current_class, new_class)) {
465 return ACCESS_OK;
466 }
467 // Allow all accesses from jdk/internal/reflect/MagicAccessorImpl subclasses to
468 // succeed trivially.
469 if (SystemDictionary::reflect_MagicAccessorImpl_klass_is_loaded() &&
470 current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
471 return ACCESS_OK;
472 }
473
474 // module boundaries
475 if (new_class->is_public()) {
476 // Ignore modules for DumpSharedSpaces because we do not have any package
477 // or module information for modules other than java.base.
478 if (DumpSharedSpaces) {
479 return ACCESS_OK;
480 }
481
482 // Find the module entry for current_class, the accessor
483 ModuleEntry* module_from = current_class->module();
484 // Find the module entry for new_class, the accessee
485 ModuleEntry* module_to = new_class->module();
486
487 // both in same (possibly unnamed) module
488 if (module_from == module_to) {
489 return ACCESS_OK;
490 }
491
492 // Acceptable access to a type in an unnamed module. Note that since
493 // unnamed modules can read all unnamed modules, this also handles the
494 // case where module_from is also unnamed but in a different class loader.
495 if (!module_to->is_named() &&
496 (module_from->can_read_all_unnamed() || module_from->can_read(module_to))) {
497 return ACCESS_OK;
498 }
499
500 // Establish readability, check if module_from is allowed to read module_to.
501 if (!module_from->can_read(module_to)) {
502 return MODULE_NOT_READABLE;
503 }
504
505 // Access is allowed if module_to is open, i.e. all its packages are unqualifiedly exported
506 if (module_to->is_open()) {
507 return ACCESS_OK;
508 }
509
510 PackageEntry* package_to = new_class->package();
511 assert(package_to != NULL, "can not obtain new_class' package");
512
513 {
514 MutexLocker m1(Module_lock);
515
516 // Once readability is established, if module_to exports T unqualifiedly,
517 // (to all modules), than whether module_from is in the unnamed module
518 // or not does not matter, access is allowed.
519 if (package_to->is_unqual_exported()) {
520 return ACCESS_OK;
521 }
522
523 // Access is allowed if both 1 & 2 hold:
524 // 1. Readability, module_from can read module_to (established above).
525 // 2. Either module_to exports T to module_from qualifiedly.
526 // or
527 // module_to exports T to all unnamed modules and module_from is unnamed.
528 // or
529 // module_to exports T unqualifiedly to all modules (checked above).
530 if (!package_to->is_qexported_to(module_from)) {
531 return TYPE_NOT_EXPORTED;
532 }
533 }
534 return ACCESS_OK;
535 }
536
537 if (can_relax_access_check_for(current_class, new_class, classloader_only)) {
538 return ACCESS_OK;
539 }
540 return OTHER_PROBLEM;
541}
542
543// Return an error message specific to the specified Klass*'s and result.
544// This function must be called from within a block containing a ResourceMark.
545char* Reflection::verify_class_access_msg(const Klass* current_class,
546 const InstanceKlass* new_class,
547 const VerifyClassAccessResults result) {
548 assert(result != ACCESS_OK, "must be failure result");
549 char * msg = NULL;
550 if (result != OTHER_PROBLEM && new_class != NULL && current_class != NULL) {
551 // Find the module entry for current_class, the accessor
552 ModuleEntry* module_from = current_class->module();
553 const char * module_from_name = module_from->is_named() ? module_from->name()->as_C_string() : UNNAMED_MODULE;
554 const char * current_class_name = current_class->external_name();
555
556 // Find the module entry for new_class, the accessee
557 ModuleEntry* module_to = NULL;
558 module_to = new_class->module();
559 const char * module_to_name = module_to->is_named() ? module_to->name()->as_C_string() : UNNAMED_MODULE;
560 const char * new_class_name = new_class->external_name();
561
562 if (result == MODULE_NOT_READABLE) {
563 assert(module_from->is_named(), "Unnamed modules can read all modules");
564 if (module_to->is_named()) {
565 size_t len = 100 + strlen(current_class_name) + 2*strlen(module_from_name) +
566 strlen(new_class_name) + 2*strlen(module_to_name);
567 msg = NEW_RESOURCE_ARRAY(char, len);
568 jio_snprintf(msg, len - 1,
569 "class %s (in module %s) cannot access class %s (in module %s) because module %s does not read module %s",
570 current_class_name, module_from_name, new_class_name,
571 module_to_name, module_from_name, module_to_name);
572 } else {
573 oop jlm = module_to->module();
574 assert(jlm != NULL, "Null jlm in module_to ModuleEntry");
575 intptr_t identity_hash = jlm->identity_hash();
576 size_t len = 160 + strlen(current_class_name) + 2*strlen(module_from_name) +
577 strlen(new_class_name) + 2*sizeof(uintx);
578 msg = NEW_RESOURCE_ARRAY(char, len);
579 jio_snprintf(msg, len - 1,
580 "class %s (in module %s) cannot access class %s (in unnamed module @" SIZE_FORMAT_HEX ") because module %s does not read unnamed module @" SIZE_FORMAT_HEX,
581 current_class_name, module_from_name, new_class_name, uintx(identity_hash),
582 module_from_name, uintx(identity_hash));
583 }
584
585 } else if (result == TYPE_NOT_EXPORTED) {
586 assert(new_class->package() != NULL,
587 "Unnamed packages are always exported");
588 const char * package_name =
589 new_class->package()->name()->as_klass_external_name();
590 assert(module_to->is_named(), "Unnamed modules export all packages");
591 if (module_from->is_named()) {
592 size_t len = 118 + strlen(current_class_name) + 2*strlen(module_from_name) +
593 strlen(new_class_name) + 2*strlen(module_to_name) + strlen(package_name);
594 msg = NEW_RESOURCE_ARRAY(char, len);
595 jio_snprintf(msg, len - 1,
596 "class %s (in module %s) cannot access class %s (in module %s) because module %s does not export %s to module %s",
597 current_class_name, module_from_name, new_class_name,
598 module_to_name, module_to_name, package_name, module_from_name);
599 } else {
600 oop jlm = module_from->module();
601 assert(jlm != NULL, "Null jlm in module_from ModuleEntry");
602 intptr_t identity_hash = jlm->identity_hash();
603 size_t len = 170 + strlen(current_class_name) + strlen(new_class_name) +
604 2*strlen(module_to_name) + strlen(package_name) + 2*sizeof(uintx);
605 msg = NEW_RESOURCE_ARRAY(char, len);
606 jio_snprintf(msg, len - 1,
607 "class %s (in unnamed module @" SIZE_FORMAT_HEX ") cannot access class %s (in module %s) because module %s does not export %s to unnamed module @" SIZE_FORMAT_HEX,
608 current_class_name, uintx(identity_hash), new_class_name, module_to_name,
609 module_to_name, package_name, uintx(identity_hash));
610 }
611 } else {
612 ShouldNotReachHere();
613 }
614 } // result != OTHER_PROBLEM...
615 return msg;
616}
617
618bool Reflection::verify_member_access(const Klass* current_class,
619 const Klass* resolved_class,
620 const Klass* member_class,
621 AccessFlags access,
622 bool classloader_only,
623 bool protected_restriction,
624 TRAPS) {
625 // Verify that current_class can access a member of member_class, where that
626 // field's access bits are "access". We assume that we've already verified
627 // that current_class can access member_class.
628 //
629 // If the classloader_only flag is set, we automatically allow any accesses
630 // in which current_class doesn't have a classloader.
631 //
632 // "resolved_class" is the runtime type of "member_class". Sometimes we don't
633 // need this distinction (e.g. if all we have is the runtime type, or during
634 // class file parsing when we only care about the static type); in that case
635 // callers should ensure that resolved_class == member_class.
636 //
637 if ((current_class == NULL) ||
638 (current_class == member_class) ||
639 access.is_public()) {
640 return true;
641 }
642
643 const Klass* host_class = current_class;
644 if (current_class->is_instance_klass() &&
645 InstanceKlass::cast(current_class)->is_unsafe_anonymous()) {
646 host_class = InstanceKlass::cast(current_class)->unsafe_anonymous_host();
647 assert(host_class != NULL, "Unsafe anonymous class has null host class");
648 assert(!(host_class->is_instance_klass() &&
649 InstanceKlass::cast(host_class)->is_unsafe_anonymous()),
650 "unsafe_anonymous_host should not be unsafe anonymous itself");
651 }
652 if (host_class == member_class) {
653 return true;
654 }
655
656 if (access.is_protected()) {
657 if (!protected_restriction) {
658 // See if current_class (or outermost host class) is a subclass of member_class
659 // An interface may not access protected members of j.l.Object
660 if (!host_class->is_interface() && host_class->is_subclass_of(member_class)) {
661 if (access.is_static() || // static fields are ok, see 6622385
662 current_class == resolved_class ||
663 member_class == resolved_class ||
664 host_class->is_subclass_of(resolved_class) ||
665 resolved_class->is_subclass_of(host_class)) {
666 return true;
667 }
668 }
669 }
670 }
671
672 // package access
673 if (!access.is_private() && is_same_class_package(current_class, member_class)) {
674 return true;
675 }
676
677 // private access between different classes needs a nestmate check, but
678 // not for unsafe anonymous classes - so check host_class
679 if (access.is_private() && host_class == current_class) {
680 if (current_class->is_instance_klass() && member_class->is_instance_klass() ) {
681 InstanceKlass* cur_ik = const_cast<InstanceKlass*>(InstanceKlass::cast(current_class));
682 InstanceKlass* field_ik = const_cast<InstanceKlass*>(InstanceKlass::cast(member_class));
683 // Nestmate access checks may require resolution and validation of the nest-host.
684 // It is up to the caller to check for pending exceptions and handle appropriately.
685 bool access = cur_ik->has_nestmate_access_to(field_ik, CHECK_false);
686 if (access) {
687 guarantee(resolved_class->is_subclass_of(member_class), "must be!");
688 return true;
689 }
690 }
691 }
692
693 // Allow all accesses from jdk/internal/reflect/MagicAccessorImpl subclasses to
694 // succeed trivially.
695 if (current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
696 return true;
697 }
698
699 // Check for special relaxations
700 return can_relax_access_check_for(current_class, member_class, classloader_only);
701}
702
703bool Reflection::is_same_class_package(const Klass* class1, const Klass* class2) {
704 return InstanceKlass::cast(class1)->is_same_class_package(class2);
705}
706
707// Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not,
708// throw an incompatible class change exception
709// If inner_is_member, require the inner to be a member of the outer.
710// If !inner_is_member, require the inner to be unsafe anonymous (a non-member).
711// Caller is responsible for figuring out in advance which case must be true.
712void Reflection::check_for_inner_class(const InstanceKlass* outer, const InstanceKlass* inner,
713 bool inner_is_member, TRAPS) {
714 InnerClassesIterator iter(outer);
715 constantPoolHandle cp (THREAD, outer->constants());
716 for (; !iter.done(); iter.next()) {
717 int ioff = iter.inner_class_info_index();
718 int ooff = iter.outer_class_info_index();
719
720 if (inner_is_member && ioff != 0 && ooff != 0) {
721 if (cp->klass_name_at_matches(outer, ooff) &&
722 cp->klass_name_at_matches(inner, ioff)) {
723 Klass* o = cp->klass_at(ooff, CHECK);
724 if (o == outer) {
725 Klass* i = cp->klass_at(ioff, CHECK);
726 if (i == inner) {
727 return;
728 }
729 }
730 }
731 }
732
733 if (!inner_is_member && ioff != 0 && ooff == 0 &&
734 cp->klass_name_at_matches(inner, ioff)) {
735 Klass* i = cp->klass_at(ioff, CHECK);
736 if (i == inner) {
737 return;
738 }
739 }
740 }
741
742 // 'inner' not declared as an inner klass in outer
743 ResourceMark rm(THREAD);
744 Exceptions::fthrow(
745 THREAD_AND_LOCATION,
746 vmSymbols::java_lang_IncompatibleClassChangeError(),
747 "%s and %s disagree on InnerClasses attribute",
748 outer->external_name(),
749 inner->external_name()
750 );
751}
752
753// Utility method converting a single SignatureStream element into java.lang.Class instance
754static oop get_mirror_from_signature(const methodHandle& method,
755 SignatureStream* ss,
756 TRAPS) {
757
758
759 if (T_OBJECT == ss->type() || T_ARRAY == ss->type()) {
760 Symbol* name = ss->as_symbol();
761 oop loader = method->method_holder()->class_loader();
762 oop protection_domain = method->method_holder()->protection_domain();
763 const Klass* k = SystemDictionary::resolve_or_fail(name,
764 Handle(THREAD, loader),
765 Handle(THREAD, protection_domain),
766 true,
767 CHECK_NULL);
768 if (log_is_enabled(Debug, class, resolve)) {
769 trace_class_resolution(k);
770 }
771 return k->java_mirror();
772 }
773
774 assert(ss->type() != T_VOID || ss->at_return_type(),
775 "T_VOID should only appear as return type");
776
777 return java_lang_Class::primitive_mirror(ss->type());
778}
779
780static objArrayHandle get_parameter_types(const methodHandle& method,
781 int parameter_count,
782 oop* return_type,
783 TRAPS) {
784 // Allocate array holding parameter types (java.lang.Class instances)
785 objArrayOop m = oopFactory::new_objArray(SystemDictionary::Class_klass(), parameter_count, CHECK_(objArrayHandle()));
786 objArrayHandle mirrors(THREAD, m);
787 int index = 0;
788 // Collect parameter types
789 ResourceMark rm(THREAD);
790 Symbol* signature = method->signature();
791 SignatureStream ss(signature);
792 while (!ss.at_return_type()) {
793 oop mirror = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle()));
794 mirrors->obj_at_put(index++, mirror);
795 ss.next();
796 }
797 assert(index == parameter_count, "invalid parameter count");
798 if (return_type != NULL) {
799 // Collect return type as well
800 assert(ss.at_return_type(), "return type should be present");
801 *return_type = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle()));
802 }
803 return mirrors;
804}
805
806static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
807 return method->resolved_checked_exceptions(THREAD);
808}
809
810static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
811 // Basic types
812 BasicType type = vmSymbols::signature_type(signature);
813 if (type != T_OBJECT) {
814 return Handle(THREAD, Universe::java_mirror(type));
815 }
816
817 Klass* result =
818 SystemDictionary::resolve_or_fail(signature,
819 Handle(THREAD, k->class_loader()),
820 Handle(THREAD, k->protection_domain()),
821 true, CHECK_(Handle()));
822
823 if (log_is_enabled(Debug, class, resolve)) {
824 trace_class_resolution(result);
825 }
826
827 oop nt = result->java_mirror();
828 return Handle(THREAD, nt);
829}
830
831
832oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
833 // Allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
834 assert(!method()->is_initializer() ||
835 (for_constant_pool_access && method()->is_static()),
836 "should call new_constructor instead");
837 InstanceKlass* holder = method->method_holder();
838 int slot = method->method_idnum();
839
840 Symbol* signature = method->signature();
841 int parameter_count = ArgumentCount(signature).size();
842 oop return_type_oop = NULL;
843 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
844 if (parameter_types.is_null() || return_type_oop == NULL) return NULL;
845
846 Handle return_type(THREAD, return_type_oop);
847
848 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
849
850 if (exception_types.is_null()) return NULL;
851
852 Symbol* method_name = method->name();
853 oop name_oop = StringTable::intern(method_name, CHECK_NULL);
854 Handle name = Handle(THREAD, name_oop);
855 if (name == NULL) return NULL;
856
857 const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
858
859 Handle mh = java_lang_reflect_Method::create(CHECK_NULL);
860
861 java_lang_reflect_Method::set_clazz(mh(), holder->java_mirror());
862 java_lang_reflect_Method::set_slot(mh(), slot);
863 java_lang_reflect_Method::set_name(mh(), name());
864 java_lang_reflect_Method::set_return_type(mh(), return_type());
865 java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
866 java_lang_reflect_Method::set_exception_types(mh(), exception_types());
867 java_lang_reflect_Method::set_modifiers(mh(), modifiers);
868 java_lang_reflect_Method::set_override(mh(), false);
869 if (method->generic_signature() != NULL) {
870 Symbol* gs = method->generic_signature();
871 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
872 java_lang_reflect_Method::set_signature(mh(), sig());
873 }
874 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
875 java_lang_reflect_Method::set_annotations(mh(), an_oop);
876 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
877 java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
878 an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
879 java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
880 return mh();
881}
882
883
884oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
885 assert(method()->is_initializer(), "should call new_method instead");
886
887 InstanceKlass* holder = method->method_holder();
888 int slot = method->method_idnum();
889
890 Symbol* signature = method->signature();
891 int parameter_count = ArgumentCount(signature).size();
892 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, NULL, CHECK_NULL);
893 if (parameter_types.is_null()) return NULL;
894
895 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
896 if (exception_types.is_null()) return NULL;
897
898 const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
899
900 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
901
902 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
903 java_lang_reflect_Constructor::set_slot(ch(), slot);
904 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
905 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
906 java_lang_reflect_Constructor::set_modifiers(ch(), modifiers);
907 java_lang_reflect_Constructor::set_override(ch(), false);
908 if (method->generic_signature() != NULL) {
909 Symbol* gs = method->generic_signature();
910 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
911 java_lang_reflect_Constructor::set_signature(ch(), sig());
912 }
913 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
914 java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
915 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
916 java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
917 return ch();
918}
919
920
921oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
922 Symbol* field_name = fd->name();
923 oop name_oop = StringTable::intern(field_name, CHECK_NULL);
924 Handle name = Handle(THREAD, name_oop);
925 Symbol* signature = fd->signature();
926 InstanceKlass* holder = fd->field_holder();
927 Handle type = new_type(signature, holder, CHECK_NULL);
928 Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
929
930 java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
931 java_lang_reflect_Field::set_slot(rh(), fd->index());
932 java_lang_reflect_Field::set_name(rh(), name());
933 java_lang_reflect_Field::set_type(rh(), type());
934 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
935 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
936 java_lang_reflect_Field::set_override(rh(), false);
937 if (fd->has_generic_signature()) {
938 Symbol* gs = fd->generic_signature();
939 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
940 java_lang_reflect_Field::set_signature(rh(), sig());
941 }
942 typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
943 java_lang_reflect_Field::set_annotations(rh(), an_oop);
944 return rh();
945}
946
947oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
948 int flags, TRAPS) {
949
950 Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
951
952 if(NULL != sym) {
953 Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
954 java_lang_reflect_Parameter::set_name(rh(), name());
955 } else {
956 java_lang_reflect_Parameter::set_name(rh(), NULL);
957 }
958
959 java_lang_reflect_Parameter::set_modifiers(rh(), flags);
960 java_lang_reflect_Parameter::set_executable(rh(), method());
961 java_lang_reflect_Parameter::set_index(rh(), index);
962 return rh();
963}
964
965
966static methodHandle resolve_interface_call(InstanceKlass* klass,
967 const methodHandle& method,
968 Klass* recv_klass,
969 Handle receiver,
970 TRAPS) {
971
972 assert(!method.is_null() , "method should not be null");
973
974 CallInfo info;
975 Symbol* signature = method->signature();
976 Symbol* name = method->name();
977 LinkResolver::resolve_interface_call(info, receiver, recv_klass,
978 LinkInfo(klass, name, signature),
979 true,
980 CHECK_(methodHandle()));
981 return info.selected_method();
982}
983
984// Conversion
985static BasicType basic_type_mirror_to_basic_type(oop basic_type_mirror, TRAPS) {
986 assert(java_lang_Class::is_primitive(basic_type_mirror),
987 "just checking");
988 return java_lang_Class::primitive_type(basic_type_mirror);
989}
990
991// Narrowing of basic types. Used to create correct jvalues for
992// boolean, byte, char and short return return values from interpreter
993// which are returned as ints. Throws IllegalArgumentException.
994static void narrow(jvalue* value, BasicType narrow_type, TRAPS) {
995 switch (narrow_type) {
996 case T_BOOLEAN:
997 value->z = (jboolean) (value->i & 1);
998 return;
999 case T_BYTE:
1000 value->b = (jbyte)value->i;
1001 return;
1002 case T_CHAR:
1003 value->c = (jchar)value->i;
1004 return;
1005 case T_SHORT:
1006 value->s = (jshort)value->i;
1007 return;
1008 default:
1009 break; // fail
1010 }
1011 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
1012}
1013
1014
1015// Method call (shared by invoke_method and invoke_constructor)
1016static oop invoke(InstanceKlass* klass,
1017 const methodHandle& reflected_method,
1018 Handle receiver,
1019 bool override,
1020 objArrayHandle ptypes,
1021 BasicType rtype,
1022 objArrayHandle args,
1023 bool is_method_invoke,
1024 TRAPS) {
1025
1026 ResourceMark rm(THREAD);
1027
1028 methodHandle method; // actual method to invoke
1029 Klass* target_klass; // target klass, receiver's klass for non-static
1030
1031 // Ensure klass is initialized
1032 klass->initialize(CHECK_NULL);
1033
1034 bool is_static = reflected_method->is_static();
1035 if (is_static) {
1036 // ignore receiver argument
1037 method = reflected_method;
1038 target_klass = klass;
1039 } else {
1040 // check for null receiver
1041 if (receiver.is_null()) {
1042 THROW_0(vmSymbols::java_lang_NullPointerException());
1043 }
1044 // Check class of receiver against class declaring method
1045 if (!receiver->is_a(klass)) {
1046 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
1047 }
1048 // target klass is receiver's klass
1049 target_klass = receiver->klass();
1050 // no need to resolve if method is private or <init>
1051 if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) {
1052 method = reflected_method;
1053 } else {
1054 // resolve based on the receiver
1055 if (reflected_method->method_holder()->is_interface()) {
1056 // resolve interface call
1057 //
1058 // Match resolution errors with those thrown due to reflection inlining
1059 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
1060 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
1061 if (HAS_PENDING_EXCEPTION) {
1062 // Method resolution threw an exception; wrap it in an InvocationTargetException
1063 oop resolution_exception = PENDING_EXCEPTION;
1064 CLEAR_PENDING_EXCEPTION;
1065 // JVMTI has already reported the pending exception
1066 // JVMTI internal flag reset is needed in order to report InvocationTargetException
1067 if (THREAD->is_Java_thread()) {
1068 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
1069 }
1070 JavaCallArguments args(Handle(THREAD, resolution_exception));
1071 THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1072 vmSymbols::throwable_void_signature(),
1073 &args);
1074 }
1075 } else {
1076 // if the method can be overridden, we resolve using the vtable index.
1077 assert(!reflected_method->has_itable_index(), "");
1078 int index = reflected_method->vtable_index();
1079 method = reflected_method;
1080 if (index != Method::nonvirtual_vtable_index) {
1081 method = methodHandle(THREAD, target_klass->method_at_vtable(index));
1082 }
1083 if (!method.is_null()) {
1084 // Check for abstract methods as well
1085 if (method->is_abstract()) {
1086 // new default: 6531596
1087 ResourceMark rm(THREAD);
1088 stringStream ss;
1089 ss.print("'");
1090 Method::print_external_name(&ss, target_klass, method->name(), method->signature());
1091 ss.print("'");
1092 Handle h_origexception = Exceptions::new_exception(THREAD,
1093 vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1094 JavaCallArguments args(h_origexception);
1095 THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1096 vmSymbols::throwable_void_signature(),
1097 &args);
1098 }
1099 }
1100 }
1101 }
1102 }
1103
1104 // I believe this is a ShouldNotGetHere case which requires
1105 // an internal vtable bug. If you ever get this please let Karen know.
1106 if (method.is_null()) {
1107 ResourceMark rm(THREAD);
1108 stringStream ss;
1109 ss.print("'");
1110 Method::print_external_name(&ss, klass,
1111 reflected_method->name(),
1112 reflected_method->signature());
1113 ss.print("'");
1114 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
1115 }
1116
1117 assert(ptypes->is_objArray(), "just checking");
1118 int args_len = args.is_null() ? 0 : args->length();
1119 // Check number of arguments
1120 if (ptypes->length() != args_len) {
1121 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1122 "wrong number of arguments");
1123 }
1124
1125 // Create object to contain parameters for the JavaCall
1126 JavaCallArguments java_args(method->size_of_parameters());
1127
1128 if (!is_static) {
1129 java_args.push_oop(receiver);
1130 }
1131
1132 for (int i = 0; i < args_len; i++) {
1133 oop type_mirror = ptypes->obj_at(i);
1134 oop arg = args->obj_at(i);
1135 if (java_lang_Class::is_primitive(type_mirror)) {
1136 jvalue value;
1137 BasicType ptype = basic_type_mirror_to_basic_type(type_mirror, CHECK_NULL);
1138 BasicType atype = Reflection::unbox_for_primitive(arg, &value, CHECK_NULL);
1139 if (ptype != atype) {
1140 Reflection::widen(&value, atype, ptype, CHECK_NULL);
1141 }
1142 switch (ptype) {
1143 case T_BOOLEAN: java_args.push_int(value.z); break;
1144 case T_CHAR: java_args.push_int(value.c); break;
1145 case T_BYTE: java_args.push_int(value.b); break;
1146 case T_SHORT: java_args.push_int(value.s); break;
1147 case T_INT: java_args.push_int(value.i); break;
1148 case T_LONG: java_args.push_long(value.j); break;
1149 case T_FLOAT: java_args.push_float(value.f); break;
1150 case T_DOUBLE: java_args.push_double(value.d); break;
1151 default:
1152 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
1153 }
1154 } else {
1155 if (arg != NULL) {
1156 Klass* k = java_lang_Class::as_Klass(type_mirror);
1157 if (!arg->is_a(k)) {
1158 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1159 "argument type mismatch");
1160 }
1161 }
1162 Handle arg_handle(THREAD, arg); // Create handle for argument
1163 java_args.push_oop(arg_handle); // Push handle
1164 }
1165 }
1166
1167 assert(java_args.size_of_parameters() == method->size_of_parameters(),
1168 "just checking");
1169
1170 // All oops (including receiver) is passed in as Handles. An potential oop is returned as an
1171 // oop (i.e., NOT as an handle)
1172 JavaValue result(rtype);
1173 JavaCalls::call(&result, method, &java_args, THREAD);
1174
1175 if (HAS_PENDING_EXCEPTION) {
1176 // Method threw an exception; wrap it in an InvocationTargetException
1177 oop target_exception = PENDING_EXCEPTION;
1178 CLEAR_PENDING_EXCEPTION;
1179 // JVMTI has already reported the pending exception
1180 // JVMTI internal flag reset is needed in order to report InvocationTargetException
1181 if (THREAD->is_Java_thread()) {
1182 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
1183 }
1184
1185 JavaCallArguments args(Handle(THREAD, target_exception));
1186 THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1187 vmSymbols::throwable_void_signature(),
1188 &args);
1189 } else {
1190 if (rtype == T_BOOLEAN || rtype == T_BYTE || rtype == T_CHAR || rtype == T_SHORT) {
1191 narrow((jvalue*)result.get_value_addr(), rtype, CHECK_NULL);
1192 }
1193 return Reflection::box((jvalue*)result.get_value_addr(), rtype, THREAD);
1194 }
1195}
1196
1197// This would be nicer if, say, java.lang.reflect.Method was a subclass
1198// of java.lang.reflect.Constructor
1199
1200oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) {
1201 oop mirror = java_lang_reflect_Method::clazz(method_mirror);
1202 int slot = java_lang_reflect_Method::slot(method_mirror);
1203 bool override = java_lang_reflect_Method::override(method_mirror) != 0;
1204 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror)));
1205
1206 oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror);
1207 BasicType rtype;
1208 if (java_lang_Class::is_primitive(return_type_mirror)) {
1209 rtype = basic_type_mirror_to_basic_type(return_type_mirror, CHECK_NULL);
1210 } else {
1211 rtype = T_OBJECT;
1212 }
1213
1214 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1215 Method* m = klass->method_with_idnum(slot);
1216 if (m == NULL) {
1217 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1218 }
1219 methodHandle method(THREAD, m);
1220
1221 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1222}
1223
1224
1225oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1226 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror);
1227 int slot = java_lang_reflect_Constructor::slot(constructor_mirror);
1228 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1229 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1230
1231 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1232 Method* m = klass->method_with_idnum(slot);
1233 if (m == NULL) {
1234 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1235 }
1236 methodHandle method(THREAD, m);
1237 assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1238
1239 // Make sure klass gets initialize
1240 klass->initialize(CHECK_NULL);
1241
1242 // Create new instance (the receiver)
1243 klass->check_valid_for_instantiation(false, CHECK_NULL);
1244 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1245
1246 // Ignore result from call and return receiver
1247 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1248 return receiver();
1249}
1250