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 (value_type == T_OBJECT) {
274 assert(a->is_objArray(), "just checking");
275 oop obj = cast_to_oop(value->l);
276 if (obj != nullptr) {
277 Klass* element_klass = ObjArrayKlass::cast(a->klass())->element_klass();
278 if (!obj->is_a(element_klass)) {
279 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
280 }
281 }
282 objArrayOop(a)->obj_at_put(index, obj);
283 } else {
284 assert(a->is_typeArray(), "just checking");
285 BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
286 if (array_type != value_type) {
287 // The widen operation can potentially throw an exception, but cannot block,
288 // so typeArrayOop a is safe if the call succeeds.
289 widen(value, value_type, array_type, CHECK);
290 }
291 switch (array_type) {
292 case T_BOOLEAN:
293 typeArrayOop(a)->bool_at_put(index, value->z);
294 break;
295 case T_CHAR:
739 }
740 if (!ss.at_return_type()) {
741 mirrors->obj_at_put(index++, mirror);
742 } else if (return_type != nullptr) {
743 // Collect return type as well
744 assert(ss.at_return_type(), "return type should be present");
745 *return_type = mirror;
746 }
747 }
748 assert(index == parameter_count, "invalid parameter count");
749 return mirrors;
750 }
751
752 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
753 return method->resolved_checked_exceptions(THREAD);
754 }
755
756 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
757 ResolvingSignatureStream ss(signature, k, false);
758 oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
759 if (log_is_enabled(Debug, class, resolve)) {
760 trace_class_resolution(nt);
761 }
762 return Handle(THREAD, nt);
763 }
764
765 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
766 // Allow jdk.internal.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
767 assert(!method()->is_object_initializer() &&
768 (for_constant_pool_access || !method()->is_static_initializer()),
769 "Should not be the initializer");
770 InstanceKlass* holder = method->method_holder();
771 int slot = method->method_idnum();
772
773 Symbol* signature = method->signature();
774 int parameter_count = ArgumentCount(signature).size();
775 oop return_type_oop = nullptr;
776 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
777 if (parameter_types.is_null() || return_type_oop == nullptr) return nullptr;
778
779 Handle return_type(THREAD, return_type_oop);
780
781 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
782 assert(!exception_types.is_null(), "cannot return null");
783
784 Symbol* method_name = method->name();
785 oop name_oop = StringTable::intern(method_name, CHECK_NULL);
786 Handle name = Handle(THREAD, name_oop);
787 if (name == nullptr) return nullptr;
788
789 const int modifiers = method->access_flags().as_method_flags();
797 java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
798 java_lang_reflect_Method::set_exception_types(mh(), exception_types());
799 java_lang_reflect_Method::set_modifiers(mh(), modifiers);
800 java_lang_reflect_Method::set_override(mh(), false);
801 if (method->generic_signature() != nullptr) {
802 Symbol* gs = method->generic_signature();
803 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
804 java_lang_reflect_Method::set_signature(mh(), sig());
805 }
806 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
807 java_lang_reflect_Method::set_annotations(mh(), an_oop);
808 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
809 java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
810 an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
811 java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
812 return mh();
813 }
814
815
816 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
817 assert(method()->is_object_initializer(), "Should be the initializer");
818
819 InstanceKlass* holder = method->method_holder();
820 int slot = method->method_idnum();
821
822 Symbol* signature = method->signature();
823 int parameter_count = ArgumentCount(signature).size();
824 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, nullptr, CHECK_NULL);
825 if (parameter_types.is_null()) return nullptr;
826
827 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
828 assert(!exception_types.is_null(), "cannot return null");
829
830 const int modifiers = method->access_flags().as_method_flags();
831
832 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
833
834 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
835 java_lang_reflect_Constructor::set_slot(ch(), slot);
836 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
837 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
846 java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
847 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
848 java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
849 return ch();
850 }
851
852
853 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
854 Symbol* field_name = fd->name();
855 oop name_oop = StringTable::intern(field_name, CHECK_NULL);
856 Handle name = Handle(THREAD, name_oop);
857 Symbol* signature = fd->signature();
858 InstanceKlass* holder = fd->field_holder();
859 Handle type = new_type(signature, holder, CHECK_NULL);
860 Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
861
862 java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
863 java_lang_reflect_Field::set_slot(rh(), fd->index());
864 java_lang_reflect_Field::set_name(rh(), name());
865 java_lang_reflect_Field::set_type(rh(), type());
866 if (fd->is_trusted_final()) {
867 java_lang_reflect_Field::set_trusted_final(rh());
868 }
869 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
870 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_field_flags());
871 java_lang_reflect_Field::set_override(rh(), false);
872 if (fd->has_generic_signature()) {
873 Symbol* gs = fd->generic_signature();
874 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
875 java_lang_reflect_Field::set_signature(rh(), sig());
876 }
877 typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
878 java_lang_reflect_Field::set_annotations(rh(), an_oop);
879 return rh();
880 }
881
882 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
883 int flags, TRAPS) {
884
885 Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
886
887 if(nullptr != sym) {
888 Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
959 // Ensure klass is initialized
960 klass->initialize(CHECK_NULL);
961
962 bool is_static = reflected_method->is_static();
963 if (is_static) {
964 // ignore receiver argument
965 method = reflected_method;
966 target_klass = klass;
967 } else {
968 // check for null receiver
969 if (receiver.is_null()) {
970 THROW_NULL(vmSymbols::java_lang_NullPointerException());
971 }
972 // Check class of receiver against class declaring method
973 if (!receiver->is_a(klass)) {
974 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
975 }
976 // target klass is receiver's klass
977 target_klass = receiver->klass();
978 // no need to resolve if method is private or <init>
979 if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) {
980 method = reflected_method;
981 } else {
982 // resolve based on the receiver
983 if (reflected_method->method_holder()->is_interface()) {
984 // resolve interface call
985 //
986 // Match resolution errors with those thrown due to reflection inlining
987 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
988 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
989 if (HAS_PENDING_EXCEPTION) {
990 // Method resolution threw an exception; wrap it in an InvocationTargetException
991 oop resolution_exception = PENDING_EXCEPTION;
992 CLEAR_PENDING_EXCEPTION;
993 // JVMTI has already reported the pending exception
994 // JVMTI internal flag reset is needed in order to report InvocationTargetException
995 JvmtiExport::clear_detected_exception(THREAD);
996 JavaCallArguments args(Handle(THREAD, resolution_exception));
997 THROW_ARG_NULL(vmSymbols::java_lang_reflect_InvocationTargetException(),
998 vmSymbols::throwable_void_signature(),
999 &args);
1039 ss.print("'");
1040 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
1041 }
1042
1043 assert(ptypes->is_objArray(), "just checking");
1044 int args_len = args.is_null() ? 0 : args->length();
1045 // Check number of arguments
1046 if (ptypes->length() != args_len) {
1047 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
1048 "wrong number of arguments");
1049 }
1050
1051 // Create object to contain parameters for the JavaCall
1052 JavaCallArguments java_args(method->size_of_parameters());
1053
1054 if (!is_static) {
1055 java_args.push_oop(receiver);
1056 }
1057
1058 for (int i = 0; i < args_len; i++) {
1059 oop type_mirror = ptypes->obj_at(i);
1060 oop arg = args->obj_at(i);
1061 if (java_lang_Class::is_primitive(type_mirror)) {
1062 jvalue value;
1063 BasicType ptype = basic_type_mirror_to_basic_type(type_mirror);
1064 BasicType atype = Reflection::unbox_for_primitive(arg, &value, CHECK_NULL);
1065 if (ptype != atype) {
1066 Reflection::widen(&value, atype, ptype, CHECK_NULL);
1067 }
1068 switch (ptype) {
1069 case T_BOOLEAN: java_args.push_int(value.z); break;
1070 case T_CHAR: java_args.push_int(value.c); break;
1071 case T_BYTE: java_args.push_int(value.b); break;
1072 case T_SHORT: java_args.push_int(value.s); break;
1073 case T_INT: java_args.push_int(value.i); break;
1074 case T_LONG: java_args.push_long(value.j); break;
1075 case T_FLOAT: java_args.push_float(value.f); break;
1076 case T_DOUBLE: java_args.push_double(value.d); break;
1077 default:
1078 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
1079 }
1080 } else {
1141 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1142 }
1143 methodHandle method(THREAD, m);
1144
1145 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1146 }
1147
1148
1149 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1150 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror);
1151 int slot = java_lang_reflect_Constructor::slot(constructor_mirror);
1152 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1153 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1154
1155 InstanceKlass* klass = java_lang_Class::as_InstanceKlass(mirror);
1156 Method* m = klass->method_with_idnum(slot);
1157 if (m == nullptr) {
1158 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1159 }
1160 methodHandle method(THREAD, m);
1161 assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1162
1163 // Make sure klass gets initialize
1164 klass->initialize(CHECK_NULL);
1165
1166 // Create new instance (the receiver)
1167 klass->check_valid_for_instantiation(false, CHECK_NULL);
1168 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1169
1170 // Ignore result from call and return receiver
1171 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1172 return receiver();
1173 }
|
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 (value_type == T_OBJECT) {
278 assert(a->is_objArray(), "just checking");
279 oop obj = cast_to_oop(value->l);
280 if (a->is_null_free_array() && obj == nullptr) {
281 THROW_MSG(vmSymbols::java_lang_NullPointerException(), "null-restricted array");
282 }
283
284 if (obj != nullptr) {
285 Klass* element_klass = ObjArrayKlass::cast(a->klass())->element_klass();
286 if (!obj->is_a(element_klass)) {
287 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
288 }
289 }
290 objArrayOop(a)->obj_at_put(index, obj);
291 } else {
292 assert(a->is_typeArray(), "just checking");
293 BasicType array_type = TypeArrayKlass::cast(a->klass())->element_type();
294 if (array_type != value_type) {
295 // The widen operation can potentially throw an exception, but cannot block,
296 // so typeArrayOop a is safe if the call succeeds.
297 widen(value, value_type, array_type, CHECK);
298 }
299 switch (array_type) {
300 case T_BOOLEAN:
301 typeArrayOop(a)->bool_at_put(index, value->z);
302 break;
303 case T_CHAR:
747 }
748 if (!ss.at_return_type()) {
749 mirrors->obj_at_put(index++, mirror);
750 } else if (return_type != nullptr) {
751 // Collect return type as well
752 assert(ss.at_return_type(), "return type should be present");
753 *return_type = mirror;
754 }
755 }
756 assert(index == parameter_count, "invalid parameter count");
757 return mirrors;
758 }
759
760 static objArrayHandle get_exception_types(const methodHandle& method, TRAPS) {
761 return method->resolved_checked_exceptions(THREAD);
762 }
763
764 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
765 ResolvingSignatureStream ss(signature, k, false);
766 oop nt = ss.as_java_mirror(SignatureStream::NCDFError, CHECK_NH);
767 return Handle(THREAD, nt);
768 }
769
770 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
771 // Allow jdk.internal.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
772 assert(!method()->name()->starts_with('<') || for_constant_pool_access,
773 "should call new_constructor instead");
774 InstanceKlass* holder = method->method_holder();
775 int slot = method->method_idnum();
776
777 Symbol* signature = method->signature();
778 int parameter_count = ArgumentCount(signature).size();
779 oop return_type_oop = nullptr;
780 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
781 if (parameter_types.is_null() || return_type_oop == nullptr) return nullptr;
782
783 Handle return_type(THREAD, return_type_oop);
784
785 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
786 assert(!exception_types.is_null(), "cannot return null");
787
788 Symbol* method_name = method->name();
789 oop name_oop = StringTable::intern(method_name, CHECK_NULL);
790 Handle name = Handle(THREAD, name_oop);
791 if (name == nullptr) return nullptr;
792
793 const int modifiers = method->access_flags().as_method_flags();
801 java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
802 java_lang_reflect_Method::set_exception_types(mh(), exception_types());
803 java_lang_reflect_Method::set_modifiers(mh(), modifiers);
804 java_lang_reflect_Method::set_override(mh(), false);
805 if (method->generic_signature() != nullptr) {
806 Symbol* gs = method->generic_signature();
807 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
808 java_lang_reflect_Method::set_signature(mh(), sig());
809 }
810 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
811 java_lang_reflect_Method::set_annotations(mh(), an_oop);
812 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
813 java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
814 an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
815 java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
816 return mh();
817 }
818
819
820 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
821 assert(method()->is_object_constructor(),
822 "should call new_method instead");
823
824 InstanceKlass* holder = method->method_holder();
825 int slot = method->method_idnum();
826
827 Symbol* signature = method->signature();
828 int parameter_count = ArgumentCount(signature).size();
829 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, nullptr, CHECK_NULL);
830 if (parameter_types.is_null()) return nullptr;
831
832 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
833 assert(!exception_types.is_null(), "cannot return null");
834
835 const int modifiers = method->access_flags().as_method_flags();
836
837 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
838
839 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
840 java_lang_reflect_Constructor::set_slot(ch(), slot);
841 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
842 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
851 java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
852 an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
853 java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
854 return ch();
855 }
856
857
858 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
859 Symbol* field_name = fd->name();
860 oop name_oop = StringTable::intern(field_name, CHECK_NULL);
861 Handle name = Handle(THREAD, name_oop);
862 Symbol* signature = fd->signature();
863 InstanceKlass* holder = fd->field_holder();
864 Handle type = new_type(signature, holder, CHECK_NULL);
865 Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
866
867 java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
868 java_lang_reflect_Field::set_slot(rh(), fd->index());
869 java_lang_reflect_Field::set_name(rh(), name());
870 java_lang_reflect_Field::set_type(rh(), type());
871
872 int flags = 0;
873 if (fd->is_trusted_final()) {
874 flags |= TRUSTED_FINAL;
875 }
876 if (fd->is_null_free_inline_type()) {
877 flags |= NULL_RESTRICTED;
878 }
879 java_lang_reflect_Field::set_flags(rh(), flags);
880
881 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
882 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_field_flags());
883 java_lang_reflect_Field::set_override(rh(), false);
884 if (fd->has_generic_signature()) {
885 Symbol* gs = fd->generic_signature();
886 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
887 java_lang_reflect_Field::set_signature(rh(), sig());
888 }
889 typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
890 java_lang_reflect_Field::set_annotations(rh(), an_oop);
891 return rh();
892 }
893
894 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
895 int flags, TRAPS) {
896
897 Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
898
899 if(nullptr != sym) {
900 Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
971 // Ensure klass is initialized
972 klass->initialize(CHECK_NULL);
973
974 bool is_static = reflected_method->is_static();
975 if (is_static) {
976 // ignore receiver argument
977 method = reflected_method;
978 target_klass = klass;
979 } else {
980 // check for null receiver
981 if (receiver.is_null()) {
982 THROW_NULL(vmSymbols::java_lang_NullPointerException());
983 }
984 // Check class of receiver against class declaring method
985 if (!receiver->is_a(klass)) {
986 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
987 }
988 // target klass is receiver's klass
989 target_klass = receiver->klass();
990 // no need to resolve if method is private or <init>
991 if (reflected_method->is_private() ||
992 reflected_method->name() == vmSymbols::object_initializer_name()) {
993 method = reflected_method;
994 } else {
995 // resolve based on the receiver
996 if (reflected_method->method_holder()->is_interface()) {
997 // resolve interface call
998 //
999 // Match resolution errors with those thrown due to reflection inlining
1000 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
1001 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
1002 if (HAS_PENDING_EXCEPTION) {
1003 // Method resolution threw an exception; wrap it in an InvocationTargetException
1004 oop resolution_exception = PENDING_EXCEPTION;
1005 CLEAR_PENDING_EXCEPTION;
1006 // JVMTI has already reported the pending exception
1007 // JVMTI internal flag reset is needed in order to report InvocationTargetException
1008 JvmtiExport::clear_detected_exception(THREAD);
1009 JavaCallArguments args(Handle(THREAD, resolution_exception));
1010 THROW_ARG_NULL(vmSymbols::java_lang_reflect_InvocationTargetException(),
1011 vmSymbols::throwable_void_signature(),
1012 &args);
1052 ss.print("'");
1053 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
1054 }
1055
1056 assert(ptypes->is_objArray(), "just checking");
1057 int args_len = args.is_null() ? 0 : args->length();
1058 // Check number of arguments
1059 if (ptypes->length() != args_len) {
1060 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(),
1061 "wrong number of arguments");
1062 }
1063
1064 // Create object to contain parameters for the JavaCall
1065 JavaCallArguments java_args(method->size_of_parameters());
1066
1067 if (!is_static) {
1068 java_args.push_oop(receiver);
1069 }
1070
1071 for (int i = 0; i < args_len; i++) {
1072 oop type_mirror = ptypes->obj_at(i, CHECK_NULL);
1073 oop arg = args->obj_at(i, CHECK_NULL);
1074 if (java_lang_Class::is_primitive(type_mirror)) {
1075 jvalue value;
1076 BasicType ptype = basic_type_mirror_to_basic_type(type_mirror);
1077 BasicType atype = Reflection::unbox_for_primitive(arg, &value, CHECK_NULL);
1078 if (ptype != atype) {
1079 Reflection::widen(&value, atype, ptype, CHECK_NULL);
1080 }
1081 switch (ptype) {
1082 case T_BOOLEAN: java_args.push_int(value.z); break;
1083 case T_CHAR: java_args.push_int(value.c); break;
1084 case T_BYTE: java_args.push_int(value.b); break;
1085 case T_SHORT: java_args.push_int(value.s); break;
1086 case T_INT: java_args.push_int(value.i); break;
1087 case T_LONG: java_args.push_long(value.j); break;
1088 case T_FLOAT: java_args.push_float(value.f); break;
1089 case T_DOUBLE: java_args.push_double(value.d); break;
1090 default:
1091 THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
1092 }
1093 } else {
1154 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1155 }
1156 methodHandle method(THREAD, m);
1157
1158 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1159 }
1160
1161
1162 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1163 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror);
1164 int slot = java_lang_reflect_Constructor::slot(constructor_mirror);
1165 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1166 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1167
1168 InstanceKlass* klass = java_lang_Class::as_InstanceKlass(mirror);
1169 Method* m = klass->method_with_idnum(slot);
1170 if (m == nullptr) {
1171 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "invoke");
1172 }
1173 methodHandle method(THREAD, m);
1174
1175 // Make sure klass gets initialize
1176 klass->initialize(CHECK_NULL);
1177
1178 // Create new instance (the receiver)
1179 klass->check_valid_for_instantiation(false, CHECK_NULL);
1180 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1181
1182 // Ignore result from call and return receiver
1183 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1184 return receiver();
1185 }
|