Skip to content

Commit

Permalink
Refactoring tabSeparatedToString logic to prepare for serialization v…
Browse files Browse the repository at this point in the history
…ersion 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.
  • Loading branch information
nimakarimipour authored Feb 22, 2023
1 parent 1548c69 commit ff6b090
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public void serializeSuggestedFixInfo(
if (enclosing) {
suggestedNullableFixInfo.initEnclosing();
}
appendToFile(suggestedNullableFixInfo.tabSeparatedToString(), suggestedFixesOutputPath);
appendToFile(
suggestedNullableFixInfo.tabSeparatedToString(serializationAdapter),
suggestedFixesOutputPath);
}

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

package com.uber.nullaway.fixserialization.adapters;

import com.sun.tools.javac.code.Symbol;
import com.uber.nullaway.fixserialization.out.ErrorInfo;

/**
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,16 +55,21 @@ 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));
}

@Override
public int getSerializationVersion() {
return 1;
}

@Override
public String serializeMethodSignature(Symbol.MethodSymbol methodSymbol) {
return methodSymbol.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,18 +68,23 @@ 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));
}

@Override
public int getSerializationVersion() {
return 2;
}

@Override
public String serializeMethodSignature(Symbol.MethodSymbol methodSymbol) {
return methodSymbol.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}

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

Expand Down Expand Up @@ -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. */
Expand Down

0 comments on commit ff6b090

Please sign in to comment.