Skip to content

Commit 9eca1f1

Browse files
committed
Ensure single line of whitespace after header
Closes gh-194
1 parent 0223bcb commit 9eca1f1

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringHeaderCheck.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,6 +77,8 @@ public class SpringHeaderCheck extends AbstractFileSetCheck {
7777

7878
private URI packageInfoHeaderFile;
7979

80+
private boolean blankLineAfter = true;
81+
8082
private HeaderCheck check;
8183

8284
private HeaderCheck packageInfoCheck;
@@ -108,7 +110,7 @@ private HeaderCheck createCheck(String headerType, URI headerFile) throws IOExce
108110

109111
@Override
110112
protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
111-
getCheck(file).run(fileText);
113+
getCheck(file).run(fileText, this.blankLineAfter);
112114
}
113115

114116
private HeaderCheck getCheck(File file) {
@@ -145,6 +147,10 @@ public void setPackageInfoHeaderFile(URI packageInfoHeaderFile) {
145147
this.packageInfoHeaderFile = packageInfoHeaderFile;
146148
}
147149

150+
public void setBlankLineAfter(boolean blankLineAfter) {
151+
this.blankLineAfter = blankLineAfter;
152+
}
153+
148154
/**
149155
* Interface used to check for a header.
150156
*/
@@ -153,14 +159,15 @@ private interface HeaderCheck {
153159
/**
154160
* Don't run any checks.
155161
*/
156-
HeaderCheck NONE = (fileText) -> true;
162+
HeaderCheck NONE = (fileText, blankLineAfter) -> true;
157163

158164
/**
159165
* Run the check.
160166
* @param fileText the text to check
167+
* @param blankLineAfter if a blank line should be after the header
161168
* @return {@code true} if the header is valid
162169
*/
163-
boolean run(FileText fileText);
170+
boolean run(FileText fileText, boolean blankLineAfter);
164171

165172
}
166173

@@ -209,7 +216,7 @@ private Pattern loadLine(String line, String copyrightPattern) {
209216
}
210217

211218
@Override
212-
public boolean run(FileText fileText) {
219+
public boolean run(FileText fileText, boolean blankLineAfter) {
213220
if (this.lines.size() > fileText.size()) {
214221
log(1, RegexpHeaderCheck.MSG_HEADER_MISSING);
215222
return false;
@@ -222,6 +229,11 @@ public boolean run(FileText fileText) {
222229
return false;
223230
}
224231
}
232+
if (blankLineAfter) {
233+
if (fileText.size() <= this.lines.size() || !"".equals(fileText.get(this.lines.size()))) {
234+
log(this.lines.size() + 1, "header.blankLine");
235+
}
236+
}
225237
return true;
226238
}
227239

@@ -233,7 +245,7 @@ public boolean run(FileText fileText) {
233245
private class NoHeaderCheck implements HeaderCheck {
234246

235247
@Override
236-
public boolean run(FileText fileText) {
248+
public boolean run(FileText fileText, boolean blankLineAfter) {
237249
for (int i = 0; i < fileText.size(); i++) {
238250
String fileLine = fileText.get(i);
239251
if (fileLine.trim().isEmpty()) {

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/check/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ catch.singleLetter=Single letter catch variable (use "ex" instead).
22
catch.wideEye=''o_O'' catch variable (use "ex" instead).
33
header.unexpected=Unexpected header.
44
header.mismatch=Line does not match expected header line of ''{0}''.
5+
header.blankLine=Blank line expected after header.
56
hide.utility.class=Utility classes should not have a public or default constructor.
67
import.avoidStatic=Using a static member import should be avoided - {0}.
78
import.ordering=Wrong order for ''{0}'' import.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
+Blank line expected after header
2+
+1 error
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
<module name="com.puppycrawl.tools.checkstyle.Checker">
6+
<module name="io.spring.javaformat.checkstyle.SpringChecks">
7+
<property name="headerFile" value="src/test/resources/customHeaderFile.txt"/>
8+
</module>
9+
</module>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// (c) Somebody 2017-2019
2+
/**
3+
* A custom header file.
4+
*
5+
* @author Phillip Webb
6+
*/
7+
public class HeaderMissingBlankLine {
8+
9+
}

0 commit comments

Comments
 (0)