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

Fix LoggerEnclosingClass edge cases for type parameters and anonymous classes #1171

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
Expand Down Expand Up @@ -72,7 +71,8 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
}

Symbol targetSymbol = ASTHelpers.getSymbol(memberSelectTree.getExpression());
Symbol.ClassSymbol enclosingClassSymbol = ASTHelpers.enclosingClass(ASTHelpers.getSymbol(tree));
Symbol.ClassSymbol enclosingClassSymbol =
enclosingConcreteClass(ASTHelpers.enclosingClass(ASTHelpers.getSymbol(tree)));
if (targetSymbol == null || enclosingClassSymbol == null) {
return Description.NO_MATCH;
}
Expand All @@ -83,8 +83,16 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
return buildDescription(classArgument)
.addFix(fix.replace(
classArgument,
SuggestedFixes.prettyType(state, fix, enclosingClassSymbol.type) + ".class")
enclosingClassSymbol.name + ".class")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simple name is sufficient here because the code we're updating is nested within the type -- the previous approach using prettyType wasn't necessary and included type parameters.

.build())
.build();
}

private static Symbol.ClassSymbol enclosingConcreteClass(Symbol.ClassSymbol input) {
Symbol.ClassSymbol current = input;
while (current != null && current.isAnonymous()) {
current = ASTHelpers.enclosingClass(current);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I haven't encountered this in the wild (it's an odd thing to do) but fixed it while I'm here.

}
return current;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ void testFix() {
.doTest();
}

@Test
void testFix_generic() {
fix().addInputLines(
"Test.java",
"import org.slf4j.*;",
"class Test<T> {",
" private static final Logger log = LoggerFactory.getLogger(String.class);",
"}")
.addOutputLines(
"Test.java",
"import org.slf4j.*;",
"class Test<T> {",
" private static final Logger log = LoggerFactory.getLogger(Test.class);",
"}")
.doTest();
}

@Test
void testFix_interface() {
fix().addInputLines(
Expand Down Expand Up @@ -75,6 +92,29 @@ void testFix_nested() {
.doTest();
}

@Test
void testFix_anonymous() {
fix().addInputLines(
"Test.java",
"import org.slf4j.*;",
"class Test {",
" Runnable run = new Runnable() {",
" private final Logger log = LoggerFactory.getLogger(String.class);",
" @Override public void run() {}",
" };",
"}")
.addOutputLines(
"Test.java",
"import org.slf4j.*;",
"class Test {",
" Runnable run = new Runnable() {",
" private final Logger log = LoggerFactory.getLogger(Test.class);",
" @Override public void run() {}",
" };",
"}")
.doTest();
}

@Test
void testNegative() {
fix().addInputLines(
Expand Down
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1171.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
fix:
description: Fix `LoggerEnclosingClass` edge cases for type parameters and anonymous
classes
links:
- https://github.com/palantir/gradle-baseline/pull/1171