Skip to content

Commit

Permalink
#849 Fall back to getDeclaredConstructor for non-public records
Browse files Browse the repository at this point in the history
  • Loading branch information
theigl committed Jul 30, 2021
1 parent 7ebe029 commit a295f19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Expand Up @@ -220,7 +220,13 @@ private static <T> T invokeCanonicalConstructor (Class<T> recordType,
Class<?>[] paramTypes = Arrays.stream(recordComponents)
.map(RecordComponent::type)
.toArray(Class<?>[]::new);
Constructor<T> canonicalConstructor = recordType.getConstructor(paramTypes);
Constructor<T> canonicalConstructor;
try {
canonicalConstructor = recordType.getConstructor(paramTypes);
} catch (NoSuchMethodException e) {
canonicalConstructor = recordType.getDeclaredConstructor(paramTypes);
canonicalConstructor.setAccessible(true);
}
return canonicalConstructor.newInstance(args);
} catch (Throwable t) {
KryoException ex = new KryoException(t);
Expand Down
Expand Up @@ -317,5 +317,16 @@ void testRecordWithSuperType() {

roundTrip(3, r2);
}

static record PackagePrivateRecord(int i, String s) {}
private static record PrivateRecord(String s, int i) {}

@Test
void testNonPublicRecords() {
kryo.register(PackagePrivateRecord.class);
kryo.register(PrivateRecord.class);

roundTrip(4, new PackagePrivateRecord(1, "s1"));
roundTrip(4, new PrivateRecord("s2",2));
}
}

0 comments on commit a295f19

Please sign in to comment.