From a7ba27e1d2606bd007bea02b06eb733e284c3ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1zaro=20Clapp?= Date: Tue, 13 Sep 2022 14:30:39 -0400 Subject: [PATCH] Make AbstractConfig collection fields explicity Immutable (#601) Follow up to #600. These values are already `ImmutableSet` at runtime (except one which was still used strictly as read-only). This change just makes it explicit. --- .../com/uber/nullaway/AbstractConfig.java | 50 ++++++++----------- .../nullaway/ErrorProneCLIFlagsConfig.java | 5 +- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/AbstractConfig.java b/nullaway/src/main/java/com/uber/nullaway/AbstractConfig.java index 57af691f26..c12b34c38b 100644 --- a/nullaway/src/main/java/com/uber/nullaway/AbstractConfig.java +++ b/nullaway/src/main/java/com/uber/nullaway/AbstractConfig.java @@ -30,8 +30,6 @@ import com.google.errorprone.util.ASTHelpers; import com.sun.tools.javac.code.Symbol; import com.uber.nullaway.fixserialization.FixSerializationConfig; -import java.util.LinkedHashSet; -import java.util.Set; import java.util.regex.Pattern; import javax.annotation.Nullable; @@ -77,29 +75,25 @@ public abstract class AbstractConfig implements Config { protected boolean handleTestAssertionLibraries; - protected Set optionalClassPaths; + protected ImmutableSet optionalClassPaths; protected boolean assertsEnabled; - /** - * if true, {@link #fromAnnotatedPackage(Symbol.ClassSymbol)} will return false for any class - * annotated with {@link javax.annotation.Generated} - */ protected boolean treatGeneratedAsUnannotated; protected boolean acknowledgeAndroidRecent; - protected Set knownInitializers; + protected ImmutableSet knownInitializers; - protected Set excludedClassAnnotations; + protected ImmutableSet excludedClassAnnotations; - protected Set generatedCodeAnnotations; + protected ImmutableSet generatedCodeAnnotations; - protected Set initializerAnnotations; + protected ImmutableSet initializerAnnotations; - protected Set externalInitAnnotations; + protected ImmutableSet externalInitAnnotations; - protected Set contractAnnotations; + protected ImmutableSet contractAnnotations; @Nullable protected String castToNonNullMethod; @@ -116,9 +110,9 @@ public abstract class AbstractConfig implements Config { protected String errorURL; /** --- Fully qualified names of custom nonnull/nullable annotation --- */ - protected Set customNonnullAnnotations; + protected ImmutableSet customNonnullAnnotations; - protected Set customNullableAnnotations; + protected ImmutableSet customNullableAnnotations; /** * If active, NullAway will write all reporting errors in output directory. The output directory @@ -141,7 +135,7 @@ public FixSerializationConfig getSerializationConfig() { return fixSerializationConfig; } - protected static Pattern getPackagePattern(Set packagePrefixes) { + protected static Pattern getPackagePattern(ImmutableSet packagePrefixes) { // noinspection ConstantConditions String choiceRegexp = Joiner.on("|") @@ -193,12 +187,12 @@ public boolean isUnannotatedClass(Symbol.ClassSymbol symbol) { @Override public ImmutableSet getExcludedClassAnnotations() { - return ImmutableSet.copyOf(excludedClassAnnotations); + return excludedClassAnnotations; } @Override public ImmutableSet getGeneratedCodeAnnotations() { - return ImmutableSet.copyOf(generatedCodeAnnotations); + return generatedCodeAnnotations; } @Override @@ -262,7 +256,7 @@ public boolean handleTestAssertionLibraries() { } @Override - public Set getOptionalClassPaths() { + public ImmutableSet getOptionalClassPaths() { return optionalClassPaths; } @@ -296,17 +290,6 @@ public boolean isContractAnnotation(String annotationName) { return contractAnnotations.contains(annotationName); } - protected Set getKnownInitializers(Set qualifiedNames) { - Set result = new LinkedHashSet<>(); - for (String name : qualifiedNames) { - int lastDot = name.lastIndexOf('.'); - String methodName = name.substring(lastDot + 1); - String className = name.substring(0, lastDot); - result.add(MethodClassAndName.create(className, methodName)); - } - return result; - } - @AutoValue abstract static class MethodClassAndName { @@ -314,6 +297,13 @@ static MethodClassAndName create(String enclosingClass, String methodName) { return new AutoValue_AbstractConfig_MethodClassAndName(enclosingClass, methodName); } + static MethodClassAndName fromClassDotMethod(String classDotMethod) { + int lastDot = classDotMethod.lastIndexOf('.'); + String methodName = classDotMethod.substring(lastDot + 1); + String className = classDotMethod.substring(0, lastDot); + return MethodClassAndName.create(className, methodName); + } + abstract String enclosingClass(); abstract String methodName(); diff --git a/nullaway/src/main/java/com/uber/nullaway/ErrorProneCLIFlagsConfig.java b/nullaway/src/main/java/com/uber/nullaway/ErrorProneCLIFlagsConfig.java index 06923c3607..2d461cc897 100644 --- a/nullaway/src/main/java/com/uber/nullaway/ErrorProneCLIFlagsConfig.java +++ b/nullaway/src/main/java/com/uber/nullaway/ErrorProneCLIFlagsConfig.java @@ -163,8 +163,9 @@ final class ErrorProneCLIFlagsConfig extends AbstractConfig { sourceClassesToExclude = getFlagStringSet(flags, FL_CLASSES_TO_EXCLUDE); unannotatedClasses = getFlagStringSet(flags, FL_UNANNOTATED_CLASSES); knownInitializers = - getKnownInitializers( - getFlagStringSet(flags, FL_KNOWN_INITIALIZERS, DEFAULT_KNOWN_INITIALIZERS)); + getFlagStringSet(flags, FL_KNOWN_INITIALIZERS, DEFAULT_KNOWN_INITIALIZERS).stream() + .map(MethodClassAndName::fromClassDotMethod) + .collect(ImmutableSet.toImmutableSet()); excludedClassAnnotations = getFlagStringSet( flags, FL_CLASS_ANNOTATIONS_TO_EXCLUDE, DEFAULT_CLASS_ANNOTATIONS_TO_EXCLUDE);