annotate src/share/vm/runtime/reflection.cpp @ 4775:a589c78a8811

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