Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cernat-catalin committed Jan 9, 2023
1 parent e641091 commit 9eb4bfe
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.errorprone.BugPattern.LinkType.CUSTOM;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static tech.picnic.errorprone.bugpatterns.StaticImport.STATIC_IMPORT_CANDIDATE_MEMBERS;
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL;

import com.google.auto.service.AutoService;
Expand All @@ -22,9 +23,6 @@

/** A {@link BugChecker} that flags methods and constants that should not be statically imported. */
// XXX: Also introduce checks that disallows the following candidates:
// - `com.google.common.base.Strings`
// - `java.util.Optional.empty`
// - `java.util.Locale.ROOT`
// - `ZoneOffset.ofHours` and other `ofXXX`-style methods.
// - `java.time.Clock`.
// - Several other `java.time` classes.
Expand Down Expand Up @@ -122,7 +120,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
@Override
public Description matchIdentifier(IdentifierTree tree, VisitorState state) {
Symbol symbol = ASTHelpers.getSymbol(tree);
if (isCandidate(symbol)) {
if (isMatch(symbol)) {
return getDescription(tree, state, symbol);
}

Expand All @@ -140,23 +138,29 @@ private Description getDescription(IdentifierTree tree, VisitorState state, Symb

@SuppressWarnings("NullAway")
private static String getImportToRemove(Symbol symbol) {
return String.format(
"%s.%s",
symbol.getEnclosingElement().getQualifiedName().toString(),
symbol.getSimpleName().toString());
return String.join(
".", symbol.getEnclosingElement().getQualifiedName(), symbol.getSimpleName());
}

private static boolean isCandidate(Symbol symbol) {
private static boolean isMatch(Symbol symbol) {
Symbol enclosingSymbol = symbol.getEnclosingElement();

if (enclosingSymbol == null) {
return false;
}

String qualifiedTypeName = enclosingSymbol.getQualifiedName().toString();
String identifierName = symbol.getSimpleName().toString();
return !isExempted(qualifiedTypeName, identifierName)
&& isCandidate(qualifiedTypeName, identifierName);
}

private static boolean isCandidate(String qualifiedTypeName, String identifierName) {
return BAD_STATIC_IMPORT_CANDIDATE_TYPES.contains(qualifiedTypeName)
|| BAD_STATIC_IMPORT_CANDIDATE_MEMBERS.containsEntry(
qualifiedTypeName, symbol.getSimpleName().toString())
|| BAD_STATIC_IMPORT_CANDIDATE_IDENTIFIERS.contains(symbol.getSimpleName().toString());
|| BAD_STATIC_IMPORT_CANDIDATE_MEMBERS.containsEntry(qualifiedTypeName, identifierName)
|| BAD_STATIC_IMPORT_CANDIDATE_IDENTIFIERS.contains(identifierName);
}

private static boolean isExempted(String qualifiedTypeName, String identifierName) {
return STATIC_IMPORT_CANDIDATE_MEMBERS.containsEntry(qualifiedTypeName, identifierName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ void identifySimpleMethodInvocation() {
compilationTestHelper
.addSourceLines(
"A.java",
"import static java.time.Clock.systemUTC;",
"import static java.util.Optional.empty;",
"import static com.google.common.collect.ImmutableList.copyOf;",
"import static com.google.common.collect.ImmutableMap.of;",
"import static java.time.Clock.systemUTC;",
"import static java.time.Instant.MAX;",
"import static java.time.Instant.MIN;",
"import static java.util.Collections.min;",
"import static java.util.Locale.ROOT;",
"import java.time.Clock;",
"import static java.util.Optional.empty;",
"",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableSet;",
"import java.time.Clock;",
"import java.util.Locale;",
"",
"class A {",
Expand All @@ -76,24 +78,29 @@ void identifySimpleMethodInvocation() {
" Object c1 = MIN;",
" // BUG: Diagnostic contains:",
" Object c2 = MAX;",
"",
" // BUG: Diagnostic contains:",
" ImmutableSet.of(min(ImmutableSet.of()));",
" }",
"}")
.doTest();
}

// XXX: Add more counterexamples
@Test
void replaceSimpleMethodInvocation() {
refactoringTestHelper
.addInputLines(
"A.java",
"import static java.time.Clock.systemUTC;",
"import static java.util.Optional.empty;",
"import static com.google.common.collect.ImmutableList.copyOf;",
"import static java.time.Clock.systemUTC;",
"import static java.time.Instant.MAX;",
"import static java.time.Instant.MIN;",
"import static java.util.Collections.min;",
"import static java.util.Locale.ROOT;",
"import static java.util.Optional.empty;",
"",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableSet;",
"import java.time.Clock;",
"import java.util.Locale;",
"",
Expand All @@ -112,13 +119,17 @@ void replaceSimpleMethodInvocation() {
"",
" Object c1 = MIN;",
" Object c2 = MAX;",
"",
" ImmutableSet.of(min(ImmutableSet.of()));",
" }",
"}")
.addOutputLines(
"A.java",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableSet;",
"import java.time.Clock;",
"import java.time.Instant;",
"import java.util.Collections;",
"import java.util.Locale;",
"import java.util.Optional;",
"",
Expand All @@ -137,6 +148,8 @@ void replaceSimpleMethodInvocation() {
"",
" Object c1 = Instant.MIN;",
" Object c2 = Instant.MAX;",
"",
" ImmutableSet.of(Collections.min(ImmutableSet.of()));",
" }",
"}")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
Expand Down

0 comments on commit 9eb4bfe

Please sign in to comment.