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