From f838b183bfceb68011bf236a2a68d2481726fecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 15 Jan 2022 15:44:01 +0100 Subject: [PATCH] Fix #3698 - Error resolving Symbol Table --- .../pmd/lang/java/symboltable/NameFinder.java | 10 ++++++---- .../ScopeAndDeclarationFinderTest.java | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/NameFinder.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/NameFinder.java index f234097f83..9f3e291345 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/NameFinder.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/NameFinder.java @@ -59,10 +59,12 @@ public class NameFinder { if (node instanceof ASTPrimarySuffix) { ASTPrimarySuffix suffix = (ASTPrimarySuffix) node; if (suffix.isArguments()) { - JavaNameOccurrence occurrence = names.get(names.size() - 1); - occurrence.setIsMethodOrConstructorInvocation(); - ASTArguments args = (ASTArguments) ((ASTPrimarySuffix) node).getChild(0); - occurrence.setArgumentCount(args.size()); + if (!names.isEmpty()) { + JavaNameOccurrence occurrence = names.get(names.size() - 1); + occurrence.setIsMethodOrConstructorInvocation(); + ASTArguments args = (ASTArguments) ((ASTPrimarySuffix) node).getChild(0); + occurrence.setArgumentCount(args.size()); + } } else if (suffix.getNumChildren() == 1 && suffix.getChild(0) instanceof ASTMemberSelector) { ASTMemberSelector member = (ASTMemberSelector) suffix.getChild(0); if (member.getNumChildren() == 1 && member.getChild(0) instanceof ASTMethodReference) { diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symboltable/ScopeAndDeclarationFinderTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symboltable/ScopeAndDeclarationFinderTest.java index 446c0c0a33..23d8dbc4dc 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symboltable/ScopeAndDeclarationFinderTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symboltable/ScopeAndDeclarationFinderTest.java @@ -16,6 +16,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator; +import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression; import net.sourceforge.pmd.lang.symboltable.NameDeclaration; public class ScopeAndDeclarationFinderTest extends BaseNonParserTest { @@ -72,4 +73,19 @@ public class ScopeAndDeclarationFinderTest extends BaseNonParserTest { ClassScope scope2 = methods.get(1).getScope().getEnclosingScope(ClassScope.class); Assert.assertSame(scope1, scope2); } + + + + @Test + public void testSuperCtor() { + // #3698 -- test that parsing does not throw (this executes the symbol pass) + ASTCompilationUnit acu = parseCode("class Foo { Object rs; class Inner { \n" + + " Inner(Object phase) {\n" + + " (rs.deferredAttr).super(AttrMode.SPECULATIVE, msym, phase);\n" + + " } } }"); + ASTClassOrInterfaceDeclaration inner = acu.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class) + .getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class); + Assert.assertEquals(5, inner.findDescendantsOfType(ASTPrimaryExpression.class).size()); + } + }