From e3602572b020ae24c617b02676caa1993fba753a Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 27 Dec 2022 13:09:11 -0800 Subject: [PATCH] Fix yet another NonCanonicalType crash PiperOrigin-RevId: 498021422 --- .../bugpatterns/NonCanonicalType.java | 9 +++++--- .../bugpatterns/NonCanonicalTypeTest.java | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/NonCanonicalType.java b/core/src/main/java/com/google/errorprone/bugpatterns/NonCanonicalType.java index 746e9578253..bfe848bba12 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/NonCanonicalType.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/NonCanonicalType.java @@ -29,8 +29,9 @@ import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.matchers.Description; import com.google.errorprone.util.Visibility; -import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.MemberSelectTree; +import com.sun.source.tree.ParameterizedTypeTree; +import com.sun.source.tree.Tree; import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol.TypeSymbol; import com.sun.tools.javac.util.Position; @@ -115,7 +116,7 @@ private static String createDescription(String canonicalName, String nonCanonica * Find the non-canonical name which is being used to refer to this type. We can't just use {@code * getSymbol}, given that points to the same symbol as the canonical name. */ - private static String getNonCanonicalName(ExpressionTree tree) { + private static String getNonCanonicalName(Tree tree) { switch (tree.getKind()) { case IDENTIFIER: return getSymbol(tree).getQualifiedName().toString(); @@ -128,8 +129,10 @@ private static String getNonCanonicalName(ExpressionTree tree) { return getNonCanonicalName(memberSelectTree.getExpression()) + "." + memberSelectTree.getIdentifier(); + case PARAMETERIZED_TYPE: + return getNonCanonicalName(((ParameterizedTypeTree) tree).getType()); default: - throw new AssertionError(); + throw new AssertionError(tree.getKind()); } } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/NonCanonicalTypeTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/NonCanonicalTypeTest.java index be7f46836fc..127bca95cc5 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/NonCanonicalTypeTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/NonCanonicalTypeTest.java @@ -219,4 +219,27 @@ public void method_noFinding() { "}") .doTest(); } + + // TODO(cushon): the fix for this should be Super.Inner, not Super.Inner + @Test + public void innerArray() { + compilationHelper + .addSourceLines( + "Super.java", // + "class Super {", + " class Inner {}", + "}") + .addSourceLines( + "Super.java", // + "class Sub extends Super {", + "}") + .addSourceLines( + "Test.java", // + "class Test {", + " // BUG: Diagnostic contains: `Super.Inner` was referred to by the non-canonical name" + + " `Sub.Inner`", + " Sub.Inner[] x;", + "}") + .doTest(); + } }