mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Require static import of most java.util.Collections methods (#23)
While there, statically import `ZoneOffset.UTC` in a few places.
This commit is contained in:
committed by
GitHub
parent
92fe96286f
commit
1cb4ce97ae
@@ -76,6 +76,7 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT
|
||||
"com.mongodb.client.model.Sorts",
|
||||
"com.mongodb.client.model.Updates",
|
||||
"java.nio.charset.StandardCharsets",
|
||||
"java.util.Collections",
|
||||
"java.util.Comparator",
|
||||
"java.util.Map.Entry",
|
||||
"java.util.regex.Pattern",
|
||||
@@ -146,10 +147,25 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT
|
||||
* <p>Identifiers listed by {@link #STATIC_IMPORT_EXEMPTED_IDENTIFIERS} should be omitted from
|
||||
* this collection.
|
||||
*/
|
||||
// XXX: Perhaps the set of exempted `java.util.Collections` methods is too strict. For now any
|
||||
// method name that could be considered "too vague" or could conceivably mean something else in a
|
||||
// specific context is left out.
|
||||
@VisibleForTesting
|
||||
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_EXEMPTED_MEMBERS =
|
||||
ImmutableSetMultimap.<String, String>builder()
|
||||
.put("com.mongodb.client.model.Filters", "empty")
|
||||
.putAll(
|
||||
"java.util.Collections",
|
||||
"addAll",
|
||||
"copy",
|
||||
"fill",
|
||||
"list",
|
||||
"max",
|
||||
"min",
|
||||
"nCopies",
|
||||
"rotate",
|
||||
"sort",
|
||||
"swap")
|
||||
.putAll("java.util.regex.Pattern", "compile", "matches", "quote")
|
||||
.put("org.springframework.http.MediaType", "ALL")
|
||||
.build();
|
||||
|
||||
@@ -4,6 +4,7 @@ import static com.google.common.base.Preconditions.checkElementIndex;
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
import static com.google.common.collect.Sets.toImmutableEnumSet;
|
||||
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
|
||||
import static java.util.Collections.disjoint;
|
||||
import static java.util.Objects.checkIndex;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
@@ -162,7 +163,7 @@ final class AssortedTemplates {
|
||||
|
||||
@AfterTemplate
|
||||
boolean after(Set<T> set1, Set<T> set2) {
|
||||
return Collections.disjoint(set1, set2);
|
||||
return disjoint(set1, set2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,15 +178,15 @@ final class AssortedTemplates {
|
||||
@BeforeTemplate
|
||||
boolean before(Collection<T> collection1, Collection<T> collection2) {
|
||||
return Refaster.anyOf(
|
||||
Collections.disjoint(ImmutableSet.copyOf(collection1), collection2),
|
||||
Collections.disjoint(new HashSet<>(collection1), collection2),
|
||||
Collections.disjoint(collection1, ImmutableSet.copyOf(collection2)),
|
||||
Collections.disjoint(collection1, new HashSet<>(collection2)));
|
||||
disjoint(ImmutableSet.copyOf(collection1), collection2),
|
||||
disjoint(new HashSet<>(collection1), collection2),
|
||||
disjoint(collection1, ImmutableSet.copyOf(collection2)),
|
||||
disjoint(collection1, new HashSet<>(collection2)));
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
boolean after(Collection<T> collection1, Collection<T> collection2) {
|
||||
return Collections.disjoint(collection1, collection2);
|
||||
return disjoint(collection1, collection2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package tech.picnic.errorprone.refastertemplates;
|
||||
import static com.google.common.collect.ImmutableList.toImmutableList;
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Comparator.naturalOrder;
|
||||
import static java.util.stream.Collectors.collectingAndThen;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
@@ -16,7 +18,6 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate;
|
||||
import com.google.errorprone.refaster.annotation.UseImportPolicy;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -168,7 +169,7 @@ final class ImmutableListTemplates {
|
||||
return Refaster.anyOf(
|
||||
ImmutableList.<T>builder().build(),
|
||||
Stream.<T>empty().collect(toImmutableList()),
|
||||
Collections.emptyList(),
|
||||
emptyList(),
|
||||
List.of());
|
||||
}
|
||||
|
||||
@@ -188,7 +189,7 @@ final class ImmutableListTemplates {
|
||||
@BeforeTemplate
|
||||
List<T> before(T e1) {
|
||||
return Refaster.anyOf(
|
||||
ImmutableList.<T>builder().add(e1).build(), Collections.singletonList(e1), List.of(e1));
|
||||
ImmutableList.<T>builder().add(e1).build(), singletonList(e1), List.of(e1));
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
|
||||
@@ -2,6 +2,8 @@ package tech.picnic.errorprone.refastertemplates;
|
||||
|
||||
import static com.google.common.collect.ImmutableMap.toImmutableMap;
|
||||
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static java.util.function.Function.identity;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
@@ -14,7 +16,6 @@ import com.google.errorprone.refaster.annotation.MayOptionallyUse;
|
||||
import com.google.errorprone.refaster.annotation.Placeholder;
|
||||
import com.google.errorprone.refaster.annotation.UseImportPolicy;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -214,7 +215,7 @@ final class ImmutableMapTemplates {
|
||||
static final class ImmutableMapOf<K, V> {
|
||||
@BeforeTemplate
|
||||
Map<K, V> before() {
|
||||
return Refaster.anyOf(ImmutableMap.<K, V>builder().build(), Collections.emptyMap(), Map.of());
|
||||
return Refaster.anyOf(ImmutableMap.<K, V>builder().build(), emptyMap(), Map.of());
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
@@ -233,9 +234,7 @@ final class ImmutableMapTemplates {
|
||||
@BeforeTemplate
|
||||
Map<K, V> before(K k1, V v1) {
|
||||
return Refaster.anyOf(
|
||||
ImmutableMap.<K, V>builder().put(k1, v1).build(),
|
||||
Collections.singletonMap(k1, v1),
|
||||
Map.of(k1, v1));
|
||||
ImmutableMap.<K, V>builder().put(k1, v1).build(), singletonMap(k1, v1), Map.of(k1, v1));
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
|
||||
@@ -2,6 +2,8 @@ package tech.picnic.errorprone.refastertemplates;
|
||||
|
||||
import static com.google.common.collect.ImmutableSet.toImmutableSet;
|
||||
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
|
||||
import static java.util.Collections.emptySet;
|
||||
import static java.util.Collections.singleton;
|
||||
import static java.util.stream.Collectors.collectingAndThen;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
@@ -15,7 +17,6 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate;
|
||||
import com.google.errorprone.refaster.annotation.UseImportPolicy;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
@@ -120,7 +121,7 @@ final class ImmutableSetTemplates {
|
||||
return Refaster.anyOf(
|
||||
ImmutableSet.<T>builder().build(),
|
||||
Stream.<T>empty().collect(toImmutableSet()),
|
||||
Collections.emptySet(),
|
||||
emptySet(),
|
||||
Set.of());
|
||||
}
|
||||
|
||||
@@ -138,8 +139,7 @@ final class ImmutableSetTemplates {
|
||||
static final class ImmutableSetOf1<T> {
|
||||
@BeforeTemplate
|
||||
Set<T> before(T e1) {
|
||||
return Refaster.anyOf(
|
||||
ImmutableSet.<T>builder().add(e1).build(), Collections.singleton(e1), Set.of(e1));
|
||||
return Refaster.anyOf(ImmutableSet.<T>builder().add(e1).build(), singleton(e1), Set.of(e1));
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package tech.picnic.errorprone.refastertemplates;
|
||||
|
||||
import static java.time.ZoneOffset.UTC;
|
||||
|
||||
import com.google.errorprone.refaster.Refaster;
|
||||
import com.google.errorprone.refaster.annotation.AfterTemplate;
|
||||
import com.google.errorprone.refaster.annotation.AlsoNegation;
|
||||
@@ -50,13 +52,13 @@ final class TimeTemplates {
|
||||
ZoneId.of("UTC"),
|
||||
ZoneId.of("+0"),
|
||||
ZoneId.of("-0"),
|
||||
ZoneOffset.UTC.normalized(),
|
||||
ZoneId.from(ZoneOffset.UTC));
|
||||
UTC.normalized(),
|
||||
ZoneId.from(UTC));
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
ZoneOffset after() {
|
||||
return ZoneOffset.UTC;
|
||||
return UTC;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +80,7 @@ final class TimeTemplates {
|
||||
@BeforeTemplate
|
||||
@SuppressWarnings("TimeZoneUsage")
|
||||
Clock before() {
|
||||
return Clock.system(ZoneOffset.UTC);
|
||||
return Clock.system(UTC);
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
|
||||
@@ -130,6 +130,8 @@ public final class StaticImportCheckTest {
|
||||
"import com.google.errorprone.BugPattern;",
|
||||
"import com.google.errorprone.BugPattern.SeverityLevel;",
|
||||
"import java.nio.charset.StandardCharsets;",
|
||||
"import java.util.ArrayList;",
|
||||
"import java.util.Collections;",
|
||||
"import java.util.Objects;",
|
||||
"import java.util.regex.Pattern;",
|
||||
"import org.junit.jupiter.params.provider.Arguments;",
|
||||
@@ -147,6 +149,9 @@ public final class StaticImportCheckTest {
|
||||
" ImmutableSet.toImmutableSet();",
|
||||
" ImmutableSet.<String>toImmutableSet();",
|
||||
"",
|
||||
" Collections.disjoint(ImmutableSet.of(), ImmutableSet.of());",
|
||||
" Collections.reverse(new ArrayList<>());",
|
||||
"",
|
||||
" Predicates.not(null);",
|
||||
" not(null);",
|
||||
"",
|
||||
@@ -194,6 +199,8 @@ public final class StaticImportCheckTest {
|
||||
"import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;",
|
||||
"import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;",
|
||||
"import static java.nio.charset.StandardCharsets.UTF_8;",
|
||||
"import static java.util.Collections.disjoint;",
|
||||
"import static java.util.Collections.reverse;",
|
||||
"import static java.util.Objects.requireNonNull;",
|
||||
"import static java.util.function.Predicate.not;",
|
||||
"import static java.util.regex.Pattern.CASE_INSENSITIVE;",
|
||||
@@ -211,6 +218,8 @@ public final class StaticImportCheckTest {
|
||||
"import com.google.errorprone.BugPattern;",
|
||||
"import com.google.errorprone.BugPattern.SeverityLevel;",
|
||||
"import java.nio.charset.StandardCharsets;",
|
||||
"import java.util.ArrayList;",
|
||||
"import java.util.Collections;",
|
||||
"import java.util.Objects;",
|
||||
"import java.util.regex.Pattern;",
|
||||
"import org.junit.jupiter.params.provider.Arguments;",
|
||||
@@ -228,6 +237,9 @@ public final class StaticImportCheckTest {
|
||||
" toImmutableSet();",
|
||||
" ImmutableSet.<String>toImmutableSet();",
|
||||
"",
|
||||
" disjoint(ImmutableSet.of(), ImmutableSet.of());",
|
||||
" reverse(new ArrayList<>());",
|
||||
"",
|
||||
" Predicates.not(null);",
|
||||
" not(null);",
|
||||
"",
|
||||
|
||||
Reference in New Issue
Block a user