src/share/vm/runtime/reflection.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 9572
624a0741915c
child 10015
eb7ce841ccec
permissions
-rw-r--r--

Merge

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

mercurial