From fc759db5ca363c8a9bf602e1ffb1276e342b9280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Thu, 10 Sep 2020 12:24:03 +0200 Subject: [PATCH 1/2] Fix #2767 Problem was the stack is empty if the local var declaration is the first node of the compilation unit to be pushed. --- pmd-java/etc/grammar/Java.jjt | 6 ++++-- .../pmd/lang/java/ast/ParserCornersTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index cafd023dcf..0962322888 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1997,8 +1997,10 @@ void BlockStatement(): | { List annotationsAndChildren = new ArrayList(); - while (jjtree.peekNode() instanceof ASTAnnotation) { - annotationsAndChildren.add(jjtree.popNode()); + if (jjtree.nodeArity() > 0) { // peekNode would throw if the stack is empty + while (jjtree.peekNode() instanceof ASTAnnotation) { + annotationsAndChildren.add(jjtree.popNode()); + } } } LocalVariableDeclaration() diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java index 83501273fd..4af25e38db 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/ParserCornersTest.java @@ -177,6 +177,16 @@ public class ParserCornersTest { java8.parseResource("GitHubBug207.java"); } + @Test + public void testGitHubBug2767() { + // PMD fails to parse an initializer block. + // PMD 6.26.0 parses this code just fine. + java.withDefaultVersion("15-preview") + .parse("class Foo {\n" + + " {final int I;}\n" + + "}\n"); + } + @Test public void testBug206() { java8.parse("public @interface Foo {" + "\n" From a13f88de5f6ffb0a05d74d41353e53f6f2568c64 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 11 Sep 2020 17:51:36 +0200 Subject: [PATCH 2/2] [doc] Update release notes, fixes #2767 --- docs/pages/release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 3910997c70..f7b5ffba44 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -18,6 +18,7 @@ This is a {{ site.pmd.release_type }} release. * pmd-java * [#2708](https://github.com/pmd/pmd/pull/2708): \[java] False positive FinalFieldCouldBeStatic when using lombok Builder.Default + * [#2767](https://github.com/pmd/pmd/issues/2767): \[java] IndexOutOfBoundsException when parsing an initializer BlockStatement ### API Changes