mirror of
https://github.com/openjdk/jdk.git
synced 2026-05-11 05:59:52 +00:00
8165550: Add class loader names to ClassCastException message
Adds new format of class loader names to VM ClassCastException messages Reviewed-by: dholmes, mchung, lfoltan
This commit is contained in:
parent
81f1e7056b
commit
3f2be39bb3
@ -54,6 +54,17 @@ void ModuleEntry::set_location(Symbol* location) {
|
||||
}
|
||||
}
|
||||
|
||||
bool ModuleEntry::is_non_jdk_module() {
|
||||
ResourceMark rm;
|
||||
if (location() != NULL) {
|
||||
const char* loc = location()->as_C_string();
|
||||
if (strncmp(loc, "jrt:/java.", 10) != 0 && strncmp(loc, "jrt:/jdk.", 9) != 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ModuleEntry::set_version(Symbol* version) {
|
||||
if (_version != NULL) {
|
||||
// _version symbol's refcounts are managed by ModuleEntry,
|
||||
|
||||
@ -36,6 +36,7 @@
|
||||
#include "utilities/ostream.hpp"
|
||||
|
||||
#define UNNAMED_MODULE "Unnamed Module"
|
||||
#define JAVA_BASE_NAME "java.base"
|
||||
|
||||
class ModuleClosure;
|
||||
|
||||
@ -100,6 +101,7 @@ public:
|
||||
|
||||
Symbol* location() const { return _location; }
|
||||
void set_location(Symbol* location);
|
||||
bool is_non_jdk_module();
|
||||
|
||||
bool can_read(ModuleEntry* m) const;
|
||||
bool has_reads() const;
|
||||
|
||||
@ -2897,11 +2897,11 @@ void SystemDictionary::verify() {
|
||||
// caller needs ResourceMark
|
||||
const char* SystemDictionary::loader_name(const oop loader) {
|
||||
return ((loader) == NULL ? "<bootloader>" :
|
||||
InstanceKlass::cast((loader)->klass())->name()->as_C_string());
|
||||
InstanceKlass::cast((loader)->klass())->name()->as_C_string());
|
||||
}
|
||||
|
||||
// caller needs ResourceMark
|
||||
const char* SystemDictionary::loader_name(const ClassLoaderData* loader_data) {
|
||||
return (loader_data->class_loader() == NULL ? "<bootloader>" :
|
||||
InstanceKlass::cast((loader_data->class_loader())->klass())->name()->as_C_string());
|
||||
SystemDictionary::loader_name(loader_data->class_loader()));
|
||||
}
|
||||
|
||||
@ -55,6 +55,7 @@
|
||||
#include "runtime/handles.inline.hpp"
|
||||
#include "runtime/init.hpp"
|
||||
#include "runtime/interfaceSupport.hpp"
|
||||
#include "runtime/java.hpp"
|
||||
#include "runtime/javaCalls.hpp"
|
||||
#include "runtime/sharedRuntime.hpp"
|
||||
#include "runtime/stubRoutines.hpp"
|
||||
@ -1933,44 +1934,103 @@ char* SharedRuntime::generate_class_cast_message(
|
||||
return generate_class_cast_message(caster_klass, target_klass);
|
||||
}
|
||||
|
||||
// The caller of class_loader_and_module_name() (or one of its callers)
|
||||
// must use a ResourceMark in order to correctly free the result.
|
||||
const char* class_loader_and_module_name(Klass* klass) {
|
||||
const char* delim = "/";
|
||||
size_t delim_len = strlen(delim);
|
||||
|
||||
const char* fqn = klass->external_name();
|
||||
// Length of message to return; always include FQN
|
||||
size_t msglen = strlen(fqn) + 1;
|
||||
|
||||
bool has_cl_name = false;
|
||||
bool has_mod_name = false;
|
||||
bool has_version = false;
|
||||
|
||||
// Use class loader name, if exists and not builtin
|
||||
const char* class_loader_name = "";
|
||||
ClassLoaderData* cld = klass->class_loader_data();
|
||||
assert(cld != NULL, "class_loader_data should not be NULL");
|
||||
if (!cld->is_builtin_class_loader_data()) {
|
||||
// If not builtin, look for name
|
||||
oop loader = klass->class_loader();
|
||||
if (loader != NULL) {
|
||||
oop class_loader_name_oop = java_lang_ClassLoader::name(loader);
|
||||
if (class_loader_name_oop != NULL) {
|
||||
class_loader_name = java_lang_String::as_utf8_string(class_loader_name_oop);
|
||||
if (class_loader_name != NULL && class_loader_name[0] != '\0') {
|
||||
has_cl_name = true;
|
||||
msglen += strlen(class_loader_name) + delim_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char* module_name = "";
|
||||
const char* version = "";
|
||||
Klass* bottom_klass = klass->is_objArray_klass() ?
|
||||
ObjArrayKlass::cast(klass)->bottom_klass() : klass;
|
||||
if (bottom_klass->is_instance_klass()) {
|
||||
ModuleEntry* module = InstanceKlass::cast(bottom_klass)->module();
|
||||
// Use module name, if exists
|
||||
if (module->is_named()) {
|
||||
has_mod_name = true;
|
||||
module_name = module->name()->as_C_string();
|
||||
msglen += strlen(module_name);
|
||||
// Use version if exists and is not a jdk module
|
||||
if (module->is_non_jdk_module() && module->version() != NULL) {
|
||||
has_version = true;
|
||||
version = module->version()->as_C_string();
|
||||
msglen += strlen("@") + strlen(version);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// klass is an array of primitives, so its module is java.base
|
||||
module_name = JAVA_BASE_NAME;
|
||||
}
|
||||
|
||||
if (has_cl_name || has_mod_name) {
|
||||
msglen += delim_len;
|
||||
}
|
||||
|
||||
char* message = NEW_RESOURCE_ARRAY_RETURN_NULL(char, msglen);
|
||||
|
||||
// Just return the FQN if error in allocating string
|
||||
if (message == NULL) {
|
||||
return fqn;
|
||||
}
|
||||
|
||||
jio_snprintf(message, msglen, "%s%s%s%s%s%s%s",
|
||||
class_loader_name,
|
||||
(has_cl_name) ? delim : "",
|
||||
(has_mod_name) ? module_name : "",
|
||||
(has_version) ? "@" : "",
|
||||
(has_version) ? version : "",
|
||||
(has_cl_name || has_mod_name) ? delim : "",
|
||||
fqn);
|
||||
return message;
|
||||
}
|
||||
|
||||
char* SharedRuntime::generate_class_cast_message(
|
||||
Klass* caster_klass, Klass* target_klass) {
|
||||
|
||||
const char* caster_klass_name = caster_klass->external_name();
|
||||
Klass* c_klass = caster_klass->is_objArray_klass() ?
|
||||
ObjArrayKlass::cast(caster_klass)->bottom_klass() : caster_klass;
|
||||
ModuleEntry* caster_module;
|
||||
const char* caster_module_name;
|
||||
if (c_klass->is_instance_klass()) {
|
||||
caster_module = InstanceKlass::cast(c_klass)->module();
|
||||
caster_module_name = caster_module->is_named() ?
|
||||
caster_module->name()->as_C_string() : UNNAMED_MODULE;
|
||||
} else {
|
||||
caster_module_name = "java.base";
|
||||
}
|
||||
const char* target_klass_name = target_klass->external_name();
|
||||
Klass* t_klass = target_klass->is_objArray_klass() ?
|
||||
ObjArrayKlass::cast(target_klass)->bottom_klass() : target_klass;
|
||||
ModuleEntry* target_module;
|
||||
const char* target_module_name;
|
||||
if (t_klass->is_instance_klass()) {
|
||||
target_module = InstanceKlass::cast(t_klass)->module();
|
||||
target_module_name = target_module->is_named() ?
|
||||
target_module->name()->as_C_string(): UNNAMED_MODULE;
|
||||
} else {
|
||||
target_module_name = "java.base";
|
||||
}
|
||||
const char* caster_name = class_loader_and_module_name(caster_klass);
|
||||
|
||||
size_t msglen = strlen(caster_klass_name) + strlen(caster_module_name) +
|
||||
strlen(target_klass_name) + strlen(target_module_name) + 50;
|
||||
const char* target_name = class_loader_and_module_name(target_klass);
|
||||
|
||||
char* message = NEW_RESOURCE_ARRAY(char, msglen);
|
||||
if (NULL == message) {
|
||||
size_t msglen = strlen(caster_name) + strlen(" cannot be cast to ") + strlen(target_name) + 1;
|
||||
|
||||
char* message = NEW_RESOURCE_ARRAY_RETURN_NULL(char, msglen);
|
||||
if (message == NULL) {
|
||||
// Shouldn't happen, but don't cause even more problems if it does
|
||||
message = const_cast<char*>(caster_klass_name);
|
||||
message = const_cast<char*>(caster_klass->external_name());
|
||||
} else {
|
||||
jio_snprintf(message, msglen, "%s (in module: %s) cannot be cast to %s (in module: %s)",
|
||||
caster_klass_name, caster_module_name, target_klass_name, target_module_name);
|
||||
jio_snprintf(message,
|
||||
msglen,
|
||||
"%s cannot be cast to %s",
|
||||
caster_name,
|
||||
target_name);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -23,17 +23,39 @@
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @run main/othervm CCE_module_msg
|
||||
* @modules java.base/jdk.internal.misc
|
||||
* @library /test/lib ..
|
||||
* @compile p2/c2.java
|
||||
* @compile p4/c4.java
|
||||
* @build sun.hotspot.WhiteBox
|
||||
* @compile/module=java.base java/lang/reflect/ModuleHelper.java
|
||||
* @run main ClassFileInstaller sun.hotspot.WhiteBox
|
||||
* sun.hotspot.WhiteBox$WhiteBoxPermission
|
||||
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI CCE_module_msg
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Module;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import static jdk.test.lib.Asserts.*;
|
||||
|
||||
// Test that the message in a runtime ClassCastException contains module info.
|
||||
public class CCE_module_msg {
|
||||
private static final Path CLASSES_DIR = Paths.get("classes");
|
||||
|
||||
public static void main(String[] args) {
|
||||
invalidCastTest();
|
||||
public static void main(String[] args) throws Throwable {
|
||||
// Should not display version
|
||||
invalidObjectToDerived();
|
||||
// Should display version
|
||||
invalidClassToString();
|
||||
// Should display customer class loader
|
||||
invalidClassToStringCustomLoader();
|
||||
}
|
||||
|
||||
public static void invalidCastTest() {
|
||||
public static void invalidObjectToDerived() {
|
||||
java.lang.Object instance = new java.lang.Object();
|
||||
int left = 23;
|
||||
int right = 42;
|
||||
@ -44,11 +66,69 @@ public class CCE_module_msg {
|
||||
throw new RuntimeException("ClassCastException wasn't thrown, test failed.");
|
||||
} catch (ClassCastException cce) {
|
||||
System.out.println(cce.getMessage());
|
||||
if (!cce.getMessage().contains("java.lang.Object (in module: java.base) cannot be cast")) {
|
||||
if (!cce.getMessage().contains("java.base/java.lang.Object cannot be cast to Derived")) {
|
||||
throw new RuntimeException("Wrong message: " + cce.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void invalidClassToString() throws Throwable {
|
||||
// Get the java.lang.reflect.Module object for module java.base.
|
||||
Class jlObject = Class.forName("java.lang.Object");
|
||||
Object jlObject_jlrM = jlObject.getModule();
|
||||
assertNotNull(jlObject_jlrM, "jlrModule object of java.lang.Object should not be null");
|
||||
|
||||
// Get the class loader for CCE_module_msg and assume it's also used to
|
||||
// load classes p1.c1 and p2.c2.
|
||||
ClassLoader this_cldr = CCE_module_msg.class.getClassLoader();
|
||||
|
||||
// Define a module for p2.
|
||||
Object m2 = ModuleHelper.ModuleObject("module2", this_cldr, new String[] { "p2" });
|
||||
assertNotNull(m2, "Module should not be null");
|
||||
ModuleHelper.DefineModule(m2, "9.0", "m2/there", new String[] { "p2" });
|
||||
ModuleHelper.AddReadsModule(m2, jlObject_jlrM);
|
||||
|
||||
try {
|
||||
ModuleHelper.AddModuleExportsToAll(m2, "p2");
|
||||
Object p2Obj = new p2.c2();
|
||||
System.out.println((String)p2Obj);
|
||||
throw new RuntimeException("ClassCastException wasn't thrown, test failed.");
|
||||
} catch (ClassCastException cce) {
|
||||
String exception = cce.getMessage();
|
||||
System.out.println(exception);
|
||||
if (exception.contains("module2/p2.c2") ||
|
||||
!(exception.contains("module2@") &&
|
||||
exception.contains("/p2.c2 cannot be cast to java.base/java.lang.String"))) {
|
||||
throw new RuntimeException("Wrong message: " + exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void invalidClassToStringCustomLoader() throws Throwable {
|
||||
// Get the java.lang.reflect.Module object for module java.base.
|
||||
Class jlObject = Class.forName("java.lang.Object");
|
||||
Object jlObject_jlrM = jlObject.getModule();
|
||||
assertNotNull(jlObject_jlrM, "jlrModule object of java.lang.Object should not be null");
|
||||
|
||||
// Create a customer class loader to load class p4/c4.
|
||||
URL[] urls = new URL[] { CLASSES_DIR.toUri().toURL() };
|
||||
ClassLoader parent = ClassLoader.getSystemClassLoader();
|
||||
MyURLClassLoader myCldr = new MyURLClassLoader("MyClassLoader", urls, parent);
|
||||
|
||||
try {
|
||||
// Class p4.c4 should be defined to the unnamed module of myCldr
|
||||
Class p4_c4_class = myCldr.loadClass("p4.c4");
|
||||
Object c4Obj = p4_c4_class.newInstance();
|
||||
System.out.println((String)c4Obj);
|
||||
throw new RuntimeException("ClassCastException wasn't thrown, test failed.");
|
||||
} catch (ClassCastException cce) {
|
||||
String exception = cce.getMessage();
|
||||
System.out.println(exception);
|
||||
if (!exception.contains("MyClassLoader//p4.c4 cannot be cast to java.base/java.lang.String")) {
|
||||
throw new RuntimeException("Wrong message: " + exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Derived extends java.lang.Object {
|
||||
@ -56,3 +136,35 @@ class Derived extends java.lang.Object {
|
||||
return right;
|
||||
}
|
||||
}
|
||||
|
||||
class MyURLClassLoader extends URLClassLoader {
|
||||
public MyURLClassLoader(String name,
|
||||
URL[] urls,
|
||||
ClassLoader parent) {
|
||||
super(name, urls, parent);
|
||||
}
|
||||
|
||||
public Class loadClass(String name) throws ClassNotFoundException {
|
||||
if (!name.equals("p4.c4")) {
|
||||
return super.loadClass(name);
|
||||
}
|
||||
byte[] data = getClassData(name);
|
||||
return defineClass(name, data, 0, data.length);
|
||||
}
|
||||
|
||||
byte[] getClassData(String name) {
|
||||
try {
|
||||
String TempName = name.replaceAll("\\.", "/");
|
||||
String currentDir = System.getProperty("test.classes");
|
||||
String filename = currentDir + File.separator + TempName + ".class";
|
||||
FileInputStream fis = new FileInputStream(filename);
|
||||
byte[] b = new byte[5000];
|
||||
int cnt = fis.read(b, 0, 5000);
|
||||
byte[] c = new byte[cnt];
|
||||
for (int i=0; i<cnt; i++) c[i] = b[i];
|
||||
return c;
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
29
hotspot/test/runtime/modules/p4/c4.java
Normal file
29
hotspot/test/runtime/modules/p4/c4.java
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
// Small class used by multiple hotspot/runtime/modules/AccessCheck* tests.
|
||||
package p4;
|
||||
|
||||
public class c4 {
|
||||
public void method4() { }
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user