2121import java .nio .charset .Charset ;
2222import java .nio .file .Files ;
2323import java .nio .file .Path ;
24- import java .util .ArrayList ;
25- import java .util .Arrays ;
26- import java .util .HashSet ;
2724import java .util .List ;
28- import java .util .Set ;
2925import java .util .stream .Collectors ;
3026
3127import org .checkstyle .autofix .parser .CheckConfiguration ;
4238public 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 ) {
@@ -81,7 +75,7 @@ private static String extractLicenseHeader(CheckConfiguration config) {
8175 .getPropertyOrDefault (CHARSET_PROPERTY , Charset .defaultCharset ().name ()));
8276 final String headerFilePath = config .getProperty (HEADER_FILE_PROPERTY );
8377 try {
84- header = Files .readString (Path .of (headerFilePath ), charsetToUse );
78+ header = toLfLineEnding ( Files .readString (Path .of (headerFilePath ), charsetToUse ) );
8579 }
8680 catch (IOException exception ) {
8781 throw new IllegalArgumentException ("Failed to extract header from config" ,
@@ -91,29 +85,17 @@ 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 ;
88+ private static String toLfLineEnding (String text ) {
89+ return text .replaceAll ("(?x)\\ \\ r(?=\\ \\ n)|\\ r(?=\\ n)" , "" );
10590 }
10691
10792 private static class HeaderVisitor extends JavaIsoVisitor <ExecutionContext > {
10893 private final List <CheckstyleViolation > violations ;
10994 private final String licenseHeader ;
110- private final List <Integer > ignoreLines ;
11195
112- HeaderVisitor (List <CheckstyleViolation > violations , String licenseHeader ,
113- List <Integer > ignoreLines ) {
96+ HeaderVisitor (List <CheckstyleViolation > violations , String licenseHeader ) {
11497 this .violations = violations ;
11598 this .licenseHeader = licenseHeader ;
116- this .ignoreLines = ignoreLines ;
11799 }
118100
119101 @ Override
@@ -126,11 +108,10 @@ public J visit(Tree tree, ExecutionContext ctx) {
126108
127109 if (hasViolation (filePath )) {
128110 final String currentHeader = extractCurrentHeader (sourceFile );
129- final String fixedHeader = fixHeaderLines (licenseHeader ,
130- currentHeader , ignoreLines );
111+ final String fixedHeader = licenseHeader + LINE_SEPARATOR + currentHeader ;
131112
132113 sourceFile = sourceFile .withPrefix (
133- Space .format (fixedHeader + LINE_SEPARATOR ));
114+ Space .format (fixedHeader ));
134115 }
135116 result = super .visit (sourceFile , ctx );
136117 }
@@ -139,35 +120,11 @@ public J visit(Tree tree, ExecutionContext ctx) {
139120
140121 private String extractCurrentHeader (JavaSourceFile sourceFile ) {
141122 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 );
123+ .map (comment -> {
124+ return comment .printComment (getCursor ())
125+ + toLfLineEnding (comment .getSuffix ());
126+ })
127+ .collect (Collectors .joining ("" ));
171128 }
172129
173130 private boolean hasViolation (Path filePath ) {
0 commit comments