#1355 NullPointerException in a java file having a single comment line

This commit is contained in:
Andreas Dangel
2015-05-18 21:17:16 +02:00
parent a416513fc9
commit b50e49777f
3 changed files with 14 additions and 2 deletions

View File

@ -47,8 +47,11 @@ public class ASTCompilationUnit extends AbstractJavaTypeNode implements RootNode
}
public ASTPackageDeclaration getPackageDeclaration() {
Node n = jjtGetChild(0);
return n instanceof ASTPackageDeclaration ? (ASTPackageDeclaration) n : null;
if (jjtGetNumChildren() > 0) {
Node n = jjtGetChild(0);
return n instanceof ASTPackageDeclaration ? (ASTPackageDeclaration) n : null;
}
return null;
}
public ClassTypeResolver getClassTypeResolver() {

View File

@ -79,6 +79,14 @@ public class ParserCornersTest extends ParserTst {
+ " private void deleteDirectory(String path) {\n" + " LOG.debug(path);\n" + " }\n" + "}");
}
/**
* Test for https://sourceforge.net/p/pmd/bugs/1355/
*/
@Test
public void emptyFileJustComment() {
parseJava18("// just a comment");
}
@Test
public void testMultipleExceptionCatching() {
String code = "public class Foo { public void bar() { "

View File

@ -19,5 +19,6 @@
* [#1345](https://sourceforge.net/p/pmd/bugs/1345/): UseCollectionIsEmpty throws NullPointerException
* [#1353](https://sourceforge.net/p/pmd/bugs/1353/): False positive "Only One Return" with lambda
* [#1354](https://sourceforge.net/p/pmd/bugs/1354/): Complex FieldDeclarationsShouldBeAtStartOfClass false positive with Spring annotations
* [#1355](https://sourceforge.net/p/pmd/bugs/1355/): NullPointerException in a java file having a single comment line
**API Changes:**