[java] Parser Error for empty statements

Fixes #378
This commit is contained in:
Andreas Dangel
2017-05-20 21:28:39 +02:00
parent 362fa0e19e
commit 4e72eef495
3 changed files with 33 additions and 5 deletions

View File

@ -1,4 +1,9 @@
/**
* Allow empty statements (";") between package, import
* and type declarations.
* Bug #378
* Andreas Dangel 05/2017
*====================================================================
* Allow method references to specify generics also for
* constructor references ("new").
* Bug #309
@ -1252,9 +1257,9 @@ TOKEN :
ASTCompilationUnit CompilationUnit() :
{}
{
[ LOOKAHEAD( ( Annotation() )* "package" ) PackageDeclaration() ]
( ImportDeclaration() )*
( TypeDeclaration() )*
[ LOOKAHEAD( ( Annotation() )* "package" ) PackageDeclaration() ( EmptyStatement() )* ]
( ImportDeclaration() ( EmptyStatement() )* )*
( TypeDeclaration() ( EmptyStatement() )* )*
( < "\u001a" > )?
( < "~[]" > )?
<EOF>
@ -1317,8 +1322,6 @@ void TypeDeclaration():
int modifiers;
}
{
";"
|
modifiers = Modifiers()
(
ClassOrInterfaceDeclaration(modifiers)

View File

@ -194,6 +194,29 @@ public class ParserCornersTest extends ParserTst {
Assert.assertEquals(0, cu.findDescendantsOfType(ASTCastExpression.class).size());
}
/**
* Empty statements should be allowed.
* @throws Exception
* @see https://github.com/pmd/pmd/issues/378
*/
@Test
public void testParseEmptyStatements() throws Exception {
String code = "import a;;import b; public class Foo {}";
ASTCompilationUnit cu = parseJava18(code);
assertNotNull(cu);
Assert.assertEquals(ASTEmptyStatement.class, cu.jjtGetChild(1).getClass());
String code2 = "package c;; import a; import b; public class Foo {}";
ASTCompilationUnit cu2 = parseJava18(code2);
assertNotNull(cu2);
Assert.assertEquals(ASTEmptyStatement.class, cu2.jjtGetChild(1).getClass());
String code3 = "package c; import a; import b; public class Foo {};";
ASTCompilationUnit cu3 = parseJava18(code3);
assertNotNull(cu3);
Assert.assertEquals(ASTEmptyStatement.class, cu3.jjtGetChild(4).getClass());
}
private String readAsString(String resource) {
InputStream in = ParserCornersTest.class.getResourceAsStream(resource);
try {

View File

@ -38,6 +38,8 @@ This is a minor release.
* General
* [#377](https://github.com/pmd/pmd/issues/377): \[core] Use maven wrapper and upgrade to maven 3.5.0
* [#376](https://github.com/pmd/pmd/issues/376): \[core] Improve build time on travis
* java
* [#378](https://github.com/pmd/pmd/issues/378): \[java] Parser Error for empty statements
* java-coupling
* [#1427](https://sourceforge.net/p/pmd/bugs/1427/): \[java] Law of Demeter violations for the Builder pattern
* java-design