Fix PackageDeclaration check for files with comments only. #1149
This commit is contained in:
parent
dd47d21b9e
commit
2ce3fc4fd0
|
|
@ -39,6 +39,9 @@ public final class PackageDeclarationCheck extends Check {
|
|||
*/
|
||||
public static final String MSG_KEY = "missing.package.declaration";
|
||||
|
||||
/** Line number used to log violation when no AST nodes are present in file. */
|
||||
private static final int DEFAULT_LINE_NUMBER = 1;
|
||||
|
||||
/** is package defined. */
|
||||
private boolean defined;
|
||||
|
||||
|
|
@ -65,7 +68,11 @@ public final class PackageDeclarationCheck extends Check {
|
|||
@Override
|
||||
public void finishTree(DetailAST ast) {
|
||||
if (!defined) {
|
||||
log(ast.getLineNo(), MSG_KEY);
|
||||
int lineNumber = DEFAULT_LINE_NUMBER;
|
||||
if (ast != null) {
|
||||
lineNumber = ast.getLineNo();
|
||||
}
|
||||
log(lineNumber, MSG_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -90,7 +90,9 @@ public class DetailASTTest {
|
|||
System.getProperty("file.encoding", "UTF-8"));
|
||||
final FileContents contents = new FileContents(text);
|
||||
final DetailAST rootAST = TreeWalker.parse(contents);
|
||||
checkTree(rootAST, null, null, filename, rootAST);
|
||||
if (rootAST != null) {
|
||||
checkTree(rootAST, null, null, filename, rootAST);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkTree(final DetailAST node,
|
||||
|
|
|
|||
|
|
@ -38,4 +38,15 @@ public class PackageDeclarationCheckTest extends BaseCheckTestSupport {
|
|||
verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/"
|
||||
+ "checkstyle/coding/InputNoPackage.java").getCanonicalPath(), expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnFileWithCommentOnly() throws Exception {
|
||||
DefaultConfiguration checkConfig = createCheckConfig(PackageDeclarationCheck.class);
|
||||
|
||||
String[] expected = {
|
||||
"1: " + getCheckMessage(MSG_KEY),
|
||||
};
|
||||
|
||||
verify(checkConfig, getPath("InputWithCommentOnly.java"), expected);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
/* Comment only */
|
||||
Loading…
Reference in New Issue