diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index cce100569b..4224b6e37f 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1,4 +1,9 @@ /** + * Allow method references to specify generics also for + * constructor references ("new"). + * Bug #309 + * Andreas Dangel 03/2017 + *==================================================================== * Provide a better fix for CastExpression, getting rid of most hacks. * Bug #257 * @@ -1831,7 +1836,7 @@ Token t; void MethodReference() : {Token t; checkForBadMethodReferenceUsage();} { - "::" ("new" {jjtThis.setImage("new");} | [TypeArguments()] t= {jjtThis.setImage(t.image);} ) + "::" [TypeArguments()] ( "new" {jjtThis.setImage("new");} | t= {jjtThis.setImage(t.image);} ) } void PrimaryPrefix() : 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 809b95357e..cbb3dfef57 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 @@ -4,6 +4,7 @@ package net.sourceforge.pmd.lang.java.ast; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import java.io.IOException; @@ -172,6 +173,13 @@ public class ParserCornersTest extends ParserTst { assertEquals("A cast was found when none expected", 0, compilationUnit.findDescendantsOfType(ASTCastExpression.class).size()); } + @Test + public void testGitHubBug309() throws Exception { + String code = readAsString("GitHubBug309.java"); + ASTCompilationUnit compilationUnit = parseJava18(code); + assertNotNull(compilationUnit); + } + /** * This triggered bug #1484 UnusedLocalVariable - false positive - parenthesis * @throws Exception diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug309.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug309.java new file mode 100644 index 0000000000..a7e1a24ec5 --- /dev/null +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/GitHubBug309.java @@ -0,0 +1,8 @@ +import java.util.*; + +public class GitHubBug309 { + public static void main(String[] args) { + Runnable r11 = Main::new; + IntFunction r13 = int[]::new; // produces the same results + } +} diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 97309d4437..5cda91b25f 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -20,6 +20,7 @@ This is a bug fixing release. * general: * [#305](https://github.com/pmd/pmd/issues/305): \[core] PMD not executing under git bash * java-design: + * [#309](https://github.com/pmd/pmd/issues/309): \[java] Parse error on method reference * [#275](https://github.com/pmd/pmd/issues/275): \[java] FinalFieldCouldBeStatic: Constant in @interface incorrectly reported as "could be made static" * java-junit * [#285](https://github.com/pmd/pmd/issues/285): \[java] JUnitTestsShouldIncludeAssertRule should support @Rule as well as @Test(expected = ...)