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 support exemption of methods #22

Merged

Conversation

rickie
Copy link
Member

@rickie rickie commented Dec 7, 2021

While there, update the Javadoc and @BugPattern content to better reflect how the check works.

@rickie rickie requested a review from Stephan202 December 7, 2021 10:16
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")
Copy link
Member Author

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() {
Copy link
Member Author

@rickie rickie Dec 7, 2021

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.

Copy link
Member

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,",
Copy link
Member Author

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) {
Copy link
Member Author

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.

Copy link
Member

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.

@rickie
Copy link
Member Author

rickie commented Dec 7, 2021

As the plan is to open source, should we not use ticket numbers here? Or can we do that until we go public?

@rickie rickie force-pushed the rossendrijver/PSM-1134_static_import_improvements branch from 7af019c to 49d614f Compare January 2, 2022 13:44
@rickie rickie changed the title PSM-1165 StaticImportCheck support exemption of methods StaticImportCheck support exemption of methods Jan 2, 2022
@Stephan202 Stephan202 force-pushed the rossendrijver/PSM-1134_static_import_improvements branch from 49d614f to fdd738c Compare January 3, 2022 05:10
Copy link
Member

@Stephan202 Stephan202 left a 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 =
Copy link
Member

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:

Suggested change
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_EXEMPTION_METHODS =
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_EXEMPTED_METHODS =

Copy link
Member

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) {
Copy link
Member

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.

Comment on lines 193 to 196
Symbol symbol =
currentTree.getKind() == MEMBER_SELECT
? ASTHelpers.getSymbol(currentTree)
: ASTHelpers.getSymbol(parentTree);
Copy link
Member

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 the Type.
  • Instead of the PACKAGE logic above we should likely simply verify that the returned symbol (type) is not null.

Copy link
Member Author

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.

Comment on lines 199 to 200
return STATIC_IMPORT_EXEMPTION_METHODS.get(symbol.owner.toString()).stream()
.anyMatch(methodName::equals)
Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
.doTest(TestMode.TEXT_MATCH);

" }",
"}")
.expectUnchanged()
.doTest();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearer error message:

Suggested change
.doTest();
.doTest(TestMode.TEXT_MATCH);

(Maybe we should enforce this mode using a separate check, though after fixing this one no "violations" remain.)

Comment on lines 25 to 27
e ->
StaticImportCheck.STATIC_IMPORT_CANDIDATE_CLASSES.contains(e)
|| StaticImportCheck.STATIC_IMPORT_CANDIDATE_METHODS.containsKey(e));
Copy link
Member

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,",
Copy link
Member

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() {
Copy link
Member

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.

Copy link
Member Author

@rickie rickie left a 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
Copy link
Member Author

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

Copy link
Member

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.)

Copy link
Member

@Stephan202 Stephan202 left a 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
Copy link
Member

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.)

@rickie
Copy link
Member Author

rickie commented Jan 3, 2022

I'd say, lets merge 🚀 !

@Stephan202 Stephan202 merged commit c163806 into master Jan 3, 2022
@Stephan202 Stephan202 deleted the rossendrijver/PSM-1134_static_import_improvements branch January 3, 2022 09:20
@Stephan202 Stephan202 added this to the 0.1.0 milestone Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants