-
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
StaticImportCheck
support exemption of methods
#22
Conversation
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_EXEMPTION_METHODS = | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
We could add "valueOf"
here as well, to make it clear. However, the case is already in BAD_STATIC_IDENTIFIERS
so I left it out.
@@ -189,4 +218,21 @@ void replacement() { | |||
"}") | |||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | |||
} | |||
|
|||
@Test | |||
void exemptions() { |
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 idea is that we can add more exemptions here in the future. If you want to leave it out, it is also fine by me.
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.
Tough one. On the one hand this allows us to test all exceptions, but we're also not testing all inclusions. So would indeed be inclined to leave it out.
@@ -172,6 +196,11 @@ void replacement() { | |||
" requireNonNull(\"bar\");", | |||
"", | |||
" Object o = UTF_8;", | |||
"", | |||
" ImmutableSet.of(TEXT_HTML,", |
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.
Would it be better to do the minimal case here and remove (e.g.) the APPLICATION_XHTML_XML
?
Tree currentTree = state.getPath().getLeaf(); | ||
Tree parentTree = state.getPath().getParentPath().getLeaf(); | ||
if ((currentTree.getKind() != MEMBER_SELECT && parentTree.getKind() != METHOD_INVOCATION) | ||
|| parentTree.getKind() == PACKAGE) { |
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.
A bug was found by the selfcheck of EPS. The problem was that String methodName = symbol.name.toString();
would fail because symbol
is null
. This only occurred in a package-info.java
file. So for that reason this parentTree.getKind() == PACKAGE
is added.
Another way of implementing this could be: isPackageInfo(CompilationUnitTree)
method.
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.
Hmm, if I remove this check then all tests still pass, so at the very least we need to increase test coverage. But even then:
- If I remove this
if
statement then the-Pself-check
build still passes. - IIUC
currentTree.getKind() == MEMBER_SELECT
is always true 👀 - It seems that some/all of this logic belongs in
isCandidate
, or that these methods should get different names.
As the plan is to open source, should we not use ticket numbers here? Or can we do that until we go public? |
7af019c
to
49d614f
Compare
StaticImportCheck
support exemption of methodsStaticImportCheck
support exemption of methods
49d614f
to
fdd738c
Compare
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.
Rebased and added two commits. Suggested commit message:
Define assorted `StaticImportCheck` exemptions
While there, require that most `org.springframework.http.MediaType` members are
statically imported.
@@ -123,9 +131,28 @@ | |||
.putAll("com.google.common.collect.Comparators", "emptiesFirst", "emptiesLast") | |||
.build(); | |||
|
|||
@VisibleForTesting | |||
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_EXEMPTION_METHODS = |
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.
To match the grammatical form of existing constants:
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_EXEMPTION_METHODS = | |
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_EXEMPTED_METHODS = |
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.
That said, ALL
isn't a method, so I suppose we should use IDENTIFIERS
, just like Error Prone's BadImport
implementation, or MEMBERS
. I guess the latter makes more sense since here we define (type, member) pairs.
Tree currentTree = state.getPath().getLeaf(); | ||
Tree parentTree = state.getPath().getParentPath().getLeaf(); | ||
if ((currentTree.getKind() != MEMBER_SELECT && parentTree.getKind() != METHOD_INVOCATION) | ||
|| parentTree.getKind() == PACKAGE) { |
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.
Hmm, if I remove this check then all tests still pass, so at the very least we need to increase test coverage. But even then:
- If I remove this
if
statement then the-Pself-check
build still passes. - IIUC
currentTree.getKind() == MEMBER_SELECT
is always true 👀 - It seems that some/all of this logic belongs in
isCandidate
, or that these methods should get different names.
Symbol symbol = | ||
currentTree.getKind() == MEMBER_SELECT | ||
? ASTHelpers.getSymbol(currentTree) | ||
: ASTHelpers.getSymbol(parentTree); |
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.
- We should be getting the
Symbol
, but rather theType
. - Instead of the
PACKAGE
logic above we should likely simply verify that the returned symbol (type) is notnull
.
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.
Hmm, I am double checking your improvements; as I was doing some things here that were overly difficult compared to what it should be / what it is now.
return STATIC_IMPORT_EXEMPTION_METHODS.get(symbol.owner.toString()).stream() | ||
.anyMatch(methodName::equals) |
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.
There's Multimap#containsEntry
:)
@@ -189,4 +218,21 @@ void replacement() { | |||
"}") | |||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); |
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.
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | |
.doTest(TestMode.TEXT_MATCH); |
" }", | ||
"}") | ||
.expectUnchanged() | ||
.doTest(); |
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.
Clearer error message:
.doTest(); | |
.doTest(TestMode.TEXT_MATCH); |
(Maybe we should enforce this mode using a separate check, though after fixing this one no "violations" remain.)
e -> | ||
StaticImportCheck.STATIC_IMPORT_CANDIDATE_CLASSES.contains(e) | ||
|| StaticImportCheck.STATIC_IMPORT_CANDIDATE_METHODS.containsKey(e)); |
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.
This doesn't seem right. Rather the key set should be a subset of STATIC_IMPORT_CANDIDATE_CLASSES
. By construction then there won't be overlap with STATIC_IMPORT_CANDIDATE_METHODS
.
Additionally, redundancy w.r.t. BAD_STATIC_IDENTIFIERS
should also be tested.
@@ -42,7 +49,7 @@ | |||
@AutoService(BugChecker.class) | |||
@BugPattern( | |||
name = "StaticImport", | |||
summary = "Method should be statically imported", | |||
summary = "Method or constant should be statically imported", |
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.
summary = "Method or constant should be statically imported", | |
summary = "Identifier should be statically imported", |
@@ -117,6 +133,11 @@ void replacement() { | |||
" Objects.requireNonNull(\"bar\");", | |||
"", | |||
" Object o = StandardCharsets.UTF_8;", | |||
"", | |||
" ImmutableSet.of(MediaType.TEXT_HTML,", |
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.
Let's stick with GJF formatting.
@@ -189,4 +218,21 @@ void replacement() { | |||
"}") | |||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | |||
} | |||
|
|||
@Test | |||
void exemptions() { |
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.
Tough one. On the one hand this allows us to test all exceptions, but we're also not testing all inclusions. So would indeed be inclined to leave it out.
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.
Only one question, but the code looks very nice! This allows us to fix many of the remaining XXX's in this class and further improve this BugPattern with improvements we already discussed.
It's interesting to see how you solve the questions I have in the code, very informative/instructive 😄 .
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 |
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? @Stephan202
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.
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.)
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.
Do we want a second review or shall we merge? :)
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 |
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.
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.)
I'd say, lets merge 🚀 ! |
While there, update the Javadoc and
@BugPattern
content to better reflect how the check works.