Skip to content

Commit

Permalink
EsotericSoftware#778 Use canonical class name for registration hint i…
Browse files Browse the repository at this point in the history
…f available
  • Loading branch information
theigl committed Oct 22, 2020
1 parent bd95a9e commit 6a2b16e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/com/esotericsoftware/kryo/Kryo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down
8 changes: 8 additions & 0 deletions src/com/esotericsoftware/kryo/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion test/com/esotericsoftware/kryo/WarnUnregisteredClassesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void testLogShouldBeWarn () {
}

@Test
public void testLogMessageShouldContainsClassName () {
public void testLogMessageShouldContainClassName () {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);
kryo.setWarnUnregisteredClasses(true);
Expand All @@ -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);
Expand Down

0 comments on commit 6a2b16e

Please sign in to comment.