Test
This commit is contained in:
		| @@ -209,7 +209,7 @@ public final class AstDisambiguationPass { | |||||||
|                 if (field != null) { |                 if (field != null) { | ||||||
|                     // todo check field is static |                     // todo check field is static | ||||||
|                     ASTTypeExpression typeExpr = new ASTTypeExpression(type); |                     ASTTypeExpression typeExpr = new ASTTypeExpression(type); | ||||||
|                     return resolveExpr(typeExpr, identifier, remaining, processor); |                     return resolveExpr(typeExpr, nextIdent, remaining, processor); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -47,11 +47,7 @@ public class ClasspathSymbolResolver implements SymbolResolver { | |||||||
|             JClassSymbol outer = resolveClassFromCanonicalName(canonicalName.substring(0, lastDotIdx)); |             JClassSymbol outer = resolveClassFromCanonicalName(canonicalName.substring(0, lastDotIdx)); | ||||||
|             if (outer != null) { |             if (outer != null) { | ||||||
|                 String innerName = canonicalName.substring(lastDotIdx + 1); |                 String innerName = canonicalName.substring(lastDotIdx + 1); | ||||||
|                 for (JClassSymbol inner : outer.getDeclaredClasses()) { |                 return outer.getDeclaredClass(innerName); | ||||||
|                     if (inner.getSimpleName().equals(innerName)) { |  | ||||||
|                         return inner; |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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") {} | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | }) | ||||||
| @@ -10,6 +10,7 @@ import com.github.oowekyala.treeutils.matchers.TreeNodeWrapper | |||||||
| import com.github.oowekyala.treeutils.matchers.baseShouldMatchSubtree | import com.github.oowekyala.treeutils.matchers.baseShouldMatchSubtree | ||||||
| import com.github.oowekyala.treeutils.printers.KotlintestBeanTreePrinter | import com.github.oowekyala.treeutils.printers.KotlintestBeanTreePrinter | ||||||
| import net.sourceforge.pmd.lang.ast.Node | import net.sourceforge.pmd.lang.ast.Node | ||||||
|  | import io.kotlintest.should as ktShould | ||||||
|  |  | ||||||
| /** An adapter for [baseShouldMatchSubtree]. */ | /** An adapter for [baseShouldMatchSubtree]. */ | ||||||
| object NodeTreeLikeAdapter : DoublyLinkedTreeLikeAdapter<Node> { | 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, *>) | inline fun <reified N : Node> matchNode(ignoreChildren: Boolean = false, noinline nodeSpec: ValuedNodeSpec<N, *>) | ||||||
|         : Assertions<Node?> = { it.shouldMatchNode(ignoreChildren, nodeSpec) } |         : 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) | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Clément Fournier
					Clément Fournier