< prev index next >

src/hotspot/share/runtime/reflection.cpp

Print this page

   1 /*
   2  * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "cds/cdsConfig.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/moduleEntry.hpp"
  28 #include "classfile/packageEntry.hpp"
  29 #include "classfile/stringTable.hpp"
  30 #include "classfile/verifier.hpp"
  31 #include "classfile/vmClasses.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "interpreter/linkResolver.hpp"
  34 #include "jvm.h"
  35 #include "logging/log.hpp"
  36 #include "memory/oopFactory.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.hpp"


  39 #include "oops/instanceKlass.inline.hpp"
  40 #include "oops/klass.inline.hpp"
  41 #include "oops/objArrayKlass.hpp"
  42 #include "oops/objArrayOop.inline.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "oops/typeArrayOop.inline.hpp"
  45 #include "prims/jvmtiExport.hpp"
  46 #include "runtime/fieldDescriptor.inline.hpp"
  47 #include "runtime/handles.inline.hpp"
  48 #include "runtime/javaCalls.hpp"
  49 #include "runtime/javaThread.hpp"
  50 #include "runtime/reflection.hpp"
  51 #include "runtime/signature.hpp"
  52 #include "runtime/vframe.inline.hpp"
  53 #include "utilities/formatBuffer.hpp"

  54 
  55 static void trace_class_resolution(oop mirror) {
  56   if (mirror == nullptr || java_lang_Class::is_primitive(mirror)) {
  57     return;
  58   }
  59   Klass* to_class = java_lang_Class::as_Klass(mirror);
  60   ResourceMark rm;
  61   int line_number = -1;
  62   const char * source_file = nullptr;
  63   Klass* caller = nullptr;
  64   JavaThread* jthread = JavaThread::current();
  65   if (jthread->has_last_Java_frame()) {
  66     vframeStream vfst(jthread);
  67     // skip over any frames belonging to java.lang.Class
  68     while (!vfst.at_end() &&
  69            vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class()) {
  70       vfst.next();
  71     }
  72     if (!vfst.at_end()) {
  73       // this frame is a likely suspect

 211           return;
 212         case T_LONG:
 213           value->d = (jdouble) value->j;
 214           return;
 215         default:
 216           break;
 217       }
 218       break;  // fail
 219     default:
 220       break;  // fail
 221   }
 222   THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
 223 }
 224 
 225 
 226 BasicType Reflection::array_get(jvalue* value, arrayOop a, int index, TRAPS) {
 227   if (!a->is_within_bounds(index)) {
 228     THROW_(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), T_ILLEGAL);
 229   }
 230   if (a->is_objArray()) {
 231     value->l = cast_from_oop<jobject>(objArrayOop(a)->obj_at(index));

 232     return T_OBJECT;
 233   } else {
 234     assert(a->is_typeArray(), "just checking");
 235     BasicType type = TypeArrayKlass::cast(a->klass())->element_type();
 236     switch (type) {
 237       case T_BOOLEAN:
 238         value->z = typeArrayOop(a)->bool_at(index);
 239         break;
 240       case T_CHAR:
 241         value->c = typeArrayOop(a)->char_at(index);
 242         break;
 243       case T_FLOAT:
 244         value->f = typeArrayOop(a)->float_at(index);
 245         break;
 246       case T_DOUBLE:
 247         value->d = typeArrayOop(a)->double_at(index);
 248         break;
 249       case T_BYTE:
 250         value->b = typeArrayOop(a)->byte_at(index);
 251         break;

 256         value->i = typeArrayOop(a)->int_at(index);
 257         break;
 258       case T_LONG:
 259         value->j = typeArrayOop(a)->long_at(index);
 260         break;
 261       default:
 262         return T_ILLEGAL;
 263     }
 264     return type;
 265   }
 266 }
 267 
 268 
 269 void Reflection::array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS) {
 270   if (!a->is_within_bounds(index)) {
 271     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 272   }
 273   if (value_type == T_OBJECT) {
 274     assert(a->is_objArray(), "just checking");
 275     oop obj = cast_to_oop(value->l);




 276     if (obj != nullptr) {
 277       Klass* element_klass = ObjArrayKlass::cast(a->klass())->element_klass();
 278       if (!obj->is_a(element_klass)) {
 279         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
 280       }
 281     }
 282     objArrayOop(a)->obj_at_put(index, obj);
 283   } else {
 284     assert(a->is_typeArray(), "just checking");
 285     BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
 286     if (array_type != value_type) {
 287       // The widen operation can potentially throw an exception, but cannot block,
 288       // so typeArrayOop a is safe if the call succeeds.
 289       widen(value, value_type, array_type, CHECK);
 290     }
 291     switch (array_type) {
 292       case T_BOOLEAN:
 293         typeArrayOop(a)->bool_at_put(index, value->z);
 294         break;
 295       case T_CHAR:

 739     }
 740     if (!ss.at_return_type()) {
 741       mirrors->obj_at_put(index++, mirror);
 742     } else if (return_type != nullptr) {
 743       // Collect return type as well
 744       assert(ss.at_return_type(), "return type should be present");
 745       *return_type = mirror;
 746     }
 747   }
 748   assert(index == parameter_count, "invalid parameter count");
 749   return mirrors;
 750 }
 751 
 752 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
 753   return method->resolved_checked_exceptions(THREAD);
 754 }
 755 
 756 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
 757   ResolvingSignatureStream ss(signature, k, false);
 758   oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
 759   if (log_is_enabled(Debug, class, resolve)) {
 760     trace_class_resolution(nt);
 761   }
 762   return Handle(THREAD, nt);
 763 }
 764 
 765 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
 766   // Allow jdk.internal.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
 767   assert(!method()->is_object_initializer() &&
 768          (for_constant_pool_access || !method()->is_static_initializer()),
 769          "Should not be the initializer");
 770   InstanceKlass* holder = method->method_holder();
 771   int slot = method->method_idnum();
 772 
 773   Symbol*  signature  = method->signature();
 774   int parameter_count = ArgumentCount(signature).size();
 775   oop return_type_oop = nullptr;
 776   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
 777   if (parameter_types.is_null() || return_type_oop == nullptr) return nullptr;
 778 
 779   Handle return_type(THREAD, return_type_oop);
 780 
 781   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 782   assert(!exception_types.is_null(), "cannot return null");
 783 
 784   Symbol*  method_name = method->name();
 785   oop name_oop = StringTable::intern(method_name, CHECK_NULL);
 786   Handle name = Handle(THREAD, name_oop);
 787   if (name == nullptr) return nullptr;
 788 
 789   const int modifiers = method->access_flags().as_method_flags();

 797   java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
 798   java_lang_reflect_Method::set_exception_types(mh(), exception_types());
 799   java_lang_reflect_Method::set_modifiers(mh(), modifiers);
 800   java_lang_reflect_Method::set_override(mh(), false);
 801   if (method->generic_signature() != nullptr) {
 802     Symbol*  gs = method->generic_signature();
 803     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 804     java_lang_reflect_Method::set_signature(mh(), sig());
 805   }
 806   typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
 807   java_lang_reflect_Method::set_annotations(mh(), an_oop);
 808   an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
 809   java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
 810   an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
 811   java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
 812   return mh();
 813 }
 814 
 815 
 816 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
 817   assert(method()->is_object_initializer(), "Should be the initializer");

 818 
 819   InstanceKlass* holder = method->method_holder();
 820   int slot = method->method_idnum();
 821 
 822   Symbol*  signature  = method->signature();
 823   int parameter_count = ArgumentCount(signature).size();
 824   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, nullptr, CHECK_NULL);
 825   if (parameter_types.is_null()) return nullptr;
 826 
 827   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 828   assert(!exception_types.is_null(), "cannot return null");
 829 
 830   const int modifiers = method->access_flags().as_method_flags();
 831 
 832   Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
 833 
 834   java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
 835   java_lang_reflect_Constructor::set_slot(ch(), slot);
 836   java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
 837   java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());

 846   java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
 847   an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
 848   java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
 849   return ch();
 850 }
 851 
 852 
 853 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
 854   Symbol*  field_name = fd->name();
 855   oop name_oop = StringTable::intern(field_name, CHECK_NULL);
 856   Handle name = Handle(THREAD, name_oop);
 857   Symbol*  signature  = fd->signature();
 858   InstanceKlass* holder = fd->field_holder();
 859   Handle type = new_type(signature, holder, CHECK_NULL);
 860   Handle rh  = java_lang_reflect_Field::create(CHECK_NULL);
 861 
 862   java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
 863   java_lang_reflect_Field::set_slot(rh(), fd->index());
 864   java_lang_reflect_Field::set_name(rh(), name());
 865   java_lang_reflect_Field::set_type(rh(), type());


 866   if (fd->is_trusted_final()) {
 867     java_lang_reflect_Field::set_trusted_final(rh());



 868   }


 869   // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
 870   java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_field_flags());
 871   java_lang_reflect_Field::set_override(rh(), false);
 872   if (fd->has_generic_signature()) {
 873     Symbol*  gs = fd->generic_signature();
 874     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 875     java_lang_reflect_Field::set_signature(rh(), sig());
 876   }
 877   typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
 878   java_lang_reflect_Field::set_annotations(rh(), an_oop);
 879   return rh();
 880 }
 881 
 882 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
 883                               int flags, TRAPS) {
 884 
 885   Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
 886 
 887   if(nullptr != sym) {
 888     Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);

 959   // Ensure klass is initialized
 960   klass->initialize(CHECK_NULL);
 961 
 962   bool is_static = reflected_method->is_static();
 963   if (is_static) {
 964     // ignore receiver argument
 965     method = reflected_method;
 966     target_klass = klass;
 967   } else {
 968     // check for null receiver
 969     if (receiver.is_null()) {
 970       THROW_NULL(vmSymbols::java_lang_NullPointerException());
 971     }
 972     // Check class of receiver against class declaring method
 973     if (!receiver->is_a(klass)) {
 974       THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
 975     }
 976     // target klass is receiver's klass
 977     target_klass = receiver->klass();
 978     // no need to resolve if method is private or <init>
 979     if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) {

 980       method = reflected_method;
 981     } else {
 982       // resolve based on the receiver
 983       if (reflected_method->method_holder()->is_interface()) {
 984         // resolve interface call
 985         //
 986         // Match resolution errors with those thrown due to reflection inlining
 987         // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
 988         method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
 989         if (HAS_PENDING_EXCEPTION) {
 990           // Method resolution threw an exception; wrap it in an InvocationTargetException
 991           oop resolution_exception = PENDING_EXCEPTION;
 992           CLEAR_PENDING_EXCEPTION;
 993           // JVMTI has already reported the pending exception
 994           // JVMTI internal flag reset is needed in order to report InvocationTargetException
 995           JvmtiExport::clear_detected_exception(THREAD);
 996           JavaCallArguments args(Handle(THREAD, resolution_exception));
 997           THROW_ARG_NULL(vmSymbols::java_lang_reflect_InvocationTargetException(),
 998                          vmSymbols::throwable_void_signature(),
 999                          &args);

1039     ss.print("'");
1040     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
1041   }
1042 
1043   assert(ptypes->is_objArray(), "just checking");
1044   int args_len = args.is_null() ? 0 : args->length();
1045   // Check number of arguments
1046   if (ptypes->length() != args_len) {
1047     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
1048                    "wrong number of arguments");
1049   }
1050 
1051   // Create object to contain parameters for the JavaCall
1052   JavaCallArguments java_args(method->size_of_parameters());
1053 
1054   if (!is_static) {
1055     java_args.push_oop(receiver);
1056   }
1057 
1058   for (int i = 0; i < args_len; i++) {
1059     oop type_mirror = ptypes->obj_at(i);
1060     oop arg = args->obj_at(i);
1061     if (java_lang_Class::is_primitive(type_mirror)) {
1062       jvalue value;
1063       BasicType ptype = basic_type_mirror_to_basic_type(type_mirror);
1064       BasicType atype = Reflection::unbox_for_primitive(arg, &value, CHECK_NULL);
1065       if (ptype != atype) {
1066         Reflection::widen(&value, atype, ptype, CHECK_NULL);
1067       }
1068       switch (ptype) {
1069         case T_BOOLEAN:     java_args.push_int(value.z);    break;
1070         case T_CHAR:        java_args.push_int(value.c);    break;
1071         case T_BYTE:        java_args.push_int(value.b);    break;
1072         case T_SHORT:       java_args.push_int(value.s);    break;
1073         case T_INT:         java_args.push_int(value.i);    break;
1074         case T_LONG:        java_args.push_long(value.j);   break;
1075         case T_FLOAT:       java_args.push_float(value.f);  break;
1076         case T_DOUBLE:      java_args.push_double(value.d); break;
1077         default:
1078           THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
1079       }
1080     } else {

   1 /*
   2  * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "cds/cdsConfig.hpp"
  26 #include "classfile/javaClasses.inline.hpp"
  27 #include "classfile/moduleEntry.hpp"
  28 #include "classfile/packageEntry.hpp"
  29 #include "classfile/stringTable.hpp"
  30 #include "classfile/verifier.hpp"
  31 #include "classfile/vmClasses.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "interpreter/linkResolver.hpp"
  34 #include "jvm.h"
  35 #include "logging/log.hpp"
  36 #include "memory/oopFactory.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "memory/universe.hpp"
  39 #include "oops/arrayOop.inline.hpp"
  40 #include "oops/inlineKlass.inline.hpp"
  41 #include "oops/instanceKlass.inline.hpp"
  42 #include "oops/klass.inline.hpp"
  43 #include "oops/objArrayKlass.hpp"
  44 #include "oops/objArrayOop.inline.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "oops/typeArrayOop.inline.hpp"
  47 #include "prims/jvmtiExport.hpp"
  48 #include "runtime/fieldDescriptor.inline.hpp"
  49 #include "runtime/handles.inline.hpp"
  50 #include "runtime/javaCalls.hpp"
  51 #include "runtime/javaThread.hpp"
  52 #include "runtime/reflection.hpp"
  53 #include "runtime/signature.hpp"
  54 #include "runtime/vframe.inline.hpp"
  55 #include "utilities/formatBuffer.hpp"
  56 #include "utilities/globalDefinitions.hpp"
  57 
  58 static void trace_class_resolution(oop mirror) {
  59   if (mirror == nullptr || java_lang_Class::is_primitive(mirror)) {
  60     return;
  61   }
  62   Klass* to_class = java_lang_Class::as_Klass(mirror);
  63   ResourceMark rm;
  64   int line_number = -1;
  65   const char * source_file = nullptr;
  66   Klass* caller = nullptr;
  67   JavaThread* jthread = JavaThread::current();
  68   if (jthread->has_last_Java_frame()) {
  69     vframeStream vfst(jthread);
  70     // skip over any frames belonging to java.lang.Class
  71     while (!vfst.at_end() &&
  72            vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class()) {
  73       vfst.next();
  74     }
  75     if (!vfst.at_end()) {
  76       // this frame is a likely suspect

 214           return;
 215         case T_LONG:
 216           value->d = (jdouble) value->j;
 217           return;
 218         default:
 219           break;
 220       }
 221       break;  // fail
 222     default:
 223       break;  // fail
 224   }
 225   THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
 226 }
 227 
 228 
 229 BasicType Reflection::array_get(jvalue* value, arrayOop a, int index, TRAPS) {
 230   if (!a->is_within_bounds(index)) {
 231     THROW_(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), T_ILLEGAL);
 232   }
 233   if (a->is_objArray()) {
 234     oop o = objArrayOop(a)->obj_at(index, CHECK_(T_ILLEGAL)); // reading from a flat array can throw an OOM
 235     value->l = cast_from_oop<jobject>(o);
 236     return T_OBJECT;
 237   } else {
 238     assert(a->is_typeArray(), "just checking");
 239     BasicType type = TypeArrayKlass::cast(a->klass())->element_type();
 240     switch (type) {
 241       case T_BOOLEAN:
 242         value->z = typeArrayOop(a)->bool_at(index);
 243         break;
 244       case T_CHAR:
 245         value->c = typeArrayOop(a)->char_at(index);
 246         break;
 247       case T_FLOAT:
 248         value->f = typeArrayOop(a)->float_at(index);
 249         break;
 250       case T_DOUBLE:
 251         value->d = typeArrayOop(a)->double_at(index);
 252         break;
 253       case T_BYTE:
 254         value->b = typeArrayOop(a)->byte_at(index);
 255         break;

 260         value->i = typeArrayOop(a)->int_at(index);
 261         break;
 262       case T_LONG:
 263         value->j = typeArrayOop(a)->long_at(index);
 264         break;
 265       default:
 266         return T_ILLEGAL;
 267     }
 268     return type;
 269   }
 270 }
 271 
 272 
 273 void Reflection::array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS) {
 274   if (!a->is_within_bounds(index)) {
 275     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 276   }
 277   if (value_type == T_OBJECT) {
 278     assert(a->is_objArray(), "just checking");
 279     oop obj = cast_to_oop(value->l);
 280     if (a->is_null_free_array() && obj == nullptr) {
 281       THROW_MSG(vmSymbols::java_lang_NullPointerException(), "null-restricted array");
 282     }
 283 
 284     if (obj != nullptr) {
 285       Klass* element_klass = ObjArrayKlass::cast(a->klass())->element_klass();
 286       if (!obj->is_a(element_klass)) {
 287         THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
 288       }
 289     }
 290     objArrayOop(a)->obj_at_put(index, obj);
 291   } else {
 292     assert(a->is_typeArray(), "just checking");
 293     BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
 294     if (array_type != value_type) {
 295       // The widen operation can potentially throw an exception, but cannot block,
 296       // so typeArrayOop a is safe if the call succeeds.
 297       widen(value, value_type, array_type, CHECK);
 298     }
 299     switch (array_type) {
 300       case T_BOOLEAN:
 301         typeArrayOop(a)->bool_at_put(index, value->z);
 302         break;
 303       case T_CHAR:

 747     }
 748     if (!ss.at_return_type()) {
 749       mirrors->obj_at_put(index++, mirror);
 750     } else if (return_type != nullptr) {
 751       // Collect return type as well
 752       assert(ss.at_return_type(), "return type should be present");
 753       *return_type = mirror;
 754     }
 755   }
 756   assert(index == parameter_count, "invalid parameter count");
 757   return mirrors;
 758 }
 759 
 760 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
 761   return method->resolved_checked_exceptions(THREAD);
 762 }
 763 
 764 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
 765   ResolvingSignatureStream ss(signature, k, false);
 766   oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);



 767   return Handle(THREAD, nt);
 768 }
 769 
 770 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
 771   // Allow jdk.internal.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
 772   assert(!method()->is_object_constructor() ||
 773          (for_constant_pool_access || !method()->is_class_initializer()),
 774          "should call new_constructor instead");
 775   InstanceKlass* holder = method->method_holder();
 776   int slot = method->method_idnum();
 777 
 778   Symbol*  signature  = method->signature();
 779   int parameter_count = ArgumentCount(signature).size();
 780   oop return_type_oop = nullptr;
 781   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
 782   if (parameter_types.is_null() || return_type_oop == nullptr) return nullptr;
 783 
 784   Handle return_type(THREAD, return_type_oop);
 785 
 786   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 787   assert(!exception_types.is_null(), "cannot return null");
 788 
 789   Symbol*  method_name = method->name();
 790   oop name_oop = StringTable::intern(method_name, CHECK_NULL);
 791   Handle name = Handle(THREAD, name_oop);
 792   if (name == nullptr) return nullptr;
 793 
 794   const int modifiers = method->access_flags().as_method_flags();

 802   java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
 803   java_lang_reflect_Method::set_exception_types(mh(), exception_types());
 804   java_lang_reflect_Method::set_modifiers(mh(), modifiers);
 805   java_lang_reflect_Method::set_override(mh(), false);
 806   if (method->generic_signature() != nullptr) {
 807     Symbol*  gs = method->generic_signature();
 808     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 809     java_lang_reflect_Method::set_signature(mh(), sig());
 810   }
 811   typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
 812   java_lang_reflect_Method::set_annotations(mh(), an_oop);
 813   an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
 814   java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
 815   an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
 816   java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
 817   return mh();
 818 }
 819 
 820 
 821 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
 822   assert(method()->is_object_constructor(),
 823          "should call new_method instead");
 824 
 825   InstanceKlass* holder = method->method_holder();
 826   int slot = method->method_idnum();
 827 
 828   Symbol*  signature  = method->signature();
 829   int parameter_count = ArgumentCount(signature).size();
 830   objArrayHandle parameter_types = get_parameter_types(method, parameter_count, nullptr, CHECK_NULL);
 831   if (parameter_types.is_null()) return nullptr;
 832 
 833   objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
 834   assert(!exception_types.is_null(), "cannot return null");
 835 
 836   const int modifiers = method->access_flags().as_method_flags();
 837 
 838   Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
 839 
 840   java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
 841   java_lang_reflect_Constructor::set_slot(ch(), slot);
 842   java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
 843   java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());

 852   java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
 853   an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
 854   java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
 855   return ch();
 856 }
 857 
 858 
 859 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
 860   Symbol*  field_name = fd->name();
 861   oop name_oop = StringTable::intern(field_name, CHECK_NULL);
 862   Handle name = Handle(THREAD, name_oop);
 863   Symbol*  signature  = fd->signature();
 864   InstanceKlass* holder = fd->field_holder();
 865   Handle type = new_type(signature, holder, CHECK_NULL);
 866   Handle rh  = java_lang_reflect_Field::create(CHECK_NULL);
 867 
 868   java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
 869   java_lang_reflect_Field::set_slot(rh(), fd->index());
 870   java_lang_reflect_Field::set_name(rh(), name());
 871   java_lang_reflect_Field::set_type(rh(), type());
 872 
 873   int flags = 0;
 874   if (fd->is_trusted_final()) {
 875     flags |= TRUSTED_FINAL;
 876   }
 877   if (fd->is_null_free_inline_type()) {
 878     flags |= NULL_RESTRICTED;
 879   }
 880   java_lang_reflect_Field::set_flags(rh(), flags);
 881 
 882   // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
 883   java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_field_flags());
 884   java_lang_reflect_Field::set_override(rh(), false);
 885   if (fd->has_generic_signature()) {
 886     Symbol*  gs = fd->generic_signature();
 887     Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
 888     java_lang_reflect_Field::set_signature(rh(), sig());
 889   }
 890   typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
 891   java_lang_reflect_Field::set_annotations(rh(), an_oop);
 892   return rh();
 893 }
 894 
 895 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
 896                               int flags, TRAPS) {
 897 
 898   Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
 899 
 900   if(nullptr != sym) {
 901     Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);

 972   // Ensure klass is initialized
 973   klass->initialize(CHECK_NULL);
 974 
 975   bool is_static = reflected_method->is_static();
 976   if (is_static) {
 977     // ignore receiver argument
 978     method = reflected_method;
 979     target_klass = klass;
 980   } else {
 981     // check for null receiver
 982     if (receiver.is_null()) {
 983       THROW_NULL(vmSymbols::java_lang_NullPointerException());
 984     }
 985     // Check class of receiver against class declaring method
 986     if (!receiver->is_a(klass)) {
 987       THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
 988     }
 989     // target klass is receiver's klass
 990     target_klass = receiver->klass();
 991     // no need to resolve if method is private or <init>
 992     if (reflected_method->is_private() ||
 993         reflected_method->name() == vmSymbols::object_initializer_name()) {
 994       method = reflected_method;
 995     } else {
 996       // resolve based on the receiver
 997       if (reflected_method->method_holder()->is_interface()) {
 998         // resolve interface call
 999         //
1000         // Match resolution errors with those thrown due to reflection inlining
1001         // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
1002         method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
1003         if (HAS_PENDING_EXCEPTION) {
1004           // Method resolution threw an exception; wrap it in an InvocationTargetException
1005           oop resolution_exception = PENDING_EXCEPTION;
1006           CLEAR_PENDING_EXCEPTION;
1007           // JVMTI has already reported the pending exception
1008           // JVMTI internal flag reset is needed in order to report InvocationTargetException
1009           JvmtiExport::clear_detected_exception(THREAD);
1010           JavaCallArguments args(Handle(THREAD, resolution_exception));
1011           THROW_ARG_NULL(vmSymbols::java_lang_reflect_InvocationTargetException(),
1012                          vmSymbols::throwable_void_signature(),
1013                          &args);

1053     ss.print("'");
1054     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
1055   }
1056 
1057   assert(ptypes->is_objArray(), "just checking");
1058   int args_len = args.is_null() ? 0 : args->length();
1059   // Check number of arguments
1060   if (ptypes->length() != args_len) {
1061     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
1062                    "wrong number of arguments");
1063   }
1064 
1065   // Create object to contain parameters for the JavaCall
1066   JavaCallArguments java_args(method->size_of_parameters());
1067 
1068   if (!is_static) {
1069     java_args.push_oop(receiver);
1070   }
1071 
1072   for (int i = 0; i < args_len; i++) {
1073     oop type_mirror = ptypes->obj_at(i, CHECK_NULL);
1074     oop arg = args->obj_at(i, CHECK_NULL);
1075     if (java_lang_Class::is_primitive(type_mirror)) {
1076       jvalue value;
1077       BasicType ptype = basic_type_mirror_to_basic_type(type_mirror);
1078       BasicType atype = Reflection::unbox_for_primitive(arg, &value, CHECK_NULL);
1079       if (ptype != atype) {
1080         Reflection::widen(&value, atype, ptype, CHECK_NULL);
1081       }
1082       switch (ptype) {
1083         case T_BOOLEAN:     java_args.push_int(value.z);    break;
1084         case T_CHAR:        java_args.push_int(value.c);    break;
1085         case T_BYTE:        java_args.push_int(value.b);    break;
1086         case T_SHORT:       java_args.push_int(value.s);    break;
1087         case T_INT:         java_args.push_int(value.i);    break;
1088         case T_LONG:        java_args.push_long(value.j);   break;
1089         case T_FLOAT:       java_args.push_float(value.f);  break;
1090         case T_DOUBLE:      java_args.push_double(value.d); break;
1091         default:
1092           THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
1093       }
1094     } else {
< prev index next >