From 17fc1ba5cfc84fb3fffba038f393808fad0daaab Mon Sep 17 00:00:00 2001 From: Nima Karimipour Date: Mon, 4 Nov 2024 20:41:28 -0800 Subject: [PATCH] Refactor serialization adapter retrieval by version (#1066) This PR prepares for #1063 by moving the `initializeAdapter` declaration from `FixSerializationConfig` to `SerializationVersionAdapter`. This change ensures that version-specific serializations are handled directly by `SerializationVersionAdapter`, which will simplify test modifications in #1063. This transition also aligns with a clearer design, as `SerializationVersionAdapter` should be responsible for computing the appropriate adapter for each new version. This approach will help us maintain version updates within `SerializationVersionAdapter` while keeping `FixSerializationConfig` unchanged. I'm uncertain if maintaining backward compatibility is necessary in the long term. If it's not needed, we can consider removing it in the future. For now, however, this PR is required to keep #1063 concise. --- .../FixSerializationConfig.java | 31 +++---------------- .../adapters/SerializationAdapter.java | 25 +++++++++++++++ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/FixSerializationConfig.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/FixSerializationConfig.java index cc5b446324..cb9d71f919 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/FixSerializationConfig.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/FixSerializationConfig.java @@ -24,8 +24,6 @@ import com.google.common.base.Preconditions; import com.uber.nullaway.fixserialization.adapters.SerializationAdapter; -import com.uber.nullaway.fixserialization.adapters.SerializationV1Adapter; -import com.uber.nullaway.fixserialization.adapters.SerializationV3Adapter; import com.uber.nullaway.fixserialization.out.SuggestedNullableFixInfo; import java.io.IOException; import java.nio.file.Files; @@ -86,7 +84,9 @@ public FixSerializationConfig( this.suggestEnclosing = suggestEnclosing; this.fieldInitInfoEnabled = fieldInitInfoEnabled; this.outputDirectory = outputDirectory; - serializer = new Serializer(this, initializeAdapter(SerializationAdapter.LATEST_VERSION)); + serializer = + new Serializer( + this, SerializationAdapter.getAdapterForVersion(SerializationAdapter.LATEST_VERSION)); } /** @@ -126,32 +126,11 @@ public FixSerializationConfig(String configFilePath, int serializationVersion) { XMLUtil.getValueFromAttribute( document, "/serialization/fieldInitInfo", "active", Boolean.class) .orElse(false); - SerializationAdapter serializationAdapter = initializeAdapter(serializationVersion); + SerializationAdapter serializationAdapter = + SerializationAdapter.getAdapterForVersion(serializationVersion); serializer = new Serializer(this, serializationAdapter); } - /** - * Initializes NullAway serialization adapter according to the requested serialization version. - */ - private SerializationAdapter initializeAdapter(int version) { - switch (version) { - case 1: - return new SerializationV1Adapter(); - case 2: - throw new RuntimeException( - "Serialization version v2 is skipped and was used for an alpha version of the auto-annotator tool. Please use version 3 instead."); - case 3: - return new SerializationV3Adapter(); - default: - throw new RuntimeException( - "Unrecognized NullAway serialization version: " - + version - + ". Supported versions: 1 to " - + SerializationAdapter.LATEST_VERSION - + "."); - } - } - public @Nullable Serializer getSerializer() { return serializer; } diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationAdapter.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationAdapter.java index 2f3bc5fc84..ce7e802fdb 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationAdapter.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationAdapter.java @@ -71,4 +71,29 @@ public interface SerializationAdapter { * @return The serialized method symbol. */ String serializeMethodSignature(Symbol.MethodSymbol methodSymbol); + + /** + * Gets the adapter for the given version number. + * + * @param version Version number of the adapter. + * @return Adapter for the given version. + */ + static SerializationAdapter getAdapterForVersion(int version) { + switch (version) { + case 1: + return new SerializationV1Adapter(); + case 2: + throw new RuntimeException( + "Serialization version v2 is skipped and was used for an alpha version of the auto-annotator tool. Please use version 3 instead."); + case 3: + return new SerializationV3Adapter(); + default: + throw new RuntimeException( + "Unrecognized NullAway serialization version: " + + version + + ". Supported versions: 1 to " + + SerializationAdapter.LATEST_VERSION + + "."); + } + } }