aggressive treeshaker: fixing ObjectOutputStream.writeObject()
27 Aug 2018 | tree shaker enhancementAggressive tree shaker helps to reduce application footprint by removing all methods that are not referenced in code. This often breaks reflection-based code and required classes has to be explicitly specified using <forceLinkClasses>
but sometimes it is not enough. While working with nitrite-database I faced case when class is referenced but some methods of it is still removed. Here is an example:
private void testSerialize() {
try {
new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(new HashMap<>());
} catch (IOException e) {
e.printStackTrace();
}
}
fails with:
java.io.InvalidClassException: java.util.HashMap doesn't have a field loadFactor of type float
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:955)
This happens due internal
private and unreferenced methods got removed from HashMap
.
java.io.Serializable contract
As specified in documentation there is special case: