Skip to content

Commit caa8c71

Browse files
authored
SONARJAVA-6269 S1451 should correctly handle empty headerFormat (#5580)
1 parent d1e2465 commit caa8c71

6 files changed

Lines changed: 84 additions & 22 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
package checks.FileHeaderCheck;
3+
4+
public class ClassBlankLine {
5+
}
6+
// Compliant
7+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* <Your-Product-Name>
3+
* Copyright (c) <Year-From>-<Year-To> <Your-Company-Name>
4+
*
5+
* Please configure this header in your SonarCloud/SonarQube quality profile.
6+
*/
7+
package checks.FileHeaderCheck;
8+
9+
public class ClassDefaultHeader {
10+
}
11+
// Compliant (default header)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package checks.FileHeaderCheck;
2+
3+
public class ClassNoBlankLine {
4+
}
5+
// Compliant

java-checks/src/main/java/org/sonar/java/checks/FileHeaderCheck.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public class FileHeaderCheck extends IssuableSubscriptionVisitor {
4747
defaultValue = "false")
4848
public boolean isRegularExpression = false;
4949

50-
private String[] expectedLines;
5150
private Pattern searchPattern = null;
5251

5352
@Override
@@ -58,18 +57,26 @@ public List<Tree.Kind> nodesToVisit() {
5857
@Override
5958
public void setContext(JavaFileScannerContext context) {
6059
super.context = context;
61-
if (isRegularExpression) {
62-
if (searchPattern == null) {
63-
try {
64-
searchPattern = Pattern.compile(getHeaderFormat(), Pattern.DOTALL);
65-
} catch (IllegalArgumentException e) {
66-
throw new IllegalArgumentException("[" + getClass().getSimpleName() + "] Unable to compile the regular expression: " + headerFormat, e);
67-
}
60+
61+
if (headerFormat.isEmpty()) {
62+
checkExpectedLines(new String[] {});
63+
return;
64+
}
65+
66+
if (!isRegularExpression) {
67+
String[] expectedLines = headerFormat.split("(?:\r)?\n|\r");
68+
checkExpectedLines(expectedLines);
69+
return;
70+
}
71+
72+
if (searchPattern == null) {
73+
try {
74+
searchPattern = Pattern.compile(getHeaderFormat(), Pattern.DOTALL);
75+
} catch (IllegalArgumentException e) {
76+
throw new IllegalArgumentException("[" + getClass().getSimpleName() + "] Unable to compile the regular expression: " + headerFormat, e);
6877
}
69-
} else {
70-
expectedLines = headerFormat.split("(?:\r)?\n|\r");
7178
}
72-
visitFile();
79+
checkRegularExpression(context.getFileContent());
7380
}
7481

7582
private String getHeaderFormat() {
@@ -80,13 +87,9 @@ private String getHeaderFormat() {
8087
return format;
8188
}
8289

83-
private void visitFile() {
84-
if (isRegularExpression) {
85-
checkRegularExpression(context.getFileContent());
86-
} else {
87-
if (!matches(expectedLines, context.getFileLines())) {
88-
addIssueOnFile(MESSAGE);
89-
}
90+
private void checkExpectedLines(String[] expectedLines) {
91+
if (!matches(expectedLines, context.getFileLines())) {
92+
addIssueOnFile(MESSAGE);
9093
}
9194
}
9295

@@ -104,9 +107,9 @@ private static boolean matches(String[] expectedLines, List<String> lines) {
104107
result = true;
105108

106109
Iterator<String> it = lines.iterator();
107-
for (int i = 0; i < expectedLines.length; i++) {
110+
for (String expectedLine : expectedLines) {
108111
String line = it.next();
109-
if (!line.equals(expectedLines[i])) {
112+
if (!line.equals(expectedLine)) {
110113
result = false;
111114
break;
112115
}

java-checks/src/test/java/org/sonar/java/checks/FileHeaderCheckTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,26 @@ void test() {
9898
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Class3.java"))
9999
.withCheck(check)
100100
.verifyNoIssues();
101+
102+
check = new FileHeaderCheck();
103+
check.headerFormat = "";
104+
CheckVerifier.newVerifier()
105+
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/ClassBlankLine.java"))
106+
.withCheck(check)
107+
.verifyNoIssues();
108+
109+
check = new FileHeaderCheck();
110+
check.headerFormat = "";
111+
CheckVerifier.newVerifier()
112+
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/ClassNoBlankLine.java"))
113+
.withCheck(check)
114+
.verifyNoIssues();
115+
116+
check = new FileHeaderCheck();
117+
CheckVerifier.newVerifier()
118+
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/ClassDefaultHeader.java"))
119+
.withCheck(check)
120+
.verifyNoIssues();
101121
}
102122

103123
@Test
@@ -139,6 +159,22 @@ void regex() {
139159
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/Regex4.java"))
140160
.withCheck(check)
141161
.verifyIssues();
162+
163+
check = new FileHeaderCheck();
164+
check.headerFormat = "";
165+
check.isRegularExpression = true;
166+
CheckVerifier.newVerifier()
167+
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/ClassBlankLine.java"))
168+
.withCheck(check)
169+
.verifyNoIssues();
170+
171+
check = new FileHeaderCheck();
172+
check.headerFormat = "";
173+
check.isRegularExpression = true;
174+
CheckVerifier.newVerifier()
175+
.onFile(mainCodeSourcesPath("checks/FileHeaderCheck/ClassNoBlankLine.java"))
176+
.withCheck(check)
177+
.verifyNoIssues();
142178
}
143179

144180
@Test

sonar-java-plugin/src/test/java/org/sonar/plugins/java/JavaSensorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ void do_not_filter_checks_when_no_autoscan() throws IOException {
429429
"CustomRepository:CustomMainCheck",
430430
"CustomRepository:CustomJspCheck",
431431
"CustomRepository:CustomTestCheck",
432-
// not in SonarWay (FileHeaderCheck)
433-
"java:S1451",
432+
// not in SonarWay (CatchUsesExceptionWithContextCheck)
433+
"java:S1166",
434434
// main check in SonarWay (DefaultPackageCheck)
435435
"java:S1220",
436436
// main check in SonarWay, not supported by autoscan (CombineCatchCheck)

0 commit comments

Comments
 (0)