From ff6b0907601156d42ae88c77ddc6c4331c8e5744 Mon Sep 17 00:00:00 2001 From: Nima Karimipour Date: Wed, 22 Feb 2023 11:01:18 -0800 Subject: [PATCH] Refactoring tabSeparatedToString logic to prepare for serialization version 3. (#738) This PR is merely refactoring which prepares serialization logic for version `v3` implemented in #735. Changes in this PR - Uses adapter to serialize method signatures. - Adds `SerializationAdapter` to `tabSeparatedToString` methods for symbol serialization. - Adds `SerializationAdapter` to `serializeSymbol` static method for symbol serialization. --- .../uber/nullaway/fixserialization/Serializer.java | 11 +++++++---- .../adapters/SerializationAdapter.java | 9 +++++++++ .../adapters/SerializationV1Adapter.java | 12 +++++++++--- .../adapters/SerializationV2Adapter.java | 12 +++++++++--- .../fixserialization/location/FieldLocation.java | 7 ++++--- .../fixserialization/location/MethodLocation.java | 7 ++++--- .../location/MethodParameterLocation.java | 9 +++++---- .../fixserialization/location/SymbolLocation.java | 4 +++- .../out/FieldInitializationInfo.java | 8 +++++--- .../out/SuggestedNullableFixInfo.java | 10 ++++++---- 10 files changed, 61 insertions(+), 28 deletions(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/Serializer.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/Serializer.java index 87154d0a39..db6d8c7250 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/Serializer.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/Serializer.java @@ -77,7 +77,9 @@ public void serializeSuggestedFixInfo( if (enclosing) { suggestedNullableFixInfo.initEnclosing(); } - appendToFile(suggestedNullableFixInfo.tabSeparatedToString(), suggestedFixesOutputPath); + appendToFile( + suggestedNullableFixInfo.tabSeparatedToString(serializationAdapter), + suggestedFixesOutputPath); } /** @@ -91,7 +93,7 @@ public void serializeErrorInfo(ErrorInfo errorInfo) { } public void serializeFieldInitializationInfo(FieldInitializationInfo info) { - appendToFile(info.tabSeparatedToString(), fieldInitializationOutputPath); + appendToFile(info.tabSeparatedToString(serializationAdapter), fieldInitializationOutputPath); } /** Cleared the content of the file if exists and writes the header in the first line. */ @@ -201,9 +203,10 @@ public static Path pathToSourceFileFromURI(@Nullable URI uri) { * Serializes the given {@link Symbol} to a string. * * @param symbol The symbol to serialize. + * @param adapter adapter used to serialize symbols. * @return The serialized symbol. */ - public static String serializeSymbol(@Nullable Symbol symbol) { + public static String serializeSymbol(@Nullable Symbol symbol, SerializationAdapter adapter) { if (symbol == null) { return "null"; } @@ -213,7 +216,7 @@ public static String serializeSymbol(@Nullable Symbol symbol) { return symbol.name.toString(); case METHOD: case CONSTRUCTOR: - return symbol.toString(); + return adapter.serializeMethodSignature((Symbol.MethodSymbol) symbol); default: return symbol.flatName().toString(); } 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 0120ef785f..c7ef01bc0b 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 @@ -22,6 +22,7 @@ package com.uber.nullaway.fixserialization.adapters; +import com.sun.tools.javac.code.Symbol; import com.uber.nullaway.fixserialization.out.ErrorInfo; /** @@ -62,4 +63,12 @@ public interface SerializationAdapter { * @return Supporting serialization version number. */ int getSerializationVersion(); + + /** + * Serializes the signature of the given {@link Symbol.MethodSymbol} to a string. + * + * @param methodSymbol The method symbol to serialize. + * @return The serialized method symbol. + */ + String serializeMethodSignature(Symbol.MethodSymbol methodSymbol); } diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationV1Adapter.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationV1Adapter.java index d622cec71e..b0ad0cc9c6 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationV1Adapter.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationV1Adapter.java @@ -24,6 +24,7 @@ import static com.uber.nullaway.fixserialization.out.ErrorInfo.EMPTY_NONNULL_TARGET_LOCATION_STRING; +import com.sun.tools.javac.code.Symbol; import com.uber.nullaway.fixserialization.SerializationService; import com.uber.nullaway.fixserialization.Serializer; import com.uber.nullaway.fixserialization.location.SymbolLocation; @@ -54,11 +55,11 @@ public String serializeError(ErrorInfo errorInfo) { "\t", errorInfo.getErrorMessage().getMessageType().toString(), SerializationService.escapeSpecialCharacters(errorInfo.getErrorMessage().getMessage()), - Serializer.serializeSymbol(errorInfo.getRegionClass()), - Serializer.serializeSymbol(errorInfo.getRegionMember()), + Serializer.serializeSymbol(errorInfo.getRegionClass(), this), + Serializer.serializeSymbol(errorInfo.getRegionMember(), this), (errorInfo.getNonnullTarget() != null ? SymbolLocation.createLocationFromSymbol(errorInfo.getNonnullTarget()) - .tabSeparatedToString() + .tabSeparatedToString(this) : EMPTY_NONNULL_TARGET_LOCATION_STRING)); } @@ -66,4 +67,9 @@ public String serializeError(ErrorInfo errorInfo) { public int getSerializationVersion() { return 1; } + + @Override + public String serializeMethodSignature(Symbol.MethodSymbol methodSymbol) { + return methodSymbol.toString(); + } } diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationV2Adapter.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationV2Adapter.java index eda75d6d03..6b72441852 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationV2Adapter.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/adapters/SerializationV2Adapter.java @@ -24,6 +24,7 @@ import static com.uber.nullaway.fixserialization.out.ErrorInfo.EMPTY_NONNULL_TARGET_LOCATION_STRING; +import com.sun.tools.javac.code.Symbol; import com.uber.nullaway.fixserialization.SerializationService; import com.uber.nullaway.fixserialization.Serializer; import com.uber.nullaway.fixserialization.location.SymbolLocation; @@ -67,13 +68,13 @@ public String serializeError(ErrorInfo errorInfo) { "\t", errorInfo.getErrorMessage().getMessageType().toString(), SerializationService.escapeSpecialCharacters(errorInfo.getErrorMessage().getMessage()), - Serializer.serializeSymbol(errorInfo.getRegionClass()), - Serializer.serializeSymbol(errorInfo.getRegionMember()), + Serializer.serializeSymbol(errorInfo.getRegionClass(), this), + Serializer.serializeSymbol(errorInfo.getRegionMember(), this), String.valueOf(errorInfo.getOffset()), errorInfo.getPath() != null ? errorInfo.getPath().toString() : "null", (errorInfo.getNonnullTarget() != null ? SymbolLocation.createLocationFromSymbol(errorInfo.getNonnullTarget()) - .tabSeparatedToString() + .tabSeparatedToString(this) : EMPTY_NONNULL_TARGET_LOCATION_STRING)); } @@ -81,4 +82,9 @@ public String serializeError(ErrorInfo errorInfo) { public int getSerializationVersion() { return 2; } + + @Override + public String serializeMethodSignature(Symbol.MethodSymbol methodSymbol) { + return methodSymbol.toString(); + } } diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/FieldLocation.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/FieldLocation.java index 71c8989f99..19ca23dae1 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/FieldLocation.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/FieldLocation.java @@ -24,6 +24,7 @@ import com.sun.tools.javac.code.Symbol; import com.uber.nullaway.fixserialization.Serializer; +import com.uber.nullaway.fixserialization.adapters.SerializationAdapter; import javax.lang.model.element.ElementKind; /** subtype of {@link AbstractSymbolLocation} targeting class fields. */ @@ -38,13 +39,13 @@ public FieldLocation(Symbol target) { } @Override - public String tabSeparatedToString() { + public String tabSeparatedToString(SerializationAdapter adapter) { return String.join( "\t", type.toString(), - Serializer.serializeSymbol(enclosingClass), + Serializer.serializeSymbol(enclosingClass, adapter), "null", - Serializer.serializeSymbol(variableSymbol), + Serializer.serializeSymbol(variableSymbol, adapter), "null", path != null ? path.toString() : "null"); } diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/MethodLocation.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/MethodLocation.java index c53075d904..f3c3022842 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/MethodLocation.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/MethodLocation.java @@ -24,6 +24,7 @@ import com.sun.tools.javac.code.Symbol; import com.uber.nullaway.fixserialization.Serializer; +import com.uber.nullaway.fixserialization.adapters.SerializationAdapter; import javax.lang.model.element.ElementKind; /** subtype of {@link AbstractSymbolLocation} targeting methods. */ @@ -38,12 +39,12 @@ public MethodLocation(Symbol target) { } @Override - public String tabSeparatedToString() { + public String tabSeparatedToString(SerializationAdapter adapter) { return String.join( "\t", type.toString(), - Serializer.serializeSymbol(enclosingClass), - Serializer.serializeSymbol(enclosingMethod), + Serializer.serializeSymbol(enclosingClass, adapter), + Serializer.serializeSymbol(enclosingMethod, adapter), "null", "null", path != null ? path.toString() : "null"); diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/MethodParameterLocation.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/MethodParameterLocation.java index 622d3109b2..ff4f0a542e 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/MethodParameterLocation.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/MethodParameterLocation.java @@ -25,6 +25,7 @@ import com.google.common.base.Preconditions; import com.sun.tools.javac.code.Symbol; import com.uber.nullaway.fixserialization.Serializer; +import com.uber.nullaway.fixserialization.adapters.SerializationAdapter; import javax.lang.model.element.ElementKind; /** subtype of {@link AbstractSymbolLocation} targeting a method parameter. */ @@ -59,13 +60,13 @@ public MethodParameterLocation(Symbol target) { } @Override - public String tabSeparatedToString() { + public String tabSeparatedToString(SerializationAdapter adapter) { return String.join( "\t", type.toString(), - Serializer.serializeSymbol(enclosingClass), - Serializer.serializeSymbol(enclosingMethod), - Serializer.serializeSymbol(paramSymbol), + Serializer.serializeSymbol(enclosingClass, adapter), + Serializer.serializeSymbol(enclosingMethod, adapter), + Serializer.serializeSymbol(paramSymbol, adapter), String.valueOf(index), path != null ? path.toString() : "null"); } diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/SymbolLocation.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/SymbolLocation.java index 5824866b2e..262df711e1 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/SymbolLocation.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/location/SymbolLocation.java @@ -23,6 +23,7 @@ package com.uber.nullaway.fixserialization.location; import com.sun.tools.javac.code.Symbol; +import com.uber.nullaway.fixserialization.adapters.SerializationAdapter; /** Provides method for symbol locations. */ public interface SymbolLocation { @@ -32,9 +33,10 @@ public interface SymbolLocation { * of the element, symbol of the containing class, symbol of the enclosing method, symbol of the * variable, index of the element and uri to containing file. * + * @param adapter adapter used to serialize symbols. * @return string representation of contents in a line seperated by tabs. */ - String tabSeparatedToString(); + String tabSeparatedToString(SerializationAdapter adapter); /** * Creates header of an output file containing all {@link SymbolLocation} written in string which diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/out/FieldInitializationInfo.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/out/FieldInitializationInfo.java index b5caca2697..dac0c6d117 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/out/FieldInitializationInfo.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/out/FieldInitializationInfo.java @@ -24,6 +24,7 @@ import com.sun.tools.javac.code.Symbol; import com.uber.nullaway.fixserialization.Serializer; +import com.uber.nullaway.fixserialization.adapters.SerializationAdapter; import com.uber.nullaway.fixserialization.location.SymbolLocation; /** @@ -45,12 +46,13 @@ public FieldInitializationInfo(Symbol.MethodSymbol initializerMethod, Symbol fie /** * Returns string representation of content of an object. * + * @param adapter adapter used to serialize symbols. * @return string representation of contents of an object in a line seperated by tabs. */ - public String tabSeparatedToString() { - return initializerMethodLocation.tabSeparatedToString() + public String tabSeparatedToString(SerializationAdapter adapter) { + return initializerMethodLocation.tabSeparatedToString(adapter) + '\t' - + Serializer.serializeSymbol(field); + + Serializer.serializeSymbol(field, adapter); } /** diff --git a/nullaway/src/main/java/com/uber/nullaway/fixserialization/out/SuggestedNullableFixInfo.java b/nullaway/src/main/java/com/uber/nullaway/fixserialization/out/SuggestedNullableFixInfo.java index 68377a818d..fb271cd644 100644 --- a/nullaway/src/main/java/com/uber/nullaway/fixserialization/out/SuggestedNullableFixInfo.java +++ b/nullaway/src/main/java/com/uber/nullaway/fixserialization/out/SuggestedNullableFixInfo.java @@ -25,6 +25,7 @@ import com.sun.source.util.TreePath; import com.uber.nullaway.ErrorMessage; import com.uber.nullaway.fixserialization.Serializer; +import com.uber.nullaway.fixserialization.adapters.SerializationAdapter; import com.uber.nullaway.fixserialization.location.SymbolLocation; import java.util.Objects; @@ -68,16 +69,17 @@ public int hashCode() { /** * returns string representation of content of an object. * + * @param adapter adapter used to serialize symbols. * @return string representation of contents of an object in a line separated by tabs. */ - public String tabSeparatedToString() { + public String tabSeparatedToString(SerializationAdapter adapter) { return String.join( "\t", - symbolLocation.tabSeparatedToString(), + symbolLocation.tabSeparatedToString(adapter), errorMessage.getMessageType().toString(), "nullable", - Serializer.serializeSymbol(classAndMemberInfo.getClazz()), - Serializer.serializeSymbol(classAndMemberInfo.getMember())); + Serializer.serializeSymbol(classAndMemberInfo.getClazz(), adapter), + Serializer.serializeSymbol(classAndMemberInfo.getMember(), adapter)); } /** Finds the class and member of program point where triggered this type change. */