From 266e8f7249e0d1182b5e50e355bf4ac1840d91e4 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Thu, 31 Dec 2020 13:23:46 +0100 Subject: [PATCH] Platform: Avoid SerializedForm reference Prior to this commit each `TestIdentifier` instance required memory for storing a `null` reference. --- .../platform/launcher/TestIdentifier.java | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/TestIdentifier.java b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/TestIdentifier.java index 679c7966d7b7..91ddb288439e 100644 --- a/junit-platform-launcher/src/main/java/org/junit/platform/launcher/TestIdentifier.java +++ b/junit-platform-launcher/src/main/java/org/junit/platform/launcher/TestIdentifier.java @@ -51,16 +51,14 @@ public final class TestIdentifier implements Serializable { private static final ObjectStreamField[] serialPersistentFields = ObjectStreamClass.lookup( SerializedForm.class).getFields(); - // Only set during deserialization process - private SerializedForm serializedForm; - - private final UniqueId uniqueId; - private final UniqueId parentId; - private final String displayName; - private final String legacyReportingName; - private final TestSource source; - private final Set tags; - private final Type type; + // These are effectively final but not technically due to late initialization when deserializing + private /* final */ UniqueId uniqueId; + private /* final */ UniqueId parentId; + private /* final */ String displayName; + private /* final */ String legacyReportingName; + private /* final */ TestSource source; + private /* final */ Set tags; + private /* final */ Type type; /** * Factory for creating a new {@link TestIdentifier} from a {@link TestDescriptor}. @@ -78,12 +76,6 @@ public static TestIdentifier from(TestDescriptor testDescriptor) { return new TestIdentifier(uniqueId, displayName, source, tags, type, parentId, legacyReportingName); } - private TestIdentifier(SerializedForm serializedForm) { - this(UniqueId.parse(serializedForm.uniqueId), serializedForm.displayName, serializedForm.source, - serializedForm.tags, serializedForm.type, UniqueId.parse(serializedForm.parentId), - serializedForm.legacyReportingName); - } - private TestIdentifier(UniqueId uniqueId, String displayName, TestSource source, Set tags, Type type, UniqueId parentId, String legacyReportingName) { Preconditions.notNull(type, "TestDescriptor.Type must not be null"); @@ -286,11 +278,14 @@ private void writeObject(ObjectOutputStream s) throws IOException { } private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { - serializedForm = SerializedForm.deserialize(s); - } - - private Object readResolve() { - return new TestIdentifier(serializedForm); + SerializedForm serializedForm = SerializedForm.deserialize(s); + uniqueId = UniqueId.parse(serializedForm.uniqueId); + displayName = serializedForm.displayName; + source = serializedForm.source; + tags = serializedForm.tags; + type = serializedForm.type; + parentId = UniqueId.parse(serializedForm.parentId); + legacyReportingName = serializedForm.legacyReportingName; } /**