917 wait = true; 918 ol.waitUninterruptibly(CHECK); 919 } 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(). 1002 PerfClassTraceTime::CLASS_CLINIT); 1003 call_class_initializer(THREAD); 1004 } 1005 1006 // Step 9 1007 if (!HAS_PENDING_EXCEPTION) { 1008 set_initialization_state_and_notify(fully_initialized, CHECK); 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 | 917 wait = true; 918 ol.waitUninterruptibly(CHECK); 919 } 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 // Delegate throwing of NoClassDefFoundError to java handler 938 class_loader_data()->throw_reinit_exception(java_mirror_handle(), THREAD); 939 if (HAS_PENDING_EXCEPTION) { 940 Handle e(THREAD, PENDING_EXCEPTION); 941 CLEAR_PENDING_EXCEPTION; 942 if (e->is_a(SystemDictionary::NoClassDefFoundError_klass())) { 943 // java handler has thrown correct type of exception -> rethrow it 944 THROW_OOP(e()); 945 } 946 } 947 // misbehaving/incapable java handler (throwing OOME or not at all) -> fallback 948 const char* className = external_name(); 949 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), className); 950 } 951 952 // Step 6 953 set_init_state(being_initialized); 954 set_init_thread(self); 955 } 956 957 // Step 7 958 // Next, if C is a class rather than an interface, initialize it's super class and super 959 // interfaces. 960 if (!is_interface()) { 961 Klass* super_klass = super(); 962 if (super_klass != NULL && super_klass->should_be_initialized()) { 963 super_klass->initialize(THREAD); 964 } 965 // If C implements any interface that declares a non-static, concrete method, 966 // the initialization of C triggers initialization of its super interfaces. 967 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and 968 // having a superinterface that declares, non-static, concrete methods 969 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) { 970 initialize_super_interfaces(THREAD); 971 } 972 973 // If any exceptions, complete abruptly, throwing the same exception as above. 974 if (HAS_PENDING_EXCEPTION) { 975 Handle e(THREAD, PENDING_EXCEPTION); 976 CLEAR_PENDING_EXCEPTION; 977 { 978 EXCEPTION_MARK; 979 // Record the exception thrown from super class/interface initialization so that 980 // it can be chained into potential later NoClassDefFoundErrors. 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(). 1007 PerfClassTraceTime::CLASS_CLINIT); 1008 call_class_initializer(THREAD); 1009 } 1010 1011 // Step 9 1012 if (!HAS_PENDING_EXCEPTION) { 1013 set_initialization_state_and_notify(fully_initialized, CHECK); 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 if (!e->is_a(SystemDictionary::Error_klass())) { 1028 // Wrap anything but errors into ExceptionInInitializerError. 1029 JavaCallArguments args(e); 1030 Handle h_loader(THREAD, NULL); 1031 Handle h_prot(THREAD, NULL); 1032 e = Exceptions::new_exception(THREAD, 1033 vmSymbols::java_lang_ExceptionInInitializerError(), 1034 vmSymbols::throwable_void_signature(), 1035 &args, h_loader, h_prot); 1036 } 1037 // Record the exception that originally caused <clinit> to fail so 1038 // it can be chained into potential later NoClassDefFoundErrors. 1039 class_loader_data()->record_init_exception(java_mirror_handle(), e, THREAD); 1040 // Locks object, set state, and notify all waiting threads 1041 set_initialization_state_and_notify(initialization_error, THREAD); 1042 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below 1043 // JVMTI has already reported the pending exception 1044 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError 1045 JvmtiExport::clear_detected_exception((JavaThread*)THREAD); 1046 } 1047 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait); 1048 THROW_OOP(e()); 1049 } 1050 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait); 1051 } 1052 1053 1054 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) { 1055 Handle h_init_lock(THREAD, init_lock()); 1056 if (h_init_lock() != NULL) { 1057 ObjectLocker ol(h_init_lock, THREAD); 1058 set_init_state(state); 1059 fence_and_clear_init_lock(); 1060 ol.notify_all(CHECK); 1061 } else { 1062 assert(h_init_lock() != NULL, "The initialization state should never be set twice"); 1063 set_init_state(state); 1064 } 1065 } 1066 1067 // The embedded _implementor field can only record one implementor. 1068 // When there are more than one implementors, the _implementor field |