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
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;
730 }
731 if (!ss.at_return_type()) {
732 mirrors->obj_at_put(index++, mirror);
733 } else if (return_type != nullptr) {
734 // Collect return type as well
735 assert(ss.at_return_type(), "return type should be present");
736 *return_type = mirror;
737 }
738 }
739 assert(index == parameter_count, "invalid parameter count");
740 return mirrors;
741 }
742
743 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
744 return method->resolved_checked_exceptions(THREAD);
745 }
746
747 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
748 ResolvingSignatureStream ss(signature, k, false);
749 oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
750 if (log_is_enabled(Debug, class, resolve)) {
751 trace_class_resolution(nt);
752 }
753 return Handle(THREAD, nt);
754 }
755
756 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
757 // Allow jdk.internal.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
758 assert(!method()->is_object_initializer() &&
759 (for_constant_pool_access || !method()->is_static_initializer()),
760 "Should not be the initializer");
761 InstanceKlass* holder = method->method_holder();
762 int slot = method->method_idnum();
763
764 Symbol* signature = method->signature();
765 int parameter_count = ArgumentCount(signature).size();
766 oop return_type_oop = nullptr;
767 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
768 if (parameter_types.is_null() || return_type_oop == nullptr) return nullptr;
769
770 Handle return_type(THREAD, return_type_oop);
771
772 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
773 assert(!exception_types.is_null(), "cannot return null");
774
775 Symbol* method_name = method->name();
776 oop name_oop = StringTable::intern(method_name, CHECK_NULL);
777 Handle name = Handle(THREAD, name_oop);
778 if (name == nullptr) return nullptr;
779
780 const int modifiers = method->access_flags().as_method_flags();
788 java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
789 java_lang_reflect_Method::set_exception_types(mh(), exception_types());
790 java_lang_reflect_Method::set_modifiers(mh(), modifiers);
791 java_lang_reflect_Method::set_override(mh(), false);
792 if (method->generic_signature() != nullptr) {
793 Symbol* gs = method->generic_signature();
794 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
795 java_lang_reflect_Method::set_signature(mh(), sig());
796 }
797 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
798 java_lang_reflect_Method::set_annotations(mh(), an_oop);
799 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
800 java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
801 an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
802 java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
803 return mh();
804 }
805
806
807 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
808 assert(method()->is_object_initializer(), "Should be the initializer");
809
810 InstanceKlass* holder = method->method_holder();
811 int slot = method->method_idnum();
812
813 Symbol* signature = method->signature();
814 int parameter_count = ArgumentCount(signature).size();
815 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, nullptr, CHECK_NULL);
816 if (parameter_types.is_null()) return nullptr;
817
818 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
819 assert(!exception_types.is_null(), "cannot return null");
820
821 const int modifiers = method->access_flags().as_method_flags();
822
823 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
824
825 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
826 java_lang_reflect_Constructor::set_slot(ch(), slot);
827 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
828 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
837 java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
838 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
839 java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
840 return ch();
841 }
842
843
844 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
845 Symbol* field_name = fd->name();
846 oop name_oop = StringTable::intern(field_name, CHECK_NULL);
847 Handle name = Handle(THREAD, name_oop);
848 Symbol* signature = fd->signature();
849 InstanceKlass* holder = fd->field_holder();
850 Handle type = new_type(signature, holder, CHECK_NULL);
851 Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
852
853 java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
854 java_lang_reflect_Field::set_slot(rh(), fd->index());
855 java_lang_reflect_Field::set_name(rh(), name());
856 java_lang_reflect_Field::set_type(rh(), type());
857 if (fd->is_trusted_final()) {
858 java_lang_reflect_Field::set_trusted_final(rh());
859 }
860 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
861 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_field_flags());
862 java_lang_reflect_Field::set_override(rh(), false);
863 if (fd->has_generic_signature()) {
864 Symbol* gs = fd->generic_signature();
865 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
866 java_lang_reflect_Field::set_signature(rh(), sig());
867 }
868 typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
869 java_lang_reflect_Field::set_annotations(rh(), an_oop);
870 return rh();
871 }
872
873 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
874 int flags, TRAPS) {
875
876 Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
877
878 if(nullptr != sym) {
879 Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
957 // Ensure klass is initialized
958 klass->initialize(CHECK_NULL);
959
960 bool is_static = reflected_method->is_static();
961 if (is_static) {
962 // ignore receiver argument
963 method = reflected_method;
964 target_klass = klass;
965 } else {
966 // check for null receiver
967 if (receiver.is_null()) {
968 THROW_NULL(vmSymbols::java_lang_NullPointerException());
969 }
970 // Check class of receiver against class declaring method
971 if (!receiver->is_a(klass)) {
972 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
973 }
974 // target klass is receiver's klass
975 target_klass = receiver->klass();
976 // no need to resolve if method is private or <init>
977 if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) {
978 method = reflected_method;
979 } else {
980 // resolve based on the receiver
981 if (reflected_method->method_holder()->is_interface()) {
982 // resolve interface call
983 //
984 // Match resolution errors with those thrown due to reflection inlining
985 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
986 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
987 if (HAS_PENDING_EXCEPTION) {
988 // Method resolution threw an exception; wrap it in an InvocationTargetException
989 oop resolution_exception = PENDING_EXCEPTION;
990 CLEAR_PENDING_EXCEPTION;
991 // JVMTI has already reported the pending exception
992 // JVMTI internal flag reset is needed in order to report InvocationTargetException
993 JvmtiExport::clear_detected_exception(THREAD);
994 JavaCallArguments args(Handle(THREAD, resolution_exception));
995 THROW_ARG_NULL(vmSymbols::java_lang_reflect_InvocationTargetException(),
996 vmSymbols::throwable_void_signature(),
997 &args);
1139 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1140 }
1141 methodHandle method(THREAD, m);
1142
1143 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1144 }
1145
1146
1147 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1148 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror);
1149 int slot = java_lang_reflect_Constructor::slot(constructor_mirror);
1150 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1151 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1152
1153 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1154 Method* m = klass->method_with_idnum(slot);
1155 if (m == nullptr) {
1156 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1157 }
1158 methodHandle method(THREAD, m);
1159 assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1160
1161 // Make sure klass gets initialize
1162 klass->initialize(CHECK_NULL);
1163
1164 // Create new instance (the receiver)
1165 klass->check_valid_for_instantiation(false, CHECK_NULL);
1166 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1167
1168 // Ignore result from call and return receiver
1169 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1170 return receiver();
1171 }
|
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
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
276 if (a->is_objArray()) {
277 if (value_type == T_OBJECT) {
278 oop obj = cast_to_oop(value->l);
279 if (a->is_null_free_array() && obj == nullptr) {
280 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "null-restricted array");
281 }
282
283 if (obj != nullptr) {
284 Klass* element_klass = ObjArrayKlass::cast(a->klass())->element_klass();
285 if (!obj->is_a(element_klass)) {
286 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
287 }
288 }
289 objArrayOop(a)->obj_at_put(index, obj);
290 }
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;
737 }
738 if (!ss.at_return_type()) {
739 mirrors->obj_at_put(index++, mirror);
740 } else if (return_type != nullptr) {
741 // Collect return type as well
742 assert(ss.at_return_type(), "return type should be present");
743 *return_type = mirror;
744 }
745 }
746 assert(index == parameter_count, "invalid parameter count");
747 return mirrors;
748 }
749
750 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
751 return method->resolved_checked_exceptions(THREAD);
752 }
753
754 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
755 ResolvingSignatureStream ss(signature, k, false);
756 oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
757 return Handle(THREAD, nt);
758 }
759
760 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
761 // Allow jdk.internal.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
762 assert(!method()->name()->starts_with('<') || for_constant_pool_access,
763 "should call new_constructor instead");
764 InstanceKlass* holder = method->method_holder();
765 int slot = method->method_idnum();
766
767 Symbol* signature = method->signature();
768 int parameter_count = ArgumentCount(signature).size();
769 oop return_type_oop = nullptr;
770 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
771 if (parameter_types.is_null() || return_type_oop == nullptr) return nullptr;
772
773 Handle return_type(THREAD, return_type_oop);
774
775 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
776 assert(!exception_types.is_null(), "cannot return null");
777
778 Symbol* method_name = method->name();
779 oop name_oop = StringTable::intern(method_name, CHECK_NULL);
780 Handle name = Handle(THREAD, name_oop);
781 if (name == nullptr) return nullptr;
782
783 const int modifiers = method->access_flags().as_method_flags();
791 java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
792 java_lang_reflect_Method::set_exception_types(mh(), exception_types());
793 java_lang_reflect_Method::set_modifiers(mh(), modifiers);
794 java_lang_reflect_Method::set_override(mh(), false);
795 if (method->generic_signature() != nullptr) {
796 Symbol* gs = method->generic_signature();
797 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
798 java_lang_reflect_Method::set_signature(mh(), sig());
799 }
800 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
801 java_lang_reflect_Method::set_annotations(mh(), an_oop);
802 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
803 java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
804 an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
805 java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
806 return mh();
807 }
808
809
810 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
811 assert(method()->is_object_constructor(),
812 "should call new_method instead");
813
814 InstanceKlass* holder = method->method_holder();
815 int slot = method->method_idnum();
816
817 Symbol* signature = method->signature();
818 int parameter_count = ArgumentCount(signature).size();
819 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, nullptr, CHECK_NULL);
820 if (parameter_types.is_null()) return nullptr;
821
822 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
823 assert(!exception_types.is_null(), "cannot return null");
824
825 const int modifiers = method->access_flags().as_method_flags();
826
827 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
828
829 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
830 java_lang_reflect_Constructor::set_slot(ch(), slot);
831 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
832 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
841 java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
842 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
843 java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
844 return ch();
845 }
846
847
848 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
849 Symbol* field_name = fd->name();
850 oop name_oop = StringTable::intern(field_name, CHECK_NULL);
851 Handle name = Handle(THREAD, name_oop);
852 Symbol* signature = fd->signature();
853 InstanceKlass* holder = fd->field_holder();
854 Handle type = new_type(signature, holder, CHECK_NULL);
855 Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
856
857 java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
858 java_lang_reflect_Field::set_slot(rh(), fd->index());
859 java_lang_reflect_Field::set_name(rh(), name());
860 java_lang_reflect_Field::set_type(rh(), type());
861
862 int flags = 0;
863 if (fd->is_trusted_final()) {
864 flags |= TRUSTED_FINAL;
865 }
866 if (fd->is_null_free_inline_type()) {
867 flags |= NULL_RESTRICTED;
868 }
869 java_lang_reflect_Field::set_flags(rh(), flags);
870
871 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
872 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_field_flags());
873 java_lang_reflect_Field::set_override(rh(), false);
874 if (fd->has_generic_signature()) {
875 Symbol* gs = fd->generic_signature();
876 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
877 java_lang_reflect_Field::set_signature(rh(), sig());
878 }
879 typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
880 java_lang_reflect_Field::set_annotations(rh(), an_oop);
881 return rh();
882 }
883
884 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
885 int flags, TRAPS) {
886
887 Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
888
889 if(nullptr != sym) {
890 Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
968 // Ensure klass is initialized
969 klass->initialize(CHECK_NULL);
970
971 bool is_static = reflected_method->is_static();
972 if (is_static) {
973 // ignore receiver argument
974 method = reflected_method;
975 target_klass = klass;
976 } else {
977 // check for null receiver
978 if (receiver.is_null()) {
979 THROW_NULL(vmSymbols::java_lang_NullPointerException());
980 }
981 // Check class of receiver against class declaring method
982 if (!receiver->is_a(klass)) {
983 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
984 }
985 // target klass is receiver's klass
986 target_klass = receiver->klass();
987 // no need to resolve if method is private or <init>
988 if (reflected_method->is_private() ||
989 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
1172 // Make sure klass gets initialize
1173 klass->initialize(CHECK_NULL);
1174
1175 // Create new instance (the receiver)
1176 klass->check_valid_for_instantiation(false, CHECK_NULL);
1177 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1178
1179 // Ignore result from call and return receiver
1180 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1181 return receiver();
1182 }
|