From 7f6e53eccb484815191b7f817026107e9f03629e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Tue, 11 Feb 2020 02:33:45 +0100 Subject: [PATCH] Test --- .../lang/java/ast/AstDisambiguationPass.java | 2 +- .../impl/reflect/ClasspathSymbolResolver.java | 6 +- .../lang/java/ast/VarDisambiguationTest.kt | 57 +++++++++++++++++++ .../pmd/lang/ast/test/AstMatcherDslAdapter.kt | 20 +++++++ 4 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/VarDisambiguationTest.kt diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AstDisambiguationPass.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AstDisambiguationPass.java index 8a3b9a852e..b886d06f61 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AstDisambiguationPass.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AstDisambiguationPass.java @@ -209,7 +209,7 @@ public final class AstDisambiguationPass { if (field != null) { // todo check field is static ASTTypeExpression typeExpr = new ASTTypeExpression(type); - return resolveExpr(typeExpr, identifier, remaining, processor); + return resolveExpr(typeExpr, nextIdent, remaining, processor); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/impl/reflect/ClasspathSymbolResolver.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/impl/reflect/ClasspathSymbolResolver.java index 31801e26d3..ac9d07b075 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/impl/reflect/ClasspathSymbolResolver.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symbols/internal/impl/reflect/ClasspathSymbolResolver.java @@ -47,11 +47,7 @@ public class ClasspathSymbolResolver implements SymbolResolver { JClassSymbol outer = resolveClassFromCanonicalName(canonicalName.substring(0, lastDotIdx)); if (outer != null) { String innerName = canonicalName.substring(lastDotIdx + 1); - for (JClassSymbol inner : outer.getDeclaredClasses()) { - if (inner.getSimpleName().equals(innerName)) { - return inner; - } - } + return outer.getDeclaredClass(innerName); } } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/VarDisambiguationTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/VarDisambiguationTest.kt new file mode 100644 index 0000000000..e979d4d869 --- /dev/null +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/VarDisambiguationTest.kt @@ -0,0 +1,57 @@ +package net.sourceforge.pmd.lang.java.ast + +import net.sourceforge.pmd.lang.ast.test.shouldMatchN + +/** + * @author Clément Fournier + * @since 7.0.0 + */ +class VarDisambiguationTest : ParserTestSpec({ + + + parserTest("Inner class names") { + val code = (""" + package com.foo.bar; + class Foo { + Foo f1; + static class Inner { + static final Foo inField; + } + + { + Foo.Inner.inField.call(); + Inner.inField.call(); + } + } + """) + + + doTest("Without disambig") { + val acu = parser.withProcessing(false).parse(code) + val (m1, m2) = acu.descendants(ASTMethodCall::class.java).toList() + m1.qualifier!!.shouldMatchN { ambiguousName("Foo.Inner.inField") } + m2.qualifier!!.shouldMatchN { ambiguousName("Inner.inField") } + } + + doTest("With disambig") { + val acu = parser.withProcessing(true).parse(code) + val (m1, m2) = acu.descendants(ASTMethodCall::class.java).toList() + m1.qualifier!!.shouldMatchN { + fieldAccess("inField") { + typeExpr { + classType("Inner") { + classType("Foo") + } + } + } + } + m2.qualifier!!.shouldMatchN { + fieldAccess("inField") { + typeExpr { + classType("Inner") {} + } + } + } + } + } +}) diff --git a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt index f4a9ca95bd..b0942c6a2b 100644 --- a/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt +++ b/pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/AstMatcherDslAdapter.kt @@ -10,6 +10,7 @@ import com.github.oowekyala.treeutils.matchers.TreeNodeWrapper import com.github.oowekyala.treeutils.matchers.baseShouldMatchSubtree import com.github.oowekyala.treeutils.printers.KotlintestBeanTreePrinter import net.sourceforge.pmd.lang.ast.Node +import io.kotlintest.should as ktShould /** An adapter for [baseShouldMatchSubtree]. */ object NodeTreeLikeAdapter : DoublyLinkedTreeLikeAdapter { @@ -65,3 +66,22 @@ inline fun Node?.shouldMatchNode(ignoreChildren: Boolean = fa */ inline fun matchNode(ignoreChildren: Boolean = false, noinline nodeSpec: ValuedNodeSpec) : Assertions = { it.shouldMatchNode(ignoreChildren, nodeSpec) } + +/** + * The spec applies to the parent, shifted so that [this] node + * is the first node to be queried. This allows using sweeter + * DSL constructs like in the Java module. + */ +fun Node.shouldMatchN(matcher: ValuedNodeSpec) { + val idx = indexInParent + parent ktShould matchNode { + if (idx > 0) { + unspecifiedChildren(idx) + } + matcher() + val left = it.numChildren - 1 - idx + if (left > 0) { + unspecifiedChildren(left) + } + } +}