diff --git a/src/com/esotericsoftware/kryo/Kryo.java b/src/com/esotericsoftware/kryo/Kryo.java index ea35fe755..d4fe9ebbe 100644 --- a/src/com/esotericsoftware/kryo/Kryo.java +++ b/src/com/esotericsoftware/kryo/Kryo.java @@ -519,7 +519,7 @@ else if (isClosure(type)) // protected String unregisteredClassMessage (Class type) { return "Class is not registered: " + className(type) + "\nNote: To register this class use: kryo.register(" - + className(type) + ".class);"; + + canonicalName(type) + ".class);"; } /** @see ClassResolver#getRegistration(int) */ diff --git a/src/com/esotericsoftware/kryo/util/Util.java b/src/com/esotericsoftware/kryo/util/Util.java index c0f8a676b..37e95cf23 100644 --- a/src/com/esotericsoftware/kryo/util/Util.java +++ b/src/com/esotericsoftware/kryo/util/Util.java @@ -179,6 +179,14 @@ public static String classNames (Class[] types) { return buffer.toString(); } + /** Returns the class formatted as a string. If the class has a canonical name, the canonical name is returned, + * otherwise it returns the result of {@link #className(Class)} */ + public static String canonicalName(Class type) { + if (type == null) return "null"; + final String canonicalName = type.getCanonicalName(); + return canonicalName != null ? canonicalName : className(type); + } + public static String simpleName (Type type) { if (type instanceof Class) return ((Class)type).getSimpleName(); return type.toString(); // Java 8: getTypeName diff --git a/test/com/esotericsoftware/kryo/WarnUnregisteredClassesTest.java b/test/com/esotericsoftware/kryo/WarnUnregisteredClassesTest.java index 2352032e5..c8df3003d 100644 --- a/test/com/esotericsoftware/kryo/WarnUnregisteredClassesTest.java +++ b/test/com/esotericsoftware/kryo/WarnUnregisteredClassesTest.java @@ -97,7 +97,7 @@ public void testLogShouldBeWarn () { } @Test - public void testLogMessageShouldContainsClassName () { + public void testLogMessageShouldContainClassName () { Kryo kryo = new Kryo(); kryo.setRegistrationRequired(false); kryo.setWarnUnregisteredClasses(true); @@ -106,6 +106,18 @@ public void testLogMessageShouldContainsClassName () { assertTrue(log.messages.get(0).contains(UnregisteredClass.class.getName())); } + @Test + public void testLogMessageShouldContainRegistrationHintWithCanonicalName () { + Kryo kryo = new Kryo(); + kryo.setRegistrationRequired(false); + kryo.setWarnUnregisteredClasses(true); + + write(kryo, new UnregisteredClass()); + assertTrue(log.messages.get(0).contains( + "kryo.register(com.esotericsoftware.kryo.WarnUnregisteredClassesTest.UnregisteredClass.class)" + )); + } + public void write (Kryo kryo, Object object) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Output output = new Output(outStream, 4096);