Skip to content

Commit ba3b0a4

Browse files
committed
Issue #68: Updated header recipe to prepend the expected header
1 parent 9279400 commit ba3b0a4

File tree

3 files changed

+21
-58
lines changed

3 files changed

+21
-58
lines changed

src/main/java/org/checkstyle/autofix/recipe/Header.java

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
import java.nio.charset.Charset;
2222
import java.nio.file.Files;
2323
import java.nio.file.Path;
24-
import java.util.ArrayList;
25-
import java.util.Arrays;
26-
import java.util.HashSet;
2724
import java.util.List;
28-
import java.util.Set;
2925
import java.util.stream.Collectors;
3026

3127
import org.checkstyle.autofix.parser.CheckConfiguration;
@@ -42,7 +38,6 @@
4238
public class Header extends Recipe {
4339
private static final String HEADER_PROPERTY = "header";
4440
private static final String HEADER_FILE_PROPERTY = "headerFile";
45-
private static final String IGNORE_LINES_PROPERTY = "ignoreLines";
4641
private static final String CHARSET_PROPERTY = "charset";
4742
private static final String LINE_SEPARATOR = "\n";
4843

@@ -67,8 +62,7 @@ public String getDescription() {
6762
@Override
6863
public TreeVisitor<?, ExecutionContext> getVisitor() {
6964
final String licenseHeader = extractLicenseHeader(config);
70-
final List<Integer> ignoreLines = extractIgnoreLines(config);
71-
return new HeaderVisitor(violations, licenseHeader, ignoreLines);
65+
return new HeaderVisitor(violations, licenseHeader);
7266
}
7367

7468
private static String extractLicenseHeader(CheckConfiguration config) {
@@ -91,29 +85,13 @@ private static String extractLicenseHeader(CheckConfiguration config) {
9185
return header;
9286
}
9387

94-
private static List<Integer> extractIgnoreLines(CheckConfiguration config) {
95-
final List<Integer> ignoreLinesList;
96-
if (config.hasProperty(IGNORE_LINES_PROPERTY)) {
97-
ignoreLinesList = Arrays.stream(config.getIntArray(IGNORE_LINES_PROPERTY))
98-
.boxed()
99-
.toList();
100-
}
101-
else {
102-
ignoreLinesList = new ArrayList<>();
103-
}
104-
return ignoreLinesList;
105-
}
106-
10788
private static class HeaderVisitor extends JavaIsoVisitor<ExecutionContext> {
10889
private final List<CheckstyleViolation> violations;
10990
private final String licenseHeader;
110-
private final List<Integer> ignoreLines;
11191

112-
HeaderVisitor(List<CheckstyleViolation> violations, String licenseHeader,
113-
List<Integer> ignoreLines) {
92+
HeaderVisitor(List<CheckstyleViolation> violations, String licenseHeader) {
11493
this.violations = violations;
11594
this.licenseHeader = licenseHeader;
116-
this.ignoreLines = ignoreLines;
11795
}
11896

11997
@Override
@@ -126,11 +104,10 @@ public J visit(Tree tree, ExecutionContext ctx) {
126104

127105
if (hasViolation(filePath)) {
128106
final String currentHeader = extractCurrentHeader(sourceFile);
129-
final String fixedHeader = fixHeaderLines(licenseHeader,
130-
currentHeader, ignoreLines);
107+
final String fixedHeader = licenseHeader + LINE_SEPARATOR + currentHeader;
131108

132109
sourceFile = sourceFile.withPrefix(
133-
Space.format(fixedHeader + LINE_SEPARATOR));
110+
Space.format(fixedHeader));
134111
}
135112
result = super.visit(sourceFile, ctx);
136113
}
@@ -139,35 +116,11 @@ public J visit(Tree tree, ExecutionContext ctx) {
139116

140117
private String extractCurrentHeader(JavaSourceFile sourceFile) {
141118
return sourceFile.getComments().stream()
142-
.map(comment -> comment.printComment(getCursor()))
143-
.collect(Collectors.joining(System.lineSeparator()));
144-
}
145-
146-
private static String fixHeaderLines(String licenseHeader,
147-
String currentHeader, List<Integer> ignoreLines) {
148-
final List<String> currentLines = Arrays
149-
.stream(currentHeader.split(System.lineSeparator()))
150-
.collect(Collectors.toList());
151-
final List<String> licenseLines = Arrays.stream(licenseHeader.split(
152-
System.lineSeparator(), -1)).toList();
153-
154-
final Set<Integer> ignoredLineNumbers = new HashSet<>(ignoreLines);
155-
156-
for (int lineNumber = 1; lineNumber <= licenseLines.size(); lineNumber++) {
157-
final String expectedLine = licenseLines.get(lineNumber - 1);
158-
159-
if (lineNumber <= currentLines.size()) {
160-
if (!ignoredLineNumbers.contains(lineNumber)
161-
&& !expectedLine.equals(currentLines.get(lineNumber - 1))) {
162-
currentLines.set(lineNumber - 1, expectedLine);
163-
}
164-
}
165-
else {
166-
currentLines.add(expectedLine);
167-
}
168-
}
169-
170-
return String.join(LINE_SEPARATOR, currentLines);
119+
.map(comment -> {
120+
return comment.printComment(getCursor())
121+
+ comment.getSuffix().replaceAll("\\r(?=\\n)|\\r", "");
122+
})
123+
.collect(Collectors.joining(""));
171124
}
172125

173126
private boolean hasViolation(Path filePath) {

src/test/resources/org/checkstyle/autofix/recipe/header/headercomments/OutputHeaderComments.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
///////////////////////////////////////////////////////////////////////////////////////////////
22
// Unit test for checkstyle-openrewrite-recipes.
3-
// Dated: 11.07.25
3+
// Dated: XX.XX.XX
44
// Copyright (C) 2025 Authors. Licensed under Apache 2.0.
55
// This file is part of the Checkstyle OpenRewrite test suite.
66
///////////////////////////////////////////////////////////////////////////////////////////////
77

8+
///////////////////////////////////////////////////////////////////////////////////////////////
9+
// Unit test for checkstyle-openrewrite-recipes.
10+
// Dated: 11.07.25
11+
// Copyright (C) 2025 Authors. Licensed under Apache.
12+
///////////////////////////////////////////////////////////////////////////////////////////////
13+
814
package org.checkstyle.autofix.recipe.header.headercomments;
915

1016
public class OutputHeaderComments {

src/test/resources/org/checkstyle/autofix/recipe/header/headerincorrect/OutputHeaderIncorrect.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
///////////////////////////////////////////////////////////////////////////////////////////////
22
// Unit test for checkstyle-openrewrite-recipes.
3-
///////////////////////////////////////////////////////////////////////////////////////////////
3+
// Dated: XX.XX.XX
44
// Copyright (C) 2025 Authors. Licensed under Apache 2.0.
55
// This file is part of the Checkstyle OpenRewrite test suite.
66
///////////////////////////////////////////////////////////////////////////////////////////////
77

8+
///////////////////////////////////////////////////////////////////////////////////////////////
9+
// This is a test class
10+
///////////////////////////////////////////////////////////////////////////////////////////////
11+
812
package org.checkstyle.autofix.recipe.header.headerincorrect;
913

1014
public class OutputHeaderIncorrect {

0 commit comments

Comments
 (0)