Skip to content

Commit

Permalink
Refactor serialization adapter retrieval by version (#1066)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nimakarimipour authored Nov 5, 2024
1 parent e9541e3 commit 17fc1ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
+ ".");
}
}
}

0 comments on commit 17fc1ba

Please sign in to comment.