This commit is contained in:
Clément Fournier
2020-02-11 02:33:45 +01:00
parent 578ee68213
commit 7f6e53eccb
4 changed files with 79 additions and 6 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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") {}
}
}
}
}
}
})

View File

@@ -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<Node> {
@@ -65,3 +66,22 @@ inline fun <reified N : Node> Node?.shouldMatchNode(ignoreChildren: Boolean = fa
*/
inline fun <reified N : Node> matchNode(ignoreChildren: Boolean = false, noinline nodeSpec: ValuedNodeSpec<N, *>)
: Assertions<Node?> = { 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<Node, Any>) {
val idx = indexInParent
parent ktShould matchNode<Node> {
if (idx > 0) {
unspecifiedChildren(idx)
}
matcher()
val left = it.numChildren - 1 - idx
if (left > 0) {
unspecifiedChildren(left)
}
}
}