--- old/src/hotspot/share/interpreter/linkResolver.cpp 2020-03-06 00:25:51.869494254 -0500 +++ new/src/hotspot/share/interpreter/linkResolver.cpp 2020-03-06 00:25:50.704481549 -0500 @@ -579,24 +579,51 @@ sel_klass, flags, true, false, CHECK); - // Any existing exceptions that may have been thrown, for example LinkageErrors - // from nest-host resolution, have been allowed to propagate. + // Any existing exceptions that may have been thrown + // have been allowed to propagate. if (!can_access) { ResourceMark rm(THREAD); + stringStream ss; bool same_module = (sel_klass->module() == ref_klass->module()); - Exceptions::fthrow( - THREAD_AND_LOCATION, - vmSymbols::java_lang_IllegalAccessError(), - "class %s tried to access %s%s%smethod '%s' (%s%s%s)", - ref_klass->external_name(), - sel_method->is_abstract() ? "abstract " : "", - sel_method->is_protected() ? "protected " : "", - sel_method->is_private() ? "private " : "", - sel_method->external_name(), - (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(), - (same_module) ? "" : "; ", - (same_module) ? "" : sel_klass->class_in_module_of_loader() - ); + ss.print("class %s tried to access %s%s%smethod '%s' (%s%s%s)", + ref_klass->external_name(), + sel_method->is_abstract() ? "abstract " : "", + sel_method->is_protected() ? "protected " : "", + sel_method->is_private() ? "private " : "", + sel_method->external_name(), + (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(), + (same_module) ? "" : "; ", + (same_module) ? "" : sel_klass->class_in_module_of_loader() + ); + + // For private access check if there was a problem with nest host + // resolution, and if so report that as part of the message. + if (sel_method->is_private()) { + assert(ref_klass->is_instance_klass(), "must be"); + assert(sel_klass->is_instance_klass(), "must be"); + InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass); + InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass); + const char* nest_host_error_1 = ref_ik->nest_host_resolution_error(); + const char* nest_host_error_2 = sel_ik->nest_host_resolution_error(); + if (nest_host_error_1 != NULL || nest_host_error_2 != NULL) { + ss.print("("); + if (nest_host_error_1 != NULL) { + ss.print("%s", nest_host_error_1); + if (nest_host_error_2 != NULL) { + ss.print(", %s", nest_host_error_2); + } + } else if (nest_host_error_2 != NULL) { + ss.print("%s", nest_host_error_2); + } + ss.print(")"); + } + } + + Exceptions::fthrow(THREAD_AND_LOCATION, + vmSymbols::java_lang_IllegalAccessError(), + "%s", + ss.as_string() + ); return; } } @@ -915,19 +942,44 @@ if (!can_access) { bool same_module = (sel_klass->module() == ref_klass->module()); ResourceMark rm(THREAD); - Exceptions::fthrow( - THREAD_AND_LOCATION, - vmSymbols::java_lang_IllegalAccessError(), - "class %s tried to access %s%sfield %s.%s (%s%s%s)", - ref_klass->external_name(), - fd.is_protected() ? "protected " : "", - fd.is_private() ? "private " : "", - sel_klass->external_name(), - fd.name()->as_C_string(), - (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(), - (same_module) ? "" : "; ", - (same_module) ? "" : sel_klass->class_in_module_of_loader() - ); + stringStream ss; + ss.print("class %s tried to access %s%sfield %s.%s (%s%s%s)", + ref_klass->external_name(), + fd.is_protected() ? "protected " : "", + fd.is_private() ? "private " : "", + sel_klass->external_name(), + fd.name()->as_C_string(), + (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(), + (same_module) ? "" : "; ", + (same_module) ? "" : sel_klass->class_in_module_of_loader() + ); + // For private access check if there was a problem with nest host + // resolution, and if so report that as part of the message. + if (fd.is_private()) { + assert(ref_klass->is_instance_klass(), "must be"); + assert(sel_klass->is_instance_klass(), "must be"); + InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass); + InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass); + const char* nest_host_error_1 = ref_ik->nest_host_resolution_error(); + const char* nest_host_error_2 = sel_ik->nest_host_resolution_error(); + if (nest_host_error_1 != NULL || nest_host_error_2 != NULL) { + ss.print("("); + if (nest_host_error_1 != NULL) { + ss.print("%s", nest_host_error_1); + if (nest_host_error_2 != NULL) { + ss.print(", %s", nest_host_error_2); + } + } else if (nest_host_error_2 != NULL) { + ss.print("%s", nest_host_error_2); + } + ss.print(")"); + } + } + Exceptions::fthrow(THREAD_AND_LOCATION, + vmSymbols::java_lang_IllegalAccessError(), + "%s", + ss.as_string() + ); return; } }