691
692 // Allow all accesses from jdk/internal/reflect/MagicAccessorImpl subclasses to
693 // succeed trivially.
694 if (current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
695 return true;
696 }
697
698 return can_relax_access_check_for(
699 current_class, field_class, classloader_only);
700 }
701
702 bool Reflection::is_same_class_package(const Klass* class1, const Klass* class2) {
703 return InstanceKlass::cast(class1)->is_same_class_package(class2);
704 }
705
706 // Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not,
707 // throw an incompatible class change exception
708 // If inner_is_member, require the inner to be a member of the outer.
709 // If !inner_is_member, require the inner to be anonymous (a non-member).
710 // Caller is responsible for figuring out in advance which case must be true.
711 void Reflection::check_for_inner_class(instanceKlassHandle outer, instanceKlassHandle inner,
712 bool inner_is_member, TRAPS) {
713 InnerClassesIterator iter(outer);
714 constantPoolHandle cp (THREAD, outer->constants());
715 for (; !iter.done(); iter.next()) {
716 int ioff = iter.inner_class_info_index();
717 int ooff = iter.outer_class_info_index();
718
719 if (inner_is_member && ioff != 0 && ooff != 0) {
720 Klass* o = cp->klass_at(ooff, CHECK);
721 if (o == outer()) {
722 Klass* i = cp->klass_at(ioff, CHECK);
723 if (i == inner()) {
724 return;
725 }
726 }
727 }
728 if (!inner_is_member && ioff != 0 && ooff == 0 &&
729 cp->klass_name_at_matches(inner, ioff)) {
730 Klass* i = cp->klass_at(ioff, CHECK);
731 if (i == inner()) {
732 return;
733 }
734 }
735 }
736
737 // 'inner' not declared as an inner klass in outer
738 ResourceMark rm(THREAD);
739 Exceptions::fthrow(
740 THREAD_AND_LOCATION,
741 vmSymbols::java_lang_IncompatibleClassChangeError(),
742 "%s and %s disagree on InnerClasses attribute",
743 outer->external_name(),
744 inner->external_name()
745 );
746 }
747
748 // Utility method converting a single SignatureStream element into java.lang.Class instance
749 static oop get_mirror_from_signature(methodHandle method,
750 SignatureStream* ss,
751 TRAPS) {
785 Symbol* signature = method->signature();
786 SignatureStream ss(signature);
787 while (!ss.at_return_type()) {
788 oop mirror = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle()));
789 mirrors->obj_at_put(index++, mirror);
790 ss.next();
791 }
792 assert(index == parameter_count, "invalid parameter count");
793 if (return_type != NULL) {
794 // Collect return type as well
795 assert(ss.at_return_type(), "return type should be present");
796 *return_type = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle()));
797 }
798 return mirrors;
799 }
800
801 static objArrayHandle get_exception_types(methodHandle method, TRAPS) {
802 return method->resolved_checked_exceptions(THREAD);
803 }
804
805 static Handle new_type(Symbol* signature, KlassHandle k, TRAPS) {
806 // Basic types
807 BasicType type = vmSymbols::signature_type(signature);
808 if (type != T_OBJECT) {
809 return Handle(THREAD, Universe::java_mirror(type));
810 }
811
812 Klass* result =
813 SystemDictionary::resolve_or_fail(signature,
814 Handle(THREAD, k->class_loader()),
815 Handle(THREAD, k->protection_domain()),
816 true, CHECK_(Handle()));
817
818 if (log_is_enabled(Debug, class, resolve)) {
819 trace_class_resolution(result);
820 }
821
822 oop nt = result->java_mirror();
823 return Handle(THREAD, nt);
824 }
825
826
827 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
828 // Allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
829 assert(!method()->is_initializer() ||
830 (for_constant_pool_access && method()->is_static()),
831 "should call new_constructor instead");
832 instanceKlassHandle holder (THREAD, method->method_holder());
833 int slot = method->method_idnum();
834
835 Symbol* signature = method->signature();
836 int parameter_count = ArgumentCount(signature).size();
837 oop return_type_oop = NULL;
838 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
839 if (parameter_types.is_null() || return_type_oop == NULL) return NULL;
840
841 Handle return_type(THREAD, return_type_oop);
842
843 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
844
845 if (exception_types.is_null()) return NULL;
846
847 Symbol* method_name = method->name();
848 oop name_oop = StringTable::intern(method_name, CHECK_NULL);
849 Handle name = Handle(THREAD, name_oop);
850 if (name == NULL) return NULL;
851
852 const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
873 }
874 if (java_lang_reflect_Method::has_parameter_annotations_field()) {
875 typeArrayOop an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
876 java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
877 }
878 if (java_lang_reflect_Method::has_annotation_default_field()) {
879 typeArrayOop an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
880 java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
881 }
882 if (java_lang_reflect_Method::has_type_annotations_field()) {
883 typeArrayOop an_oop = Annotations::make_java_array(method->type_annotations(), CHECK_NULL);
884 java_lang_reflect_Method::set_type_annotations(mh(), an_oop);
885 }
886 return mh();
887 }
888
889
890 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
891 assert(method()->is_initializer(), "should call new_method instead");
892
893 instanceKlassHandle holder (THREAD, method->method_holder());
894 int slot = method->method_idnum();
895
896 Symbol* signature = method->signature();
897 int parameter_count = ArgumentCount(signature).size();
898 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, NULL, CHECK_NULL);
899 if (parameter_types.is_null()) return NULL;
900
901 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
902 if (exception_types.is_null()) return NULL;
903
904 const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
905
906 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
907
908 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
909 java_lang_reflect_Constructor::set_slot(ch(), slot);
910 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
911 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
912 java_lang_reflect_Constructor::set_modifiers(ch(), modifiers);
913 java_lang_reflect_Constructor::set_override(ch(), false);
921 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
922 java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
923 }
924 if (java_lang_reflect_Constructor::has_parameter_annotations_field()) {
925 typeArrayOop an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
926 java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
927 }
928 if (java_lang_reflect_Constructor::has_type_annotations_field()) {
929 typeArrayOop an_oop = Annotations::make_java_array(method->type_annotations(), CHECK_NULL);
930 java_lang_reflect_Constructor::set_type_annotations(ch(), an_oop);
931 }
932 return ch();
933 }
934
935
936 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
937 Symbol* field_name = fd->name();
938 oop name_oop = StringTable::intern(field_name, CHECK_NULL);
939 Handle name = Handle(THREAD, name_oop);
940 Symbol* signature = fd->signature();
941 instanceKlassHandle holder (THREAD, fd->field_holder());
942 Handle type = new_type(signature, holder, CHECK_NULL);
943 Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
944
945 java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
946 java_lang_reflect_Field::set_slot(rh(), fd->index());
947 java_lang_reflect_Field::set_name(rh(), name());
948 java_lang_reflect_Field::set_type(rh(), type());
949 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
950 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
951 java_lang_reflect_Field::set_override(rh(), false);
952 if (java_lang_reflect_Field::has_signature_field() &&
953 fd->has_generic_signature()) {
954 Symbol* gs = fd->generic_signature();
955 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
956 java_lang_reflect_Field::set_signature(rh(), sig());
957 }
958 if (java_lang_reflect_Field::has_annotations_field()) {
959 typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
960 java_lang_reflect_Field::set_annotations(rh(), an_oop);
961 }
968
969 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
970 int flags, TRAPS) {
971
972 Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
973
974 if(NULL != sym) {
975 Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
976 java_lang_reflect_Parameter::set_name(rh(), name());
977 } else {
978 java_lang_reflect_Parameter::set_name(rh(), NULL);
979 }
980
981 java_lang_reflect_Parameter::set_modifiers(rh(), flags);
982 java_lang_reflect_Parameter::set_executable(rh(), method());
983 java_lang_reflect_Parameter::set_index(rh(), index);
984 return rh();
985 }
986
987
988 static methodHandle resolve_interface_call(instanceKlassHandle klass,
989 const methodHandle& method,
990 KlassHandle recv_klass,
991 Handle receiver,
992 TRAPS) {
993
994 assert(!method.is_null() , "method should not be null");
995
996 CallInfo info;
997 Symbol* signature = method->signature();
998 Symbol* name = method->name();
999 LinkResolver::resolve_interface_call(info, receiver, recv_klass,
1000 LinkInfo(klass, name, signature),
1001 true,
1002 CHECK_(methodHandle()));
1003 return info.selected_method();
1004 }
1005
1006 // Conversion
1007 static BasicType basic_type_mirror_to_basic_type(oop basic_type_mirror, TRAPS) {
1008 assert(java_lang_Class::is_primitive(basic_type_mirror),
1009 "just checking");
1010 return java_lang_Class::primitive_type(basic_type_mirror);
1018 case T_BOOLEAN:
1019 value->z = (jboolean) (value->i & 1);
1020 return;
1021 case T_BYTE:
1022 value->b = (jbyte)value->i;
1023 return;
1024 case T_CHAR:
1025 value->c = (jchar)value->i;
1026 return;
1027 case T_SHORT:
1028 value->s = (jshort)value->i;
1029 return;
1030 default:
1031 break; // fail
1032 }
1033 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
1034 }
1035
1036
1037 // Method call (shared by invoke_method and invoke_constructor)
1038 static oop invoke(instanceKlassHandle klass,
1039 methodHandle reflected_method,
1040 Handle receiver,
1041 bool override,
1042 objArrayHandle ptypes,
1043 BasicType rtype,
1044 objArrayHandle args,
1045 bool is_method_invoke,
1046 TRAPS) {
1047
1048 ResourceMark rm(THREAD);
1049
1050 methodHandle method; // actual method to invoke
1051 KlassHandle target_klass; // target klass, receiver's klass for non-static
1052
1053 // Ensure klass is initialized
1054 klass->initialize(CHECK_NULL);
1055
1056 bool is_static = reflected_method->is_static();
1057 if (is_static) {
1058 // ignore receiver argument
1059 method = reflected_method;
1060 target_klass = klass;
1061 } else {
1062 // check for null receiver
1063 if (receiver.is_null()) {
1064 THROW_0(vmSymbols::java_lang_NullPointerException());
1065 }
1066 // Check class of receiver against class declaring method
1067 if (!receiver->is_a(klass())) {
1068 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
1069 }
1070 // target klass is receiver's klass
1071 target_klass = KlassHandle(THREAD, receiver->klass());
1072 // no need to resolve if method is private or <init>
1073 if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) {
1074 method = reflected_method;
1075 } else {
1076 // resolve based on the receiver
1077 if (reflected_method->method_holder()->is_interface()) {
1078 // resolve interface call
1079 //
1080 // Match resolution errors with those thrown due to reflection inlining
1081 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
1082 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
1083 if (HAS_PENDING_EXCEPTION) {
1084 // Method resolution threw an exception; wrap it in an InvocationTargetException
1085 oop resolution_exception = PENDING_EXCEPTION;
1086 CLEAR_PENDING_EXCEPTION;
1087 // JVMTI has already reported the pending exception
1088 // JVMTI internal flag reset is needed in order to report InvocationTargetException
1089 if (THREAD->is_Java_thread()) {
1090 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
1091 }
1092 JavaCallArguments args(Handle(THREAD, resolution_exception));
1093 THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1094 vmSymbols::throwable_void_signature(),
1095 &args);
1096 }
1097 } else {
1098 // if the method can be overridden, we resolve using the vtable index.
1099 assert(!reflected_method->has_itable_index(), "");
1100 int index = reflected_method->vtable_index();
1101 method = reflected_method;
1102 if (index != Method::nonvirtual_vtable_index) {
1103 method = methodHandle(THREAD, target_klass->method_at_vtable(index));
1104 }
1105 if (!method.is_null()) {
1106 // Check for abstract methods as well
1107 if (method->is_abstract()) {
1108 // new default: 6531596
1109 ResourceMark rm(THREAD);
1110 Handle h_origexception = Exceptions::new_exception(THREAD,
1111 vmSymbols::java_lang_AbstractMethodError(),
1112 Method::name_and_sig_as_C_string(target_klass(),
1113 method->name(),
1114 method->signature()));
1115 JavaCallArguments args(h_origexception);
1116 THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1117 vmSymbols::throwable_void_signature(),
1118 &args);
1119 }
1120 }
1121 }
1122 }
1123 }
1124
1125 // I believe this is a ShouldNotGetHere case which requires
1126 // an internal vtable bug. If you ever get this please let Karen know.
1127 if (method.is_null()) {
1128 ResourceMark rm(THREAD);
1129 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(),
1130 Method::name_and_sig_as_C_string(klass(),
1131 reflected_method->name(),
1132 reflected_method->signature()));
1133 }
1134
1135 assert(ptypes->is_objArray(), "just checking");
1136 int args_len = args.is_null() ? 0 : args->length();
1137 // Check number of arguments
1138 if (ptypes->length() != args_len) {
1139 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1140 "wrong number of arguments");
1141 }
1142
1143 // Create object to contain parameters for the JavaCall
1144 JavaCallArguments java_args(method->size_of_parameters());
1145
1146 if (!is_static) {
1147 java_args.push_oop(receiver);
1148 }
1149
1150 for (int i = 0; i < args_len; i++) {
1212 }
1213 }
1214
1215 // This would be nicer if, say, java.lang.reflect.Method was a subclass
1216 // of java.lang.reflect.Constructor
1217
1218 oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) {
1219 oop mirror = java_lang_reflect_Method::clazz(method_mirror);
1220 int slot = java_lang_reflect_Method::slot(method_mirror);
1221 bool override = java_lang_reflect_Method::override(method_mirror) != 0;
1222 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror)));
1223
1224 oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror);
1225 BasicType rtype;
1226 if (java_lang_Class::is_primitive(return_type_mirror)) {
1227 rtype = basic_type_mirror_to_basic_type(return_type_mirror, CHECK_NULL);
1228 } else {
1229 rtype = T_OBJECT;
1230 }
1231
1232 instanceKlassHandle klass(THREAD, java_lang_Class::as_Klass(mirror));
1233 Method* m = klass->method_with_idnum(slot);
1234 if (m == NULL) {
1235 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1236 }
1237 methodHandle method(THREAD, m);
1238
1239 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1240 }
1241
1242
1243 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1244 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror);
1245 int slot = java_lang_reflect_Constructor::slot(constructor_mirror);
1246 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1247 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1248
1249 instanceKlassHandle klass(THREAD, java_lang_Class::as_Klass(mirror));
1250 Method* m = klass->method_with_idnum(slot);
1251 if (m == NULL) {
1252 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1253 }
1254 methodHandle method(THREAD, m);
1255 assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1256
1257 // Make sure klass gets initialize
1258 klass->initialize(CHECK_NULL);
1259
1260 // Create new instance (the receiver)
1261 klass->check_valid_for_instantiation(false, CHECK_NULL);
1262 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1263
1264 // Ignore result from call and return receiver
1265 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1266 return receiver();
1267 }
|
691
692 // Allow all accesses from jdk/internal/reflect/MagicAccessorImpl subclasses to
693 // succeed trivially.
694 if (current_class->is_subclass_of(SystemDictionary::reflect_MagicAccessorImpl_klass())) {
695 return true;
696 }
697
698 return can_relax_access_check_for(
699 current_class, field_class, classloader_only);
700 }
701
702 bool Reflection::is_same_class_package(const Klass* class1, const Klass* class2) {
703 return InstanceKlass::cast(class1)->is_same_class_package(class2);
704 }
705
706 // Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not,
707 // throw an incompatible class change exception
708 // If inner_is_member, require the inner to be a member of the outer.
709 // If !inner_is_member, require the inner to be anonymous (a non-member).
710 // Caller is responsible for figuring out in advance which case must be true.
711 void Reflection::check_for_inner_class(const InstanceKlass* outer, const InstanceKlass* inner,
712 bool inner_is_member, TRAPS) {
713 InnerClassesIterator iter(outer);
714 constantPoolHandle cp (THREAD, outer->constants());
715 for (; !iter.done(); iter.next()) {
716 int ioff = iter.inner_class_info_index();
717 int ooff = iter.outer_class_info_index();
718
719 if (inner_is_member && ioff != 0 && ooff != 0) {
720 Klass* o = cp->klass_at(ooff, CHECK);
721 if (o == outer) {
722 Klass* i = cp->klass_at(ioff, CHECK);
723 if (i == inner) {
724 return;
725 }
726 }
727 }
728 if (!inner_is_member && ioff != 0 && ooff == 0 &&
729 cp->klass_name_at_matches(inner, ioff)) {
730 Klass* i = cp->klass_at(ioff, CHECK);
731 if (i == inner) {
732 return;
733 }
734 }
735 }
736
737 // 'inner' not declared as an inner klass in outer
738 ResourceMark rm(THREAD);
739 Exceptions::fthrow(
740 THREAD_AND_LOCATION,
741 vmSymbols::java_lang_IncompatibleClassChangeError(),
742 "%s and %s disagree on InnerClasses attribute",
743 outer->external_name(),
744 inner->external_name()
745 );
746 }
747
748 // Utility method converting a single SignatureStream element into java.lang.Class instance
749 static oop get_mirror_from_signature(methodHandle method,
750 SignatureStream* ss,
751 TRAPS) {
785 Symbol* signature = method->signature();
786 SignatureStream ss(signature);
787 while (!ss.at_return_type()) {
788 oop mirror = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle()));
789 mirrors->obj_at_put(index++, mirror);
790 ss.next();
791 }
792 assert(index == parameter_count, "invalid parameter count");
793 if (return_type != NULL) {
794 // Collect return type as well
795 assert(ss.at_return_type(), "return type should be present");
796 *return_type = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle()));
797 }
798 return mirrors;
799 }
800
801 static objArrayHandle get_exception_types(methodHandle method, TRAPS) {
802 return method->resolved_checked_exceptions(THREAD);
803 }
804
805 static Handle new_type(Symbol* signature, Klass* k, TRAPS) {
806 // Basic types
807 BasicType type = vmSymbols::signature_type(signature);
808 if (type != T_OBJECT) {
809 return Handle(THREAD, Universe::java_mirror(type));
810 }
811
812 Klass* result =
813 SystemDictionary::resolve_or_fail(signature,
814 Handle(THREAD, k->class_loader()),
815 Handle(THREAD, k->protection_domain()),
816 true, CHECK_(Handle()));
817
818 if (log_is_enabled(Debug, class, resolve)) {
819 trace_class_resolution(result);
820 }
821
822 oop nt = result->java_mirror();
823 return Handle(THREAD, nt);
824 }
825
826
827 oop Reflection::new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS) {
828 // Allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
829 assert(!method()->is_initializer() ||
830 (for_constant_pool_access && method()->is_static()),
831 "should call new_constructor instead");
832 InstanceKlass* holder = method->method_holder();
833 int slot = method->method_idnum();
834
835 Symbol* signature = method->signature();
836 int parameter_count = ArgumentCount(signature).size();
837 oop return_type_oop = NULL;
838 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
839 if (parameter_types.is_null() || return_type_oop == NULL) return NULL;
840
841 Handle return_type(THREAD, return_type_oop);
842
843 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
844
845 if (exception_types.is_null()) return NULL;
846
847 Symbol* method_name = method->name();
848 oop name_oop = StringTable::intern(method_name, CHECK_NULL);
849 Handle name = Handle(THREAD, name_oop);
850 if (name == NULL) return NULL;
851
852 const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
873 }
874 if (java_lang_reflect_Method::has_parameter_annotations_field()) {
875 typeArrayOop an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
876 java_lang_reflect_Method::set_parameter_annotations(mh(), an_oop);
877 }
878 if (java_lang_reflect_Method::has_annotation_default_field()) {
879 typeArrayOop an_oop = Annotations::make_java_array(method->annotation_default(), CHECK_NULL);
880 java_lang_reflect_Method::set_annotation_default(mh(), an_oop);
881 }
882 if (java_lang_reflect_Method::has_type_annotations_field()) {
883 typeArrayOop an_oop = Annotations::make_java_array(method->type_annotations(), CHECK_NULL);
884 java_lang_reflect_Method::set_type_annotations(mh(), an_oop);
885 }
886 return mh();
887 }
888
889
890 oop Reflection::new_constructor(const methodHandle& method, TRAPS) {
891 assert(method()->is_initializer(), "should call new_method instead");
892
893 InstanceKlass* holder = method->method_holder();
894 int slot = method->method_idnum();
895
896 Symbol* signature = method->signature();
897 int parameter_count = ArgumentCount(signature).size();
898 objArrayHandle parameter_types = get_parameter_types(method, parameter_count, NULL, CHECK_NULL);
899 if (parameter_types.is_null()) return NULL;
900
901 objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
902 if (exception_types.is_null()) return NULL;
903
904 const int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
905
906 Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
907
908 java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
909 java_lang_reflect_Constructor::set_slot(ch(), slot);
910 java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
911 java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
912 java_lang_reflect_Constructor::set_modifiers(ch(), modifiers);
913 java_lang_reflect_Constructor::set_override(ch(), false);
921 typeArrayOop an_oop = Annotations::make_java_array(method->annotations(), CHECK_NULL);
922 java_lang_reflect_Constructor::set_annotations(ch(), an_oop);
923 }
924 if (java_lang_reflect_Constructor::has_parameter_annotations_field()) {
925 typeArrayOop an_oop = Annotations::make_java_array(method->parameter_annotations(), CHECK_NULL);
926 java_lang_reflect_Constructor::set_parameter_annotations(ch(), an_oop);
927 }
928 if (java_lang_reflect_Constructor::has_type_annotations_field()) {
929 typeArrayOop an_oop = Annotations::make_java_array(method->type_annotations(), CHECK_NULL);
930 java_lang_reflect_Constructor::set_type_annotations(ch(), an_oop);
931 }
932 return ch();
933 }
934
935
936 oop Reflection::new_field(fieldDescriptor* fd, TRAPS) {
937 Symbol* field_name = fd->name();
938 oop name_oop = StringTable::intern(field_name, CHECK_NULL);
939 Handle name = Handle(THREAD, name_oop);
940 Symbol* signature = fd->signature();
941 InstanceKlass* holder = fd->field_holder();
942 Handle type = new_type(signature, holder, CHECK_NULL);
943 Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
944
945 java_lang_reflect_Field::set_clazz(rh(), fd->field_holder()->java_mirror());
946 java_lang_reflect_Field::set_slot(rh(), fd->index());
947 java_lang_reflect_Field::set_name(rh(), name());
948 java_lang_reflect_Field::set_type(rh(), type());
949 // Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
950 java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
951 java_lang_reflect_Field::set_override(rh(), false);
952 if (java_lang_reflect_Field::has_signature_field() &&
953 fd->has_generic_signature()) {
954 Symbol* gs = fd->generic_signature();
955 Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
956 java_lang_reflect_Field::set_signature(rh(), sig());
957 }
958 if (java_lang_reflect_Field::has_annotations_field()) {
959 typeArrayOop an_oop = Annotations::make_java_array(fd->annotations(), CHECK_NULL);
960 java_lang_reflect_Field::set_annotations(rh(), an_oop);
961 }
968
969 oop Reflection::new_parameter(Handle method, int index, Symbol* sym,
970 int flags, TRAPS) {
971
972 Handle rh = java_lang_reflect_Parameter::create(CHECK_NULL);
973
974 if(NULL != sym) {
975 Handle name = java_lang_String::create_from_symbol(sym, CHECK_NULL);
976 java_lang_reflect_Parameter::set_name(rh(), name());
977 } else {
978 java_lang_reflect_Parameter::set_name(rh(), NULL);
979 }
980
981 java_lang_reflect_Parameter::set_modifiers(rh(), flags);
982 java_lang_reflect_Parameter::set_executable(rh(), method());
983 java_lang_reflect_Parameter::set_index(rh(), index);
984 return rh();
985 }
986
987
988 static methodHandle resolve_interface_call(InstanceKlass* klass,
989 const methodHandle& method,
990 Klass* recv_klass,
991 Handle receiver,
992 TRAPS) {
993
994 assert(!method.is_null() , "method should not be null");
995
996 CallInfo info;
997 Symbol* signature = method->signature();
998 Symbol* name = method->name();
999 LinkResolver::resolve_interface_call(info, receiver, recv_klass,
1000 LinkInfo(klass, name, signature),
1001 true,
1002 CHECK_(methodHandle()));
1003 return info.selected_method();
1004 }
1005
1006 // Conversion
1007 static BasicType basic_type_mirror_to_basic_type(oop basic_type_mirror, TRAPS) {
1008 assert(java_lang_Class::is_primitive(basic_type_mirror),
1009 "just checking");
1010 return java_lang_Class::primitive_type(basic_type_mirror);
1018 case T_BOOLEAN:
1019 value->z = (jboolean) (value->i & 1);
1020 return;
1021 case T_BYTE:
1022 value->b = (jbyte)value->i;
1023 return;
1024 case T_CHAR:
1025 value->c = (jchar)value->i;
1026 return;
1027 case T_SHORT:
1028 value->s = (jshort)value->i;
1029 return;
1030 default:
1031 break; // fail
1032 }
1033 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
1034 }
1035
1036
1037 // Method call (shared by invoke_method and invoke_constructor)
1038 static oop invoke(InstanceKlass* klass,
1039 methodHandle reflected_method,
1040 Handle receiver,
1041 bool override,
1042 objArrayHandle ptypes,
1043 BasicType rtype,
1044 objArrayHandle args,
1045 bool is_method_invoke,
1046 TRAPS) {
1047
1048 ResourceMark rm(THREAD);
1049
1050 methodHandle method; // actual method to invoke
1051 Klass* target_klass; // target klass, receiver's klass for non-static
1052
1053 // Ensure klass is initialized
1054 klass->initialize(CHECK_NULL);
1055
1056 bool is_static = reflected_method->is_static();
1057 if (is_static) {
1058 // ignore receiver argument
1059 method = reflected_method;
1060 target_klass = klass;
1061 } else {
1062 // check for null receiver
1063 if (receiver.is_null()) {
1064 THROW_0(vmSymbols::java_lang_NullPointerException());
1065 }
1066 // Check class of receiver against class declaring method
1067 if (!receiver->is_a(klass)) {
1068 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
1069 }
1070 // target klass is receiver's klass
1071 target_klass = receiver->klass();
1072 // no need to resolve if method is private or <init>
1073 if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) {
1074 method = reflected_method;
1075 } else {
1076 // resolve based on the receiver
1077 if (reflected_method->method_holder()->is_interface()) {
1078 // resolve interface call
1079 //
1080 // Match resolution errors with those thrown due to reflection inlining
1081 // Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
1082 method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
1083 if (HAS_PENDING_EXCEPTION) {
1084 // Method resolution threw an exception; wrap it in an InvocationTargetException
1085 oop resolution_exception = PENDING_EXCEPTION;
1086 CLEAR_PENDING_EXCEPTION;
1087 // JVMTI has already reported the pending exception
1088 // JVMTI internal flag reset is needed in order to report InvocationTargetException
1089 if (THREAD->is_Java_thread()) {
1090 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
1091 }
1092 JavaCallArguments args(Handle(THREAD, resolution_exception));
1093 THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1094 vmSymbols::throwable_void_signature(),
1095 &args);
1096 }
1097 } else {
1098 // if the method can be overridden, we resolve using the vtable index.
1099 assert(!reflected_method->has_itable_index(), "");
1100 int index = reflected_method->vtable_index();
1101 method = reflected_method;
1102 if (index != Method::nonvirtual_vtable_index) {
1103 method = methodHandle(THREAD, target_klass->method_at_vtable(index));
1104 }
1105 if (!method.is_null()) {
1106 // Check for abstract methods as well
1107 if (method->is_abstract()) {
1108 // new default: 6531596
1109 ResourceMark rm(THREAD);
1110 Handle h_origexception = Exceptions::new_exception(THREAD,
1111 vmSymbols::java_lang_AbstractMethodError(),
1112 Method::name_and_sig_as_C_string(target_klass,
1113 method->name(),
1114 method->signature()));
1115 JavaCallArguments args(h_origexception);
1116 THROW_ARG_0(vmSymbols::java_lang_reflect_InvocationTargetException(),
1117 vmSymbols::throwable_void_signature(),
1118 &args);
1119 }
1120 }
1121 }
1122 }
1123 }
1124
1125 // I believe this is a ShouldNotGetHere case which requires
1126 // an internal vtable bug. If you ever get this please let Karen know.
1127 if (method.is_null()) {
1128 ResourceMark rm(THREAD);
1129 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(),
1130 Method::name_and_sig_as_C_string(klass,
1131 reflected_method->name(),
1132 reflected_method->signature()));
1133 }
1134
1135 assert(ptypes->is_objArray(), "just checking");
1136 int args_len = args.is_null() ? 0 : args->length();
1137 // Check number of arguments
1138 if (ptypes->length() != args_len) {
1139 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1140 "wrong number of arguments");
1141 }
1142
1143 // Create object to contain parameters for the JavaCall
1144 JavaCallArguments java_args(method->size_of_parameters());
1145
1146 if (!is_static) {
1147 java_args.push_oop(receiver);
1148 }
1149
1150 for (int i = 0; i < args_len; i++) {
1212 }
1213 }
1214
1215 // This would be nicer if, say, java.lang.reflect.Method was a subclass
1216 // of java.lang.reflect.Constructor
1217
1218 oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) {
1219 oop mirror = java_lang_reflect_Method::clazz(method_mirror);
1220 int slot = java_lang_reflect_Method::slot(method_mirror);
1221 bool override = java_lang_reflect_Method::override(method_mirror) != 0;
1222 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror)));
1223
1224 oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror);
1225 BasicType rtype;
1226 if (java_lang_Class::is_primitive(return_type_mirror)) {
1227 rtype = basic_type_mirror_to_basic_type(return_type_mirror, CHECK_NULL);
1228 } else {
1229 rtype = T_OBJECT;
1230 }
1231
1232 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1233 Method* m = klass->method_with_idnum(slot);
1234 if (m == NULL) {
1235 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1236 }
1237 methodHandle method(THREAD, m);
1238
1239 return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
1240 }
1241
1242
1243 oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
1244 oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror);
1245 int slot = java_lang_reflect_Constructor::slot(constructor_mirror);
1246 bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
1247 objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
1248
1249 InstanceKlass* klass = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1250 Method* m = klass->method_with_idnum(slot);
1251 if (m == NULL) {
1252 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
1253 }
1254 methodHandle method(THREAD, m);
1255 assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
1256
1257 // Make sure klass gets initialize
1258 klass->initialize(CHECK_NULL);
1259
1260 // Create new instance (the receiver)
1261 klass->check_valid_for_instantiation(false, CHECK_NULL);
1262 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
1263
1264 // Ignore result from call and return receiver
1265 invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
1266 return receiver();
1267 }
|