diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index bfc40a3102..4c84fe58df 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 * @@ -1837,7 +1842,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 b2e90f2f4d..31a29eb39d 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 @@ -5,6 +5,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 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 d26fec25c3..80303aeaf2 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -439,6 +439,7 @@ You need to use this, if you have a large project with many files, and you hit t * [#207](https://github.com/pmd/pmd/issues/207): \[java] Parse error on method reference with generics * [#208](https://github.com/pmd/pmd/issues/208): \[java] Parse error with local class with 2 or more annotations * [#213](https://github.com/pmd/pmd/issues/213): \[java] CPD: OutOfMemory when analyzing Lucene + * [#309](https://github.com/pmd/pmd/issues/309): \[java] Parse error on method reference * [#1542](https://sourceforge.net/p/pmd/bugs/1542/): \[java] CPD throws an NPE when parsing enums with -ignore-identifiers * [#1545](https://sourceforge.net/p/pmd/bugs/1545/): \[java] Symbol Table fails to resolve inner classes * java-basic