2828import java .util .Set ;
2929import java .util .stream .Collectors ;
3030
31+ import org .checkstyle .autofix .parser .CheckConfiguration ;
3132import org .checkstyle .autofix .parser .CheckstyleViolation ;
3233import org .openrewrite .ExecutionContext ;
3334import org .openrewrite .Recipe ;
3839import org .openrewrite .java .tree .JavaSourceFile ;
3940import org .openrewrite .java .tree .Space ;
4041
41- import com .puppycrawl .tools .checkstyle .api .CheckstyleException ;
42- import com .puppycrawl .tools .checkstyle .api .Configuration ;
43-
4442public class Header extends Recipe {
4543 private static final String HEADER_PROPERTY = "header" ;
4644 private static final String HEADER_FILE_PROPERTY = "headerFile" ;
4745 private static final String IGNORE_LINES_PROPERTY = "ignoreLines" ;
4846 private static final String CHARSET_PROPERTY = "charset" ;
4947
5048 private final List <CheckstyleViolation > violations ;
51- private final Configuration config ;
52- private final Charset charset ;
49+ private final CheckConfiguration config ;
5350
54- public Header (List <CheckstyleViolation > violations , Configuration config , Charset charset ) {
51+ public Header (List <CheckstyleViolation > violations , CheckConfiguration config ) {
5552 this .violations = violations ;
5653 this .config = config ;
57- this .charset = charset ;
5854 }
5955
6056 @ Override
@@ -69,60 +65,53 @@ public String getDescription() {
6965
7066 @ Override
7167 public TreeVisitor <?, ExecutionContext > getVisitor () {
72- final String licenseHeader = extractLicenseHeader (config , charset );
68+ final String licenseHeader = extractLicenseHeader (config );
7369 final List <Integer > ignoreLines = extractIgnoreLines (config );
7470 return new HeaderVisitor (violations , licenseHeader , ignoreLines );
7571 }
7672
77- private static String extractLicenseHeader (Configuration config , Charset charset ) {
73+ private static String extractLicenseHeader (CheckConfiguration config ) {
7874 final String header ;
7975 try {
80- if (hasProperty (config , HEADER_PROPERTY )) {
76+ if (config . hasProperty (HEADER_PROPERTY )) {
8177 header = config .getProperty (HEADER_PROPERTY );
8278 }
8379 else {
8480 final Charset charsetToUse ;
85- if (hasProperty (config , CHARSET_PROPERTY )) {
81+ if (config . hasProperty (CHARSET_PROPERTY )) {
8682 charsetToUse = Charset .forName (config .getProperty (CHARSET_PROPERTY ));
8783 }
84+ else if (config .getParent ().hasProperty (CHARSET_PROPERTY )) {
85+ charsetToUse = Charset .forName (config
86+ .getParent ().getProperty (CHARSET_PROPERTY ));
87+ }
8888 else {
89- charsetToUse = charset ;
89+ charsetToUse = Charset . defaultCharset () ;
9090 }
91+
9192 final String headerFilePath = config .getProperty (HEADER_FILE_PROPERTY );
9293 header = Files .readString (Path .of (headerFilePath ), charsetToUse );
9394 }
9495 }
95- catch (CheckstyleException | IOException exception ) {
96+ catch (IOException exception ) {
9697 throw new IllegalArgumentException ("Failed to extract header from config" , exception );
9798 }
9899 return header ;
99100 }
100101
101- private static List <Integer > extractIgnoreLines (Configuration config ) {
102+ private static List <Integer > extractIgnoreLines (CheckConfiguration config ) {
102103 final List <Integer > ignoreLinesList ;
103- try {
104- if (!hasProperty (config , IGNORE_LINES_PROPERTY )) {
105- ignoreLinesList = new ArrayList <>();
106- }
107- else {
108- final String ignoreLines = config .getProperty (IGNORE_LINES_PROPERTY );
109- ignoreLinesList = Arrays .stream (ignoreLines .split ("," ))
110- .map (String ::trim )
111- .map (Integer ::parseInt )
112- .collect (Collectors .toList ());
113- }
104+ if (!config .hasProperty (IGNORE_LINES_PROPERTY )) {
105+ ignoreLinesList = new ArrayList <>();
114106 }
115- catch (CheckstyleException exception ) {
116- throw new IllegalArgumentException (
117- "Failed to extract ignore lines from config" , exception );
107+ else {
108+ ignoreLinesList = Arrays .stream (config .getIntArray (IGNORE_LINES_PROPERTY ))
109+ .boxed ()
110+ .toList ();
118111 }
119112 return ignoreLinesList ;
120113 }
121114
122- private static boolean hasProperty (Configuration config , String propertyName ) {
123- return Arrays .asList (config .getPropertyNames ()).contains (propertyName );
124- }
125-
126115 private static class HeaderVisitor extends JavaIsoVisitor <ExecutionContext > {
127116 private final List <CheckstyleViolation > violations ;
128117 private final String licenseHeader ;
@@ -146,7 +135,7 @@ public J visit(Tree tree, ExecutionContext ctx) {
146135 if (hasViolation (filePath )) {
147136 final String currentHeader = extractCurrentHeader (sourceFile );
148137 final String fixedHeader = fixHeaderLines (licenseHeader ,
149- currentHeader , ignoreLines );
138+ currentHeader , ignoreLines );
150139
151140 sourceFile = sourceFile .withPrefix (
152141 Space .format (fixedHeader + System .lineSeparator ()));
0 commit comments