Skip to content

Commit

Permalink
Make AbstractConfig collection fields explicity Immutable
Browse files Browse the repository at this point in the history
This is a re-do, from scratch of a very stale PR.
  • Loading branch information
lazaroclapp committed Sep 13, 2022
1 parent 4d854cb commit 9fbea32
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
50 changes: 20 additions & 30 deletions nullaway/src/main/java/com/uber/nullaway/AbstractConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -77,29 +75,25 @@ public abstract class AbstractConfig implements Config {

protected boolean handleTestAssertionLibraries;

protected Set<String> optionalClassPaths;
protected ImmutableSet<String> 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<MethodClassAndName> knownInitializers;
protected ImmutableSet<MethodClassAndName> knownInitializers;

protected Set<String> excludedClassAnnotations;
protected ImmutableSet<String> excludedClassAnnotations;

protected Set<String> generatedCodeAnnotations;
protected ImmutableSet<String> generatedCodeAnnotations;

protected Set<String> initializerAnnotations;
protected ImmutableSet<String> initializerAnnotations;

protected Set<String> externalInitAnnotations;
protected ImmutableSet<String> externalInitAnnotations;

protected Set<String> contractAnnotations;
protected ImmutableSet<String> contractAnnotations;

@Nullable protected String castToNonNullMethod;

Expand All @@ -116,9 +110,9 @@ public abstract class AbstractConfig implements Config {
protected String errorURL;

/** --- Fully qualified names of custom nonnull/nullable annotation --- */
protected Set<String> customNonnullAnnotations;
protected ImmutableSet<String> customNonnullAnnotations;

protected Set<String> customNullableAnnotations;
protected ImmutableSet<String> customNullableAnnotations;

/**
* If active, NullAway will write all reporting errors in output directory. The output directory
Expand All @@ -141,7 +135,7 @@ public FixSerializationConfig getSerializationConfig() {
return fixSerializationConfig;
}

protected static Pattern getPackagePattern(Set<String> packagePrefixes) {
protected static Pattern getPackagePattern(ImmutableSet<String> packagePrefixes) {
// noinspection ConstantConditions
String choiceRegexp =
Joiner.on("|")
Expand Down Expand Up @@ -193,12 +187,12 @@ public boolean isUnannotatedClass(Symbol.ClassSymbol symbol) {

@Override
public ImmutableSet<String> getExcludedClassAnnotations() {
return ImmutableSet.copyOf(excludedClassAnnotations);
return excludedClassAnnotations;
}

@Override
public ImmutableSet<String> getGeneratedCodeAnnotations() {
return ImmutableSet.copyOf(generatedCodeAnnotations);
return generatedCodeAnnotations;
}

@Override
Expand Down Expand Up @@ -262,7 +256,7 @@ public boolean handleTestAssertionLibraries() {
}

@Override
public Set<String> getOptionalClassPaths() {
public ImmutableSet<String> getOptionalClassPaths() {
return optionalClassPaths;
}

Expand Down Expand Up @@ -296,24 +290,20 @@ public boolean isContractAnnotation(String annotationName) {
return contractAnnotations.contains(annotationName);
}

protected Set<MethodClassAndName> getKnownInitializers(Set<String> qualifiedNames) {
Set<MethodClassAndName> 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 {

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

0 comments on commit 9fbea32

Please sign in to comment.