920
921 // Step 3
922 if (is_being_initialized() && is_reentrant_initialization(self)) {
923 DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
924 return;
925 }
926
927 // Step 4
928 if (is_initialized()) {
929 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, -1, wait);
930 return;
931 }
932
933 // Step 5
934 if (is_in_error_state()) {
935 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
936 ResourceMark rm(THREAD);
937 const char* desc = "Could not initialize class ";
938 const char* className = external_name();
939 size_t msglen = strlen(desc) + strlen(className) + 1;
940 char* message = NEW_RESOURCE_ARRAY(char, msglen);
941 if (NULL == message) {
942 // Out of memory: can't create detailed error message
943 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className);
944 } else {
945 jio_snprintf(message, msglen, "%s%s", desc, className);
946 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), message);
947 }
948 }
949
950 // Step 6
951 set_init_state(being_initialized);
952 set_init_thread(self);
953 }
954
955 // Step 7
956 // Next, if C is a class rather than an interface, initialize it's super class and super
957 // interfaces.
958 if (!is_interface()) {
959 Klass* super_klass = super();
960 if (super_klass != NULL && super_klass->should_be_initialized()) {
961 super_klass->initialize(THREAD);
962 }
963 // If C implements any interface that declares a non-static, concrete method,
964 // the initialization of C triggers initialization of its super interfaces.
965 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
966 // having a superinterface that declares, non-static, concrete methods
967 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
968 initialize_super_interfaces(THREAD);
969 }
970
971 // If any exceptions, complete abruptly, throwing the same exception as above.
972 if (HAS_PENDING_EXCEPTION) {
973 Handle e(THREAD, PENDING_EXCEPTION);
974 CLEAR_PENDING_EXCEPTION;
975 {
976 EXCEPTION_MARK;
977 // Locks object, set state, and notify all waiting threads
978 set_initialization_state_and_notify(initialization_error, THREAD);
979 CLEAR_PENDING_EXCEPTION;
980 }
981 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
982 THROW_OOP(e());
983 }
984 }
985
986
987 // Look for aot compiled methods for this klass, including class initializer.
988 AOTLoader::load_for_klass(this, THREAD);
989
990 // Step 8
991 {
992 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
993 JavaThread* jt = (JavaThread*)THREAD;
994 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
995 // Timer includes any side effects of class initialization (resolution,
996 // etc), but not recursive entry into call_class_initializer().
1009 {
1010 debug_only(vtable().verify(tty, true);)
1011 }
1012 }
1013 else {
1014 // Step 10 and 11
1015 Handle e(THREAD, PENDING_EXCEPTION);
1016 CLEAR_PENDING_EXCEPTION;
1017 // JVMTI has already reported the pending exception
1018 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1019 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
1020 {
1021 EXCEPTION_MARK;
1022 set_initialization_state_and_notify(initialization_error, THREAD);
1023 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1024 // JVMTI has already reported the pending exception
1025 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1026 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
1027 }
1028 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1029 if (e->is_a(SystemDictionary::Error_klass())) {
1030 THROW_OOP(e());
1031 } else {
1032 JavaCallArguments args(e);
1033 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1034 vmSymbols::throwable_void_signature(),
1035 &args);
1036 }
1037 }
1038 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1039 }
1040
1041
1042 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1043 Handle h_init_lock(THREAD, init_lock());
1044 if (h_init_lock() != NULL) {
1045 ObjectLocker ol(h_init_lock, THREAD);
1046 set_init_state(state);
1047 fence_and_clear_init_lock();
1048 ol.notify_all(CHECK);
1049 } else {
1050 assert(h_init_lock() != NULL, "The initialization state should never be set twice");
1051 set_init_state(state);
1052 }
1053 }
1054
1055 // The embedded _implementor field can only record one implementor.
1056 // When there are more than one implementors, the _implementor field
|
920
921 // Step 3
922 if (is_being_initialized() && is_reentrant_initialization(self)) {
923 DTRACE_CLASSINIT_PROBE_WAIT(recursive, -1, wait);
924 return;
925 }
926
927 // Step 4
928 if (is_initialized()) {
929 DTRACE_CLASSINIT_PROBE_WAIT(concurrent, -1, wait);
930 return;
931 }
932
933 // Step 5
934 if (is_in_error_state()) {
935 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
936 ResourceMark rm(THREAD);
937 const char* desc = "Could not initialize class ";
938 const char* className = external_name();
939 size_t msglen = strlen(desc) + strlen(className) + 1;
940 char* message = NEW_RESOURCE_ARRAY_IN_THREAD_RETURN_NULL(THREAD, char, msglen);
941 if (NULL == message) {
942 // Out of memory: can't create detailed error message
943 message = const_cast<char*>(className);
944 } else {
945 jio_snprintf(message, msglen, "%s%s", desc, className);
946 }
947 Exceptions::_throw_msg_cause(THREAD_AND_LOCATION,
948 vmSymbols::java_lang_NoClassDefFoundError(),
949 message,
950 class_loader_data()->query_init_exception(java_mirror_handle(), THREAD));
951 return;
952 }
953
954 // Step 6
955 set_init_state(being_initialized);
956 set_init_thread(self);
957 }
958
959 // Step 7
960 // Next, if C is a class rather than an interface, initialize it's super class and super
961 // interfaces.
962 if (!is_interface()) {
963 Klass* super_klass = super();
964 if (super_klass != NULL && super_klass->should_be_initialized()) {
965 super_klass->initialize(THREAD);
966 }
967 // If C implements any interface that declares a non-static, concrete method,
968 // the initialization of C triggers initialization of its super interfaces.
969 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
970 // having a superinterface that declares, non-static, concrete methods
971 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
972 initialize_super_interfaces(THREAD);
973 }
974
975 // If any exceptions, complete abruptly, throwing the same exception as above.
976 if (HAS_PENDING_EXCEPTION) {
977 Handle e(THREAD, PENDING_EXCEPTION);
978 CLEAR_PENDING_EXCEPTION;
979 {
980 EXCEPTION_MARK;
981 class_loader_data()->record_init_exception(java_mirror_handle(), e, THREAD);
982 // Locks object, set state, and notify all waiting threads
983 set_initialization_state_and_notify(initialization_error, THREAD);
984 CLEAR_PENDING_EXCEPTION;
985 }
986 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
987 THROW_OOP(e());
988 }
989 }
990
991
992 // Look for aot compiled methods for this klass, including class initializer.
993 AOTLoader::load_for_klass(this, THREAD);
994
995 // Step 8
996 {
997 assert(THREAD->is_Java_thread(), "non-JavaThread in initialize_impl");
998 JavaThread* jt = (JavaThread*)THREAD;
999 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1000 // Timer includes any side effects of class initialization (resolution,
1001 // etc), but not recursive entry into call_class_initializer().
1014 {
1015 debug_only(vtable().verify(tty, true);)
1016 }
1017 }
1018 else {
1019 // Step 10 and 11
1020 Handle e(THREAD, PENDING_EXCEPTION);
1021 CLEAR_PENDING_EXCEPTION;
1022 // JVMTI has already reported the pending exception
1023 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1024 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
1025 {
1026 EXCEPTION_MARK;
1027 set_initialization_state_and_notify(initialization_error, THREAD);
1028 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1029 // JVMTI has already reported the pending exception
1030 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1031 JvmtiExport::clear_detected_exception((JavaThread*)THREAD);
1032 }
1033 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1034 if (!e->is_a(SystemDictionary::Error_klass())) {
1035 // Wrap anything but errors into ExceptionInInitializerError.
1036 JavaCallArguments args(e);
1037 Handle h_loader(THREAD, NULL);
1038 Handle h_prot(THREAD, NULL);
1039 e = Exceptions::new_exception(THREAD,
1040 vmSymbols::java_lang_ExceptionInInitializerError(),
1041 vmSymbols::throwable_void_signature(),
1042 &args, h_loader, h_prot);
1043 }
1044 // Store the exception that originally caused <clinit> to fail so
1045 // it can be chained into potential later NoClassDefFoundErrors.
1046 class_loader_data()->record_init_exception(java_mirror_handle(), e, THREAD);
1047 THROW_OOP(e());
1048 }
1049 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1050 }
1051
1052
1053 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1054 Handle h_init_lock(THREAD, init_lock());
1055 if (h_init_lock() != NULL) {
1056 ObjectLocker ol(h_init_lock, THREAD);
1057 set_init_state(state);
1058 fence_and_clear_init_lock();
1059 ol.notify_all(CHECK);
1060 } else {
1061 assert(h_init_lock() != NULL, "The initialization state should never be set twice");
1062 set_init_state(state);
1063 }
1064 }
1065
1066 // The embedded _implementor field can only record one implementor.
1067 // When there are more than one implementors, the _implementor field
|