-
Notifications
You must be signed in to change notification settings - Fork 39
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
support exemption of methods
#22
Merged
Stephan202
merged 4 commits into
master
from
rossendrijver/PSM-1134_static_import_improvements
Jan 3, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,14 +19,18 @@ | |
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.MemberSelectTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.tools.javac.code.Type; | ||
import java.util.Optional; | ||
|
||
/** A {@link BugChecker} which flags methods that can and should be statically imported. */ | ||
/** | ||
* A {@link BugChecker} which flags methods and constants that can and should be statically | ||
* imported. | ||
*/ | ||
// XXX: Tricky cases: | ||
// - `org.springframework.http.MediaType` (do except for `ALL`?) | ||
// - `org.springframework.http.HttpStatus` (not always an improvement, and `valueOf` must | ||
// certainly be excluded) | ||
// - `com.google.common.collect.Tables` | ||
|
@@ -42,15 +46,19 @@ | |
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "StaticImport", | ||
summary = "Method should be statically imported", | ||
summary = "Identifier should be statically imported", | ||
linkType = LinkType.NONE, | ||
severity = SeverityLevel.SUGGESTION, | ||
tags = StandardTags.SIMPLIFICATION) | ||
public final class StaticImportCheck extends BugChecker implements MemberSelectTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* Types whose members should be statically imported, unless exempted by {@link | ||
* #STATIC_IMPORT_EXEMPTED_MEMBERS} or {@link #STATIC_IMPORT_EXEMPTED_IDENTIFIERS}. | ||
*/ | ||
@VisibleForTesting | ||
static final ImmutableSet<String> STATIC_IMPORT_CANDIDATE_CLASSES = | ||
static final ImmutableSet<String> STATIC_IMPORT_CANDIDATE_TYPES = | ||
ImmutableSet.of( | ||
"com.google.common.base.Preconditions", | ||
"com.google.common.base.Predicates", | ||
|
@@ -84,11 +92,13 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT | |
"org.springframework.format.annotation.DateTimeFormat.ISO", | ||
"org.springframework.http.HttpHeaders", | ||
"org.springframework.http.HttpMethod", | ||
"org.springframework.http.MediaType", | ||
"org.testng.Assert", | ||
"reactor.function.TupleUtils"); | ||
|
||
/** Type members that should be statically imported. */ | ||
@VisibleForTesting | ||
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_CANDIDATE_METHODS = | ||
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_CANDIDATE_MEMBERS = | ||
ImmutableSetMultimap.<String, String>builder() | ||
.putAll( | ||
"com.google.common.collect.ImmutableListMultimap", | ||
|
@@ -123,9 +133,41 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT | |
.putAll("com.google.common.collect.Comparators", "emptiesFirst", "emptiesLast") | ||
.build(); | ||
|
||
/** | ||
* Type members that should never be statically imported. | ||
* | ||
* <p>Identifiers listed by {@link #STATIC_IMPORT_EXEMPTED_IDENTIFIERS} should be omitted from | ||
* this collection. | ||
*/ | ||
@VisibleForTesting | ||
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_EXEMPTED_MEMBERS = | ||
ImmutableSetMultimap.<String, String>builder() | ||
.put("com.mongodb.client.model.Filters", "empty") | ||
.put("org.springframework.http.MediaType", "ALL") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add |
||
.build(); | ||
|
||
/** | ||
* Identifiers that should never be statically imported. | ||
* | ||
* <p>This should be a superset of the identifiers flagged by {@link | ||
* com.google.errorprone.bugpatterns.BadImport}. | ||
*/ | ||
@VisibleForTesting | ||
static final ImmutableSet<String> STATIC_IMPORT_EXEMPTED_IDENTIFIERS = | ||
ImmutableSet.of( | ||
"builder", | ||
"create", | ||
"copyOf", | ||
"from", | ||
"getDefaultInstance", | ||
"INSTANCE", | ||
"newBuilder", | ||
"of", | ||
"valueOf"); | ||
|
||
@Override | ||
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) { | ||
if (!isCandidate(state)) { | ||
if (!isCandidateContext(state) || !isCandidate(tree)) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
|
@@ -140,7 +182,7 @@ public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) | |
.orElse(Description.NO_MATCH); | ||
} | ||
|
||
private static boolean isCandidate(VisitorState state) { | ||
private static boolean isCandidateContext(VisitorState state) { | ||
Tree parentTree = | ||
requireNonNull(state.getPath().getParentPath(), "MemberSelectTree lacks enclosing node") | ||
.getLeaf(); | ||
|
@@ -155,15 +197,26 @@ private static boolean isCandidate(VisitorState state) { | |
} | ||
} | ||
|
||
private static boolean isCandidate(MemberSelectTree tree) { | ||
String identifier = tree.getIdentifier().toString(); | ||
if (STATIC_IMPORT_EXEMPTED_IDENTIFIERS.contains(identifier)) { | ||
return false; | ||
} | ||
|
||
Type type = ASTHelpers.getType(tree.getExpression()); | ||
return type != null | ||
&& !STATIC_IMPORT_EXEMPTED_MEMBERS.containsEntry(type.toString(), identifier); | ||
} | ||
|
||
private static Optional<String> getCandidateSimpleName(StaticImportInfo importInfo) { | ||
String canonicalName = importInfo.canonicalName(); | ||
return importInfo | ||
.simpleName() | ||
.toJavaUtil() | ||
.filter( | ||
name -> | ||
STATIC_IMPORT_CANDIDATE_CLASSES.contains(canonicalName) | ||
|| STATIC_IMPORT_CANDIDATE_METHODS.containsEntry(canonicalName, name)); | ||
STATIC_IMPORT_CANDIDATE_TYPES.contains(canonicalName) | ||
|| STATIC_IMPORT_CANDIDATE_MEMBERS.containsEntry(canonicalName, name)); | ||
} | ||
|
||
private static Optional<Fix> tryStaticImport( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The summary is now updated to have
Identifier
shouldn't that also be updated here in the Javadoc? @Stephan202There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that, but here I was thinking this might be a bit clearer albeit less accurate. (The summary is shown the context of a specific "did you mean" suggestion, in which case the "X or Y" feels a bit awkward.)