Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StaticImportCheck: Add java.util.Collections candidates #23

Merged
merged 6 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand Down Expand Up @@ -162,7 +163,7 @@ boolean before2(Set<T> set1, Set<T> set2) {

@AfterTemplate
boolean after(Set<T> set1, Set<T> set2) {
return Collections.disjoint(set1, set2);
return disjoint(set1, set2);
}
}

Expand All @@ -177,15 +178,15 @@ static final class DisjointCollections<T> {
@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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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;
Expand All @@ -16,7 +18,6 @@
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;
Expand Down Expand Up @@ -168,7 +169,7 @@ List<T> before() {
return Refaster.anyOf(
ImmutableList.<T>builder().build(),
Stream.<T>empty().collect(toImmutableList()),
Collections.emptyList(),
emptyList(),
List.of());
}

Expand All @@ -188,7 +189,7 @@ static final class ImmutableListOf1<T> {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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;
Expand All @@ -14,7 +16,6 @@
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;
Expand Down Expand Up @@ -214,7 +215,7 @@ ImmutableMap<K, V2> after(Map<K, V1> map) {
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
Expand All @@ -233,9 +234,7 @@ static final class ImmutableMapOf1<K, V> {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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;
Expand All @@ -15,7 +17,6 @@
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;
Expand Down Expand Up @@ -120,7 +121,7 @@ Set<T> before() {
return Refaster.anyOf(
ImmutableSet.<T>builder().build(),
Stream.<T>empty().collect(toImmutableSet()),
Collections.emptySet(),
emptySet(),
Set.of());
}

Expand All @@ -138,8 +139,7 @@ ImmutableSet<T> after() {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -50,13 +52,13 @@ ZoneId before() {
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;
}
}

Expand All @@ -78,7 +80,7 @@ static final class UtcClock {
@BeforeTemplate
@SuppressWarnings("TimeZoneUsage")
Clock before() {
return Clock.system(ZoneOffset.UTC);
return Clock.system(UTC);
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ void replacement() {
"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;",
Expand All @@ -147,6 +149,9 @@ void replacement() {
" ImmutableSet.toImmutableSet();",
" ImmutableSet.<String>toImmutableSet();",
"",
" Collections.disjoint(ImmutableSet.of(), ImmutableSet.of());",
" Collections.reverse(new ArrayList<>());",
"",
" Predicates.not(null);",
" not(null);",
"",
Expand Down Expand Up @@ -194,6 +199,8 @@ void replacement() {
"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;",
Expand All @@ -211,6 +218,8 @@ void replacement() {
"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;",
Expand All @@ -228,6 +237,9 @@ void replacement() {
" toImmutableSet();",
" ImmutableSet.<String>toImmutableSet();",
"",
" disjoint(ImmutableSet.of(), ImmutableSet.of());",
" reverse(new ArrayList<>());",
"",
" Predicates.not(null);",
" not(null);",
"",
Expand Down