From 7ae23b72ec39d1c96f01de615fc9ee9ce4fc07b7 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Thu, 16 Jan 2020 08:07:23 -0500 Subject: [PATCH 1/2] Fix `LoggerEnclosingClass` edge cases for type parameters and anonymous classes --- .../errorprone/LoggerEnclosingClass.java | 14 +++++-- .../errorprone/LoggerEnclosingClassTest.java | 40 +++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LoggerEnclosingClass.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LoggerEnclosingClass.java index 869343380..74f248662 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LoggerEnclosingClass.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/LoggerEnclosingClass.java @@ -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; @@ -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; } @@ -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") .build()) .build(); } + + private static Symbol.ClassSymbol enclosingConcreteClass(Symbol.ClassSymbol input) { + Symbol.ClassSymbol current = input; + while (current != null && current.isAnonymous()) { + current = ASTHelpers.enclosingClass(current); + } + return current; + } } diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LoggerEnclosingClassTest.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LoggerEnclosingClassTest.java index f598cf429..46f86623c 100644 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LoggerEnclosingClassTest.java +++ b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/LoggerEnclosingClassTest.java @@ -37,6 +37,23 @@ void testFix() { .doTest(); } + @Test + void testFix_generic() { + fix().addInputLines( + "Test.java", + "import org.slf4j.*;", + "class Test {", + " private static final Logger log = LoggerFactory.getLogger(String.class);", + "}") + .addOutputLines( + "Test.java", + "import org.slf4j.*;", + "class Test {", + " private static final Logger log = LoggerFactory.getLogger(Test.class);", + "}") + .doTest(); + } + @Test void testFix_interface() { fix().addInputLines( @@ -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( From 1c232b677c7412ca6dc818f0bc960b0efa2c4414 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Thu, 16 Jan 2020 13:07:23 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-1171.v2.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog/@unreleased/pr-1171.v2.yml diff --git a/changelog/@unreleased/pr-1171.v2.yml b/changelog/@unreleased/pr-1171.v2.yml new file mode 100644 index 000000000..c891c8cd7 --- /dev/null +++ b/changelog/@unreleased/pr-1171.v2.yml @@ -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