Skip to content

Commit 1d2bc93

Browse files
graememorganError Prone Team
authored and
Error Prone Team
committed
Introduce ErrorProneFlags.get{Set,List}OrEmpty, because basically every caller wants to interpret an empty optional as an empty list/set.
There's some uses of getList outside core EP, but after a JB release I should be able to migrate them, and then delete the method. PiperOrigin-RevId: 567270684
1 parent 1bec842 commit 1d2bc93

File tree

9 files changed

+57
-37
lines changed

9 files changed

+57
-37
lines changed

check_api/src/main/java/com/google/errorprone/ErrorProneFlags.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,47 @@ public Optional<Integer> getInteger(String key) {
152152
* an {@link Optional}, which is empty if the flag is unset.
153153
*
154154
* <p>(note: empty strings included, e.g. {@code "-XepOpt:List=,1,,2," => ["","1","","2",""]})
155+
*
156+
* @deprecated prefer {@link #getListOrEmpty(String)}
155157
*/
158+
@Deprecated
156159
public Optional<List<String>> getList(String key) {
157160
return this.get(key).map(v -> ImmutableList.copyOf(Splitter.on(',').split(v)));
158161
}
159162

163+
/**
164+
* Gets the flag value for the given key as a comma-separated {@link ImmutableList} of Strings, or
165+
* an empty list if the flag is unset.
166+
*
167+
* <p>(note: empty strings included, e.g. {@code "-XepOpt:List=,1,,2," => ["","1","","2",""]})
168+
*/
169+
public ImmutableList<String> getListOrEmpty(String key) {
170+
return getList(key).map(ImmutableList::copyOf).orElse(ImmutableList.of());
171+
}
172+
160173
/**
161174
* Gets the flag value for the given key as a comma-separated {@link Set} of Strings, wrapped in
162175
* an {@link Optional}, which is empty if the flag is unset.
163176
*
164177
* <p>(note: empty strings included, e.g. {@code "-XepOpt:Set=,1,,1,2," => ["","1","2"]})
178+
*
179+
* @deprecated prefer {@link #getSetOrEmpty(String)}
165180
*/
181+
@Deprecated
166182
public Optional<Set<String>> getSet(String key) {
167183
return this.get(key).map(v -> ImmutableSet.copyOf(Splitter.on(',').split(v)));
168184
}
169185

186+
/**
187+
* Gets the flag value for the given key as a comma-separated {@link Set} of Strings, or an empty
188+
* set if unset.
189+
*
190+
* <p>(note: empty strings included, e.g. {@code "-XepOpt:Set=,1,,1,2," => ["","1","2"]})
191+
*/
192+
public ImmutableSet<String> getSetOrEmpty(String key) {
193+
return getSet(key).map(ImmutableSet::copyOf).orElse(ImmutableSet.of());
194+
}
195+
170196
/** Whether this Flags object is empty, i.e. no flags have been set. */
171197
public boolean isEmpty() {
172198
return this.flagsMap.isEmpty();

check_api/src/test/java/com/google/errorprone/ErrorProneFlagsTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static com.google.common.truth.Truth8.assertThat;
2121
import static org.junit.Assert.assertThrows;
2222

23-
import com.google.common.collect.ImmutableList;
2423
import com.google.common.collect.ImmutableMap;
2524
import com.google.common.collect.ImmutableSet;
2625
import org.junit.Test;
@@ -92,12 +91,12 @@ public void parseAndGetList() {
9291
.parseFlag("-XepOpt:ArgD=7")
9392
.parseFlag("-XepOpt:ArgE=")
9493
.build();
95-
assertThat(flags.getList("ArgA")).hasValue(ImmutableList.of("1", "2", "3"));
96-
assertThat(flags.getList("ArgB")).hasValue(ImmutableList.of("4", ""));
97-
assertThat(flags.getList("ArgC")).hasValue(ImmutableList.of("5", "", "", "6"));
98-
assertThat(flags.getList("ArgD")).hasValue(ImmutableList.of("7"));
99-
assertThat(flags.getList("ArgE")).hasValue(ImmutableList.of(""));
100-
assertThat(flags.getList("absent")).isEmpty();
94+
assertThat(flags.getListOrEmpty("ArgA")).containsExactly("1", "2", "3").inOrder();
95+
assertThat(flags.getListOrEmpty("ArgB")).containsExactly("4", "").inOrder();
96+
assertThat(flags.getListOrEmpty("ArgC")).containsExactly("5", "", "", "6").inOrder();
97+
assertThat(flags.getListOrEmpty("ArgD")).containsExactly("7");
98+
assertThat(flags.getListOrEmpty("ArgE")).containsExactly("");
99+
assertThat(flags.getListOrEmpty("absent")).isEmpty();
101100
}
102101

103102
@Test

core/src/main/java/com/google/errorprone/bugpatterns/CheckReturnValue.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,10 @@ public MethodKind getMethodKind(MethodSymbol method) {
169169

170170
// This is conceptually lower precedence than the above rules.
171171
externalIgnoreList());
172-
flags
173-
.getList(CRV_PACKAGES)
174-
.ifPresent(packagePatterns -> builder.addRule(PackagesRule.fromPatterns(packagePatterns)));
172+
var crvPackages = flags.getListOrEmpty(CRV_PACKAGES);
173+
if (!crvPackages.isEmpty()) {
174+
builder.addRule(PackagesRule.fromPatterns(crvPackages));
175+
}
175176
this.evaluator =
176177
builder
177178
.addRule(

core/src/main/java/com/google/errorprone/bugpatterns/ParameterName.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ public class ParameterName extends BugChecker
7272
@Inject
7373
ParameterName(ErrorProneFlags errorProneFlags) {
7474
this.exemptPackages =
75-
errorProneFlags
76-
.getList("ParameterName:exemptPackagePrefixes")
77-
.orElse(ImmutableList.of())
78-
.stream()
75+
errorProneFlags.getListOrEmpty("ParameterName:exemptPackagePrefixes").stream()
7976
// add a trailing '.' so that e.g. com.foo matches as a prefix of com.foo.bar, but not
8077
// com.foobar
8178
.map(p -> p.endsWith(".") ? p : p + ".")

core/src/main/java/com/google/errorprone/bugpatterns/UnusedMethod.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ public final class UnusedMethod extends BugChecker implements CompilationUnitTre
167167
UnusedMethod(ErrorProneFlags errorProneFlags) {
168168
this.exemptingMethodAnnotations =
169169
union(
170-
errorProneFlags
171-
.getSet("UnusedMethod:ExemptingMethodAnnotations")
172-
.orElseGet(ImmutableSet::of),
170+
errorProneFlags.getSetOrEmpty("UnusedMethod:ExemptingMethodAnnotations"),
173171
EXEMPTING_METHOD_ANNOTATIONS)
174172
.immutableCopy();
175173
}

core/src/main/java/com/google/errorprone/bugpatterns/UnusedVariable.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,24 @@ public final class UnusedVariable extends BugChecker implements CompilationUnitT
178178

179179
@Inject
180180
UnusedVariable(ErrorProneFlags flags) {
181-
ImmutableSet.Builder<String> methodAnnotationsExemptingParameters =
182-
ImmutableSet.<String>builder().add("org.robolectric.annotation.Implementation");
183-
flags
184-
.getList("Unused:methodAnnotationsExemptingParameters")
185-
.ifPresent(methodAnnotationsExemptingParameters::addAll);
186-
this.methodAnnotationsExemptingParameters = methodAnnotationsExemptingParameters.build();
181+
this.methodAnnotationsExemptingParameters =
182+
ImmutableSet.<String>builder()
183+
.add("org.robolectric.annotation.Implementation")
184+
.addAll(flags.getListOrEmpty("Unused:methodAnnotationsExemptingParameters"))
185+
.build();
187186
this.reportInjectedFields = flags.getBoolean("Unused:ReportInjectedFields").orElse(false);
188187

189-
ImmutableSet.Builder<String> exemptNames = ImmutableSet.<String>builder().add("ignored");
190-
flags.getList("Unused:exemptNames").ifPresent(exemptNames::addAll);
191-
this.exemptNames = exemptNames.build();
192-
193-
ImmutableSet.Builder<String> exemptPrefixes = ImmutableSet.<String>builder().add("unused");
194-
flags.getSet("Unused:exemptPrefixes").ifPresent(exemptPrefixes::addAll);
195-
this.exemptPrefixes = exemptPrefixes.build();
188+
this.exemptNames =
189+
ImmutableSet.<String>builder()
190+
.add("ignored")
191+
.addAll(flags.getListOrEmpty("Unused:exemptNames"))
192+
.build();
193+
194+
this.exemptPrefixes =
195+
ImmutableSet.<String>builder()
196+
.add("unused")
197+
.addAll(flags.getSetOrEmpty("Unused:exemptPrefixes"))
198+
.build();
196199
}
197200

198201
@Override

core/src/main/java/com/google/errorprone/bugpatterns/checkreturnvalue/CanIgnoreReturnValueSuggester.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ public CanIgnoreReturnValueSuggester(ErrorProneFlags errorProneFlags) {
9797
this.exemptingMethodAnnotations =
9898
Sets.union(
9999
EXEMPTING_METHOD_ANNOTATIONS,
100-
errorProneFlags
101-
.getSet("CanIgnoreReturnValue:ExemptingMethodAnnotations")
102-
.orElse(ImmutableSet.of()))
100+
errorProneFlags.getSetOrEmpty("CanIgnoreReturnValue:ExemptingMethodAnnotations"))
103101
.immutableCopy();
104102
}
105103

core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ public final class Inliner extends BugChecker
9191

9292
@Inject
9393
Inliner(ErrorProneFlags flags) {
94-
this.apiPrefixes =
95-
ImmutableSet.copyOf(flags.getSet(PREFIX_FLAG).orElse(ImmutableSet.<String>of()));
94+
this.apiPrefixes = flags.getSetOrEmpty(PREFIX_FLAG);
9695
this.skipCallsitesWithComments = flags.getBoolean(SKIP_COMMENTS_FLAG).orElse(true);
9796
this.checkFixCompiles = flags.getBoolean(CHECK_FIX_COMPILES).orElse(false);
9897
}

core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/WellKnownThreadSafety.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
public final class WellKnownThreadSafety implements ThreadSafety.KnownTypes {
2828
@Inject
2929
WellKnownThreadSafety(ErrorProneFlags flags, WellKnownMutability wellKnownMutability) {
30-
List<String> knownThreadSafe =
31-
flags.getList("ThreadSafe:KnownThreadSafe").orElse(ImmutableList.of());
30+
ImmutableList<String> knownThreadSafe = flags.getListOrEmpty("ThreadSafe:KnownThreadSafe");
3231
this.knownThreadSafeClasses = buildThreadSafeClasses(knownThreadSafe, wellKnownMutability);
3332
this.knownUnsafeClasses = wellKnownMutability.getKnownMutableClasses();
3433
}

0 commit comments

Comments
 (0)