< prev index next >

src/hotspot/share/runtime/reflection.cpp

Print this page

  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;

 253         value->s = typeArrayOop(a)->short_at(index);
 254         break;
 255       case T_INT:
 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 (a->is_objArray()) {
 274     if (value_type == T_OBJECT) {
 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     }
 284   } else {
 285     assert(a->is_typeArray(), "just checking");
 286     BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
 287     if (array_type != value_type) {
 288       // The widen operation can potentially throw an exception, but cannot block,
 289       // so typeArrayOop a is safe if the call succeeds.
 290       widen(value, value_type, array_type, CHECK);
 291     }
 292     switch (array_type) {
 293       case T_BOOLEAN:
 294         typeArrayOop(a)->bool_at_put(index, value->z);
 295         break;

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

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

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

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


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



 869   }


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

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

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

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

1142     THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1143   }
1144   methodHandle method(THREAD, m);
1145 
1146   return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1147 }
1148 
1149 
1150 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1151   oop mirror             = java_lang_reflect_Constructor::clazz(constructor_mirror);
1152   int slot               = java_lang_reflect_Constructor::slot(constructor_mirror);
1153   bool override          = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1154   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1155 
1156   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1157   Method* m = klass->method_with_idnum(slot);
1158   if (m == nullptr) {
1159     THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1160   }
1161   methodHandle method(THREAD, m);
1162   assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1163 
1164   // Make sure klass gets initialize
1165   klass->initialize(CHECK_NULL);
1166 
1167   // Create new instance (the receiver)
1168   klass->check_valid_for_instantiation(false, CHECK_NULL);
1169   Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1170 
1171   // Ignore result from call and return receiver
1172   invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1173   return receiver();
1174 }

  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/inlineKlass.inline.hpp"
  40 #include "oops/instanceKlass.inline.hpp"
  41 #include "oops/klass.inline.hpp"
  42 #include "oops/objArrayKlass.hpp"
  43 #include "oops/objArrayOop.inline.hpp"
  44 #include "oops/oop.inline.hpp"
  45 #include "oops/typeArrayOop.inline.hpp"
  46 #include "prims/jvmtiExport.hpp"
  47 #include "runtime/fieldDescriptor.inline.hpp"
  48 #include "runtime/handles.inline.hpp"
  49 #include "runtime/javaCalls.hpp"
  50 #include "runtime/javaThread.hpp"
  51 #include "runtime/reflection.hpp"
  52 #include "runtime/signature.hpp"
  53 #include "runtime/vframe.inline.hpp"
  54 #include "utilities/formatBuffer.hpp"
  55 #include "utilities/globalDefinitions.hpp"
  56 
  57 static void trace_class_resolution(oop mirror) {
  58   if (mirror == nullptr || java_lang_Class::is_primitive(mirror)) {
  59     return;
  60   }
  61   Klass* to_class = java_lang_Class::as_Klass(mirror);
  62   ResourceMark rm;
  63   int line_number = -1;
  64   const char * source_file = nullptr;
  65   Klass* caller = nullptr;
  66   JavaThread* jthread = JavaThread::current();
  67   if (jthread->has_last_Java_frame()) {
  68     vframeStream vfst(jthread);
  69     // skip over any frames belonging to java.lang.Class
  70     while (!vfst.at_end() &&
  71            vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class()) {
  72       vfst.next();
  73     }
  74     if (!vfst.at_end()) {
  75       // this frame is a likely suspect

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

 256         value->s = typeArrayOop(a)->short_at(index);
 257         break;
 258       case T_INT:
 259         value->i = typeArrayOop(a)->int_at(index);
 260         break;
 261       case T_LONG:
 262         value->j = typeArrayOop(a)->long_at(index);
 263         break;
 264       default:
 265         return T_ILLEGAL;
 266     }
 267     return type;
 268   }
 269 }
 270 
 271 
 272 void Reflection::array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS) {
 273   if (!a->is_within_bounds(index)) {
 274     THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
 275   }
 276 
 277   if (a->is_objArray()) {
 278     if (value_type == T_OBJECT) {
 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     }
 292   } else {
 293     assert(a->is_typeArray(), "just checking");
 294     BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
 295     if (array_type != value_type) {
 296       // The widen operation can potentially throw an exception, but cannot block,
 297       // so typeArrayOop a is safe if the call succeeds.
 298       widen(value, value_type, array_type, CHECK);
 299     }
 300     switch (array_type) {
 301       case T_BOOLEAN:
 302         typeArrayOop(a)->bool_at_put(index, value->z);
 303         break;

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



 768   return Handle(THREAD, nt);
 769 }
 770 
 771 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
 772   // Allow jdk.internal.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
 773   assert(!method()->name()->starts_with('<') || for_constant_pool_access,
 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 {

1155     THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1156   }
1157   methodHandle method(THREAD, m);
1158 
1159   return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1160 }
1161 
1162 
1163 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1164   oop mirror             = java_lang_reflect_Constructor::clazz(constructor_mirror);
1165   int slot               = java_lang_reflect_Constructor::slot(constructor_mirror);
1166   bool override          = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1167   objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1168 
1169   InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1170   Method* m = klass->method_with_idnum(slot);
1171   if (m == nullptr) {
1172     THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1173   }
1174   methodHandle method(THREAD, m);

1175 
1176   // Make sure klass gets initialize
1177   klass->initialize(CHECK_NULL);
1178 
1179   // Create new instance (the receiver)
1180   klass->check_valid_for_instantiation(false, CHECK_NULL);
1181   Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1182 
1183   // Ignore result from call and return receiver
1184   invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1185   return receiver();
1186 }
< prev index next >