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

Extend UnnecessarilyFullyQualified to remove java.lang qualifiers #2

Closed
wants to merge 1 commit into from

Conversation

rickie
Copy link
Member

@rickie rickie commented Aug 28, 2021

No description provided.

@rickie rickie force-pushed the rossendrijver/java_lang_qualified branch 2 times, most recently from d0f336f to f3542a4 Compare August 28, 2021 07:07
@rickie rickie requested a review from Stephan202 August 28, 2021 07:08
@rickie rickie force-pushed the rossendrijver/java_lang_qualified branch from f3542a4 to cd5be4b Compare September 20, 2021 05:51
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 a commit. I think we can open an upstream PR for this one. Suggested PR/commit message:

UnnecessarilyFullyQualified: drop `java.lang` qualifiers

@@ -61,6 +61,7 @@
public final class UnnecessarilyFullyQualified extends BugChecker
implements CompilationUnitTreeMatcher {

private static final String JAVA_LANG = "java.lang";
Copy link
Member

Choose a reason for hiding this comment

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

While no such package currently exist, we shouldn't also match a hypothetical java.langSomeSuffix:

Suggested change
private static final String JAVA_LANG = "java.lang";
private static final String JAVA_LANG = "java.lang.";

Comment on lines 173 to 168
if (!pathsToFix.get(0).getLeaf().toString().contains(JAVA_LANG)
&& pathsToFix.stream()
.anyMatch(path -> findIdent(nameString, state.withPath(path), VAL_TYP) != null)) {
continue;
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should to special-case java.lang here. Instead we should generalize the anyMatch logic and compare any matching ident again the type symbol under consideration; if they're the same we can proceed.

Suggested change
if (!pathsToFix.get(0).getLeaf().toString().contains(JAVA_LANG)
&& pathsToFix.stream()
.anyMatch(path -> findIdent(nameString, state.withPath(path), VAL_TYP) != null)) {
continue;
}
if (pathsToFix.stream()
.anyMatch(
path -> {
Symbol ident = findIdent(nameString, state.withPath(path), VAL_TYP);
return ident != null && !ident.equals(type);
})) {
continue;
}

continue;
}
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
fixBuilder.addImport(getOnlyElement(types.keySet()).getQualifiedName().toString());
String newImport = getOnlyElement(types.keySet()).getQualifiedName().toString();
if (!newImport.contains(JAVA_LANG)) {
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
if (!newImport.contains(JAVA_LANG)) {
if (!newImport.startsWith(JAVA_LANG)) {

@Stephan202 Stephan202 force-pushed the rossendrijver/java_lang_qualified branch from cd5be4b to 9a06dc8 Compare October 10, 2021 12:40
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.

Generalized the fix to cover additional cases. New suggested commit message:

UnnecessarilyFullyQualified: handle `java.lang` and inconsistent import usages

@rickie rickie force-pushed the rossendrijver/java_lang_qualified branch from 43d7f86 to b368b93 Compare October 12, 2021 10:48
@rickie rickie force-pushed the rossendrijver/java_lang_qualified branch from b368b93 to 80dc087 Compare December 10, 2021 09:07
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.

Slightly extended suggested commit message:

UnnecessarilyFullyQualified: handle `java.lang` and inconsistent import usages

This introduces two improvements:
- Where possible `java.lang.SomeType` references are simplified to `SomeType`
  without introducing an associated `import` statement.
- References to `some.package.SomeType` are now also replaced with `SomeType`
  if said type is already imported and referenced by its simple name elsewhere
  in the compilation unit.

@Stephan202 Stephan202 force-pushed the rossendrijver/java_lang_qualified branch 2 times, most recently from d70dd0f to 8051af2 Compare December 23, 2021 08:46
@rickie
Copy link
Member Author

rickie commented Dec 23, 2021

Opened PR upstream: google#2776

@Stephan202 Stephan202 force-pushed the rossendrijver/java_lang_qualified branch from 8051af2 to 1dae0db Compare January 2, 2022 13:21
@rickie
Copy link
Member Author

rickie commented Jan 5, 2022

Closing this PR as it got merged in: google@c07a7a9

@rickie rickie closed this Jan 5, 2022
@Stephan202 Stephan202 deleted the rossendrijver/java_lang_qualified branch January 6, 2022 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants