20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "cds/cdsConfig.hpp"
27 #include "classfile/javaClasses.inline.hpp"
28 #include "classfile/moduleEntry.hpp"
29 #include "classfile/packageEntry.hpp"
30 #include "classfile/stringTable.hpp"
31 #include "classfile/verifier.hpp"
32 #include "classfile/vmClasses.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "interpreter/linkResolver.hpp"
35 #include "jvm.h"
36 #include "logging/log.hpp"
37 #include "memory/oopFactory.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "memory/universe.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/reflectionUtils.hpp"
53 #include "runtime/signature.hpp"
54 #include "runtime/vframe.inline.hpp"
55 #include "utilities/formatBuffer.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
255 value->s = typeArrayOop(a)->short_at(index);
256 break;
257 case T_INT:
258 value->i = typeArrayOop(a)->int_at(index);
259 break;
260 case T_LONG:
261 value->j = typeArrayOop(a)->long_at(index);
262 break;
263 default:
264 return T_ILLEGAL;
265 }
266 return type;
267 }
268 }
269
270
271 void Reflection::array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS) {
272 if (!a->is_within_bounds(index)) {
273 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
274 }
275 if (a->is_objArray()) {
276 if (value_type == T_OBJECT) {
277 oop obj = cast_to_oop(value->l);
278 if (obj != nullptr) {
279 Klass* element_klass = ObjArrayKlass::cast(a->klass())->element_klass();
280 if (!obj->is_a(element_klass)) {
281 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
282 }
283 }
284 objArrayOop(a)->obj_at_put(index, obj);
285 }
286 } else {
287 assert(a->is_typeArray(), "just checking");
288 BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
289 if (array_type != value_type) {
290 // The widen operation can potentially throw an exception, but cannot block,
291 // so typeArrayOop a is safe if the call succeeds.
292 widen(value, value_type, array_type, CHECK);
293 }
294 switch (array_type) {
295 case T_BOOLEAN:
296 typeArrayOop(a)->bool_at_put(index, value->z);
297 break;
749 }
750 if (!ss.at_return_type()) {
751 mirrors->obj_at_put(index++, mirror);
752 } else if (return_type != nullptr) {
753 // Collect return type as well
754 assert(ss.at_return_type(), "return type should be present");
755 *return_type = mirror;
756 }
757 }
758 assert(index == parameter_count, "invalid parameter count");
759 return mirrors;
760 }
761
762 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
763 return method->resolved_checked_exceptions(THREAD);
764 }
765
766 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
767 ResolvingSignatureStream ss(signature, k, false);
768 oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
769 if (log_is_enabled(Debug, class, resolve)) {
770 trace_class_resolution(nt);
771 }
772 return Handle(THREAD, nt);
773 }
774
775 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
776 // Allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
777 assert(!method()->is_initializer() ||
778 (for_constant_pool_access && method()->is_static()),
779 "should call new_constructor instead");
780 InstanceKlass* holder = method->method_holder();
781 int slot = method->method_idnum();
782
783 Symbol* signature = method->signature();
784 int parameter_count = ArgumentCount(signature).size();
785 oop return_type_oop = nullptr;
786 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
787 if (parameter_types.is_null() || return_type_oop == nullptr) return nullptr;
788
789 Handle return_type(THREAD, return_type_oop);
790
791 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
792 assert(!exception_types.is_null(), "cannot return null");
793
794 Symbol* method_name = method->name();
795 oop name_oop = StringTable::intern(method_name, CHECK_NULL);
796 Handle name = Handle(THREAD, name_oop);
797 if (name == nullptr) return nullptr;
798
807 java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
808 java_lang_reflect_Method::set_exception_types(mh(), exception_types());
809 java_lang_reflect_Method::set_modifiers(mh(), modifiers);
810 java_lang_reflect_Method::set_override(mh(), false);
811 if (method->generic_signature() != nullptr) {
812 Symbol* gs = method->generic_signature();
813 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
814 java_lang_reflect_Method::set_signature(mh(), sig());
815 }
816 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
817 java_lang_reflect_Method::set_annotations(mh(), an_oop);
818 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
819 java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
820 an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
821 java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
822 return mh();
823 }
824
825
826 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
827 assert(method()->is_initializer(), "should call new_method instead");
828
829 InstanceKlass* holder = method->method_holder();
830 int slot = method->method_idnum();
831
832 Symbol* signature = method->signature();
833 int parameter_count = ArgumentCount(signature).size();
834 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, nullptr, CHECK_NULL);
835 if (parameter_types.is_null()) return nullptr;
836
837 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
838 assert(!exception_types.is_null(), "cannot return null");
839
840 const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
841
842 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
843
844 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
845 java_lang_reflect_Constructor::set_slot(ch(), slot);
846 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
847 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
856 java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
857 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
858 java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
859 return ch();
860 }
861
862
863 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
864 Symbol* field_name = fd->name();
865 oop name_oop = StringTable::intern(field_name, CHECK_NULL);
866 Handle name = Handle(THREAD, name_oop);
867 Symbol* signature = fd->signature();
868 InstanceKlass* holder = fd->field_holder();
869 Handle type = new_type(signature, holder, CHECK_NULL);
870 Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
871
872 java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
873 java_lang_reflect_Field::set_slot(rh(), fd->index());
874 java_lang_reflect_Field::set_name(rh(), name());
875 java_lang_reflect_Field::set_type(rh(), type());
876 if (fd->is_trusted_final()) {
877 java_lang_reflect_Field::set_trusted_final(rh());
878 }
879 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
880 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
881 java_lang_reflect_Field::set_override(rh(), false);
882 if (fd->has_generic_signature()) {
883 Symbol* gs = fd->generic_signature();
884 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
885 java_lang_reflect_Field::set_signature(rh(), sig());
886 }
887 typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
888 java_lang_reflect_Field::set_annotations(rh(), an_oop);
889 return rh();
890 }
891
892 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
893 int flags, TRAPS) {
894
895 Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
896
897 if(nullptr != sym) {
898 Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
899 java_lang_reflect_Parameter::set_name(rh(), name());
900 } else {
976 // Ensure klass is initialized
977 klass->initialize(CHECK_NULL);
978
979 bool is_static = reflected_method->is_static();
980 if (is_static) {
981 // ignore receiver argument
982 method = reflected_method;
983 target_klass = klass;
984 } else {
985 // check for null receiver
986 if (receiver.is_null()) {
987 THROW_0(vmSymbols::java_lang_NullPointerException());
988 }
989 // Check class of receiver against class declaring method
990 if (!receiver->is_a(klass)) {
991 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
992 }
993 // target klass is receiver's klass
994 target_klass = receiver->klass();
995 // no need to resolve if method is private or <init>
996 if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) {
997 method = reflected_method;
998 } else {
999 // resolve based on the receiver
1000 if (reflected_method->method_holder()->is_interface()) {
1001 // resolve interface call
1002 //
1003 // Match resolution errors with those thrown due to reflection inlining
1004 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
1005 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
1006 if (HAS_PENDING_EXCEPTION) {
1007 // Method resolution threw an exception; wrap it in an InvocationTargetException
1008 oop resolution_exception = PENDING_EXCEPTION;
1009 CLEAR_PENDING_EXCEPTION;
1010 // JVMTI has already reported the pending exception
1011 // JVMTI internal flag reset is needed in order to report InvocationTargetException
1012 JvmtiExport::clear_detected_exception(THREAD);
1013 JavaCallArguments args(Handle(THREAD, resolution_exception));
1014 THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1015 vmSymbols::throwable_void_signature(),
1016 &args);
1158 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1159 }
1160 methodHandle method(THREAD, m);
1161
1162 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1163 }
1164
1165
1166 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1167 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror);
1168 int slot = java_lang_reflect_Constructor::slot(constructor_mirror);
1169 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1170 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1171
1172 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1173 Method* m = klass->method_with_idnum(slot);
1174 if (m == nullptr) {
1175 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1176 }
1177 methodHandle method(THREAD, m);
1178 assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1179
1180 // Make sure klass gets initialize
1181 klass->initialize(CHECK_NULL);
1182
1183 // Create new instance (the receiver)
1184 klass->check_valid_for_instantiation(false, CHECK_NULL);
1185 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1186
1187 // Ignore result from call and return receiver
1188 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1189 return receiver();
1190 }
|
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "cds/cdsConfig.hpp"
27 #include "classfile/javaClasses.inline.hpp"
28 #include "classfile/moduleEntry.hpp"
29 #include "classfile/packageEntry.hpp"
30 #include "classfile/stringTable.hpp"
31 #include "classfile/verifier.hpp"
32 #include "classfile/vmClasses.hpp"
33 #include "classfile/vmSymbols.hpp"
34 #include "interpreter/linkResolver.hpp"
35 #include "jvm.h"
36 #include "logging/log.hpp"
37 #include "memory/oopFactory.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "memory/universe.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/reflectionUtils.hpp"
54 #include "runtime/signature.hpp"
55 #include "runtime/vframe.inline.hpp"
56 #include "utilities/formatBuffer.hpp"
57 #include "utilities/globalDefinitions.hpp"
58
59 static void trace_class_resolution(oop mirror) {
60 if (mirror == nullptr || java_lang_Class::is_primitive(mirror)) {
61 return;
62 }
63 Klass* to_class = java_lang_Class::as_Klass(mirror);
64 ResourceMark rm;
65 int line_number = -1;
66 const char * source_file = nullptr;
67 Klass* caller = nullptr;
68 JavaThread* jthread = JavaThread::current();
69 if (jthread->has_last_Java_frame()) {
70 vframeStream vfst(jthread);
71 // skip over any frames belonging to java.lang.Class
72 while (!vfst.at_end() &&
73 vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class()) {
74 vfst.next();
75 }
76 if (!vfst.at_end()) {
77 // this frame is a likely suspect
257 value->s = typeArrayOop(a)->short_at(index);
258 break;
259 case T_INT:
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
278 if (a->is_objArray()) {
279 if (value_type == T_OBJECT) {
280 oop obj = cast_to_oop(value->l);
281 if (a->is_null_free_array() && obj == nullptr) {
282 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "null-restricted array");
283 }
284
285 if (obj != nullptr) {
286 Klass* element_klass = ObjArrayKlass::cast(a->klass())->element_klass();
287 if (!obj->is_a(element_klass)) {
288 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
289 }
290 }
291 objArrayOop(a)->obj_at_put(index, obj);
292 }
293 } else {
294 assert(a->is_typeArray(), "just checking");
295 BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
296 if (array_type != value_type) {
297 // The widen operation can potentially throw an exception, but cannot block,
298 // so typeArrayOop a is safe if the call succeeds.
299 widen(value, value_type, array_type, CHECK);
300 }
301 switch (array_type) {
302 case T_BOOLEAN:
303 typeArrayOop(a)->bool_at_put(index, value->z);
304 break;
756 }
757 if (!ss.at_return_type()) {
758 mirrors->obj_at_put(index++, mirror);
759 } else if (return_type != nullptr) {
760 // Collect return type as well
761 assert(ss.at_return_type(), "return type should be present");
762 *return_type = mirror;
763 }
764 }
765 assert(index == parameter_count, "invalid parameter count");
766 return mirrors;
767 }
768
769 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
770 return method->resolved_checked_exceptions(THREAD);
771 }
772
773 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
774 ResolvingSignatureStream ss(signature, k, false);
775 oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
776 return Handle(THREAD, nt);
777 }
778
779 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
780 // Allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
781 assert(!method()->name()->starts_with('<') || for_constant_pool_access,
782 "should call new_constructor instead");
783 InstanceKlass* holder = method->method_holder();
784 int slot = method->method_idnum();
785
786 Symbol* signature = method->signature();
787 int parameter_count = ArgumentCount(signature).size();
788 oop return_type_oop = nullptr;
789 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
790 if (parameter_types.is_null() || return_type_oop == nullptr) return nullptr;
791
792 Handle return_type(THREAD, return_type_oop);
793
794 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
795 assert(!exception_types.is_null(), "cannot return null");
796
797 Symbol* method_name = method->name();
798 oop name_oop = StringTable::intern(method_name, CHECK_NULL);
799 Handle name = Handle(THREAD, name_oop);
800 if (name == nullptr) return nullptr;
801
810 java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
811 java_lang_reflect_Method::set_exception_types(mh(), exception_types());
812 java_lang_reflect_Method::set_modifiers(mh(), modifiers);
813 java_lang_reflect_Method::set_override(mh(), false);
814 if (method->generic_signature() != nullptr) {
815 Symbol* gs = method->generic_signature();
816 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
817 java_lang_reflect_Method::set_signature(mh(), sig());
818 }
819 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
820 java_lang_reflect_Method::set_annotations(mh(), an_oop);
821 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
822 java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
823 an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
824 java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
825 return mh();
826 }
827
828
829 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
830 assert(method()->is_object_constructor(),
831 "should call new_method instead");
832
833 InstanceKlass* holder = method->method_holder();
834 int slot = method->method_idnum();
835
836 Symbol* signature = method->signature();
837 int parameter_count = ArgumentCount(signature).size();
838 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, nullptr, CHECK_NULL);
839 if (parameter_types.is_null()) return nullptr;
840
841 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
842 assert(!exception_types.is_null(), "cannot return null");
843
844 const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
845
846 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
847
848 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
849 java_lang_reflect_Constructor::set_slot(ch(), slot);
850 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
851 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
860 java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
861 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
862 java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
863 return ch();
864 }
865
866
867 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
868 Symbol* field_name = fd->name();
869 oop name_oop = StringTable::intern(field_name, CHECK_NULL);
870 Handle name = Handle(THREAD, name_oop);
871 Symbol* signature = fd->signature();
872 InstanceKlass* holder = fd->field_holder();
873 Handle type = new_type(signature, holder, CHECK_NULL);
874 Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
875
876 java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
877 java_lang_reflect_Field::set_slot(rh(), fd->index());
878 java_lang_reflect_Field::set_name(rh(), name());
879 java_lang_reflect_Field::set_type(rh(), type());
880
881 int flags = 0;
882 if (fd->is_trusted_final()) {
883 flags |= TRUSTED_FINAL;
884 }
885 if (fd->is_null_free_inline_type()) {
886 flags |= NULL_RESTRICTED;
887 }
888 java_lang_reflect_Field::set_flags(rh(), flags);
889
890 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
891 int modifiers = fd->access_flags().as_int();
892 java_lang_reflect_Field::set_modifiers(rh(), modifiers);
893 java_lang_reflect_Field::set_override(rh(), false);
894 if (fd->has_generic_signature()) {
895 Symbol* gs = fd->generic_signature();
896 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
897 java_lang_reflect_Field::set_signature(rh(), sig());
898 }
899 typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
900 java_lang_reflect_Field::set_annotations(rh(), an_oop);
901 return rh();
902 }
903
904 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
905 int flags, TRAPS) {
906
907 Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
908
909 if(nullptr != sym) {
910 Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
911 java_lang_reflect_Parameter::set_name(rh(), name());
912 } else {
988 // Ensure klass is initialized
989 klass->initialize(CHECK_NULL);
990
991 bool is_static = reflected_method->is_static();
992 if (is_static) {
993 // ignore receiver argument
994 method = reflected_method;
995 target_klass = klass;
996 } else {
997 // check for null receiver
998 if (receiver.is_null()) {
999 THROW_0(vmSymbols::java_lang_NullPointerException());
1000 }
1001 // Check class of receiver against class declaring method
1002 if (!receiver->is_a(klass)) {
1003 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
1004 }
1005 // target klass is receiver's klass
1006 target_klass = receiver->klass();
1007 // no need to resolve if method is private or <init>
1008 if (reflected_method->is_private() ||
1009 reflected_method->name() == vmSymbols::object_initializer_name()) {
1010 method = reflected_method;
1011 } else {
1012 // resolve based on the receiver
1013 if (reflected_method->method_holder()->is_interface()) {
1014 // resolve interface call
1015 //
1016 // Match resolution errors with those thrown due to reflection inlining
1017 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
1018 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
1019 if (HAS_PENDING_EXCEPTION) {
1020 // Method resolution threw an exception; wrap it in an InvocationTargetException
1021 oop resolution_exception = PENDING_EXCEPTION;
1022 CLEAR_PENDING_EXCEPTION;
1023 // JVMTI has already reported the pending exception
1024 // JVMTI internal flag reset is needed in order to report InvocationTargetException
1025 JvmtiExport::clear_detected_exception(THREAD);
1026 JavaCallArguments args(Handle(THREAD, resolution_exception));
1027 THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1028 vmSymbols::throwable_void_signature(),
1029 &args);
1171 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1172 }
1173 methodHandle method(THREAD, m);
1174
1175 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1176 }
1177
1178
1179 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1180 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror);
1181 int slot = java_lang_reflect_Constructor::slot(constructor_mirror);
1182 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1183 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1184
1185 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1186 Method* m = klass->method_with_idnum(slot);
1187 if (m == nullptr) {
1188 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1189 }
1190 methodHandle method(THREAD, m);
1191
1192 // Make sure klass gets initialize
1193 klass->initialize(CHECK_NULL);
1194
1195 // Create new instance (the receiver)
1196 klass->check_valid_for_instantiation(false, CHECK_NULL);
1197 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1198
1199 // Ignore result from call and return receiver
1200 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1201 return receiver();
1202 }
|