28
28
import java .util .Set ;
29
29
import java .util .stream .Collectors ;
30
30
31
+ import org .checkstyle .autofix .parser .CheckConfiguration ;
31
32
import org .checkstyle .autofix .parser .CheckstyleViolation ;
32
33
import org .openrewrite .ExecutionContext ;
33
34
import org .openrewrite .Recipe ;
38
39
import org .openrewrite .java .tree .JavaSourceFile ;
39
40
import org .openrewrite .java .tree .Space ;
40
41
41
- import com .puppycrawl .tools .checkstyle .api .CheckstyleException ;
42
- import com .puppycrawl .tools .checkstyle .api .Configuration ;
43
-
44
42
public class Header extends Recipe {
45
43
private static final String HEADER_PROPERTY = "header" ;
46
44
private static final String HEADER_FILE_PROPERTY = "headerFile" ;
47
45
private static final String IGNORE_LINES_PROPERTY = "ignoreLines" ;
48
46
private static final String CHARSET_PROPERTY = "charset" ;
49
47
50
48
private final List <CheckstyleViolation > violations ;
51
- private final Configuration config ;
52
- private final Charset charset ;
49
+ private final CheckConfiguration config ;
53
50
54
- public Header (List <CheckstyleViolation > violations , Configuration config , Charset charset ) {
51
+ public Header (List <CheckstyleViolation > violations , CheckConfiguration config ) {
55
52
this .violations = violations ;
56
53
this .config = config ;
57
- this .charset = charset ;
58
54
}
59
55
60
56
@ Override
@@ -69,60 +65,44 @@ public String getDescription() {
69
65
70
66
@ Override
71
67
public TreeVisitor <?, ExecutionContext > getVisitor () {
72
- final String licenseHeader = extractLicenseHeader (config , charset );
68
+ final String licenseHeader = extractLicenseHeader (config );
73
69
final List <Integer > ignoreLines = extractIgnoreLines (config );
74
70
return new HeaderVisitor (violations , licenseHeader , ignoreLines );
75
71
}
76
72
77
- private static String extractLicenseHeader (Configuration config , Charset charset ) {
73
+ private static String extractLicenseHeader (CheckConfiguration config ) {
78
74
final String header ;
79
- try {
80
- if (hasProperty (config , HEADER_PROPERTY )) {
81
- header = config .getProperty (HEADER_PROPERTY );
82
- }
83
- else {
84
- final Charset charsetToUse ;
85
- if (hasProperty (config , CHARSET_PROPERTY )) {
86
- charsetToUse = Charset .forName (config .getProperty (CHARSET_PROPERTY ));
87
- }
88
- else {
89
- charsetToUse = charset ;
90
- }
91
- final String headerFilePath = config .getProperty (HEADER_FILE_PROPERTY );
75
+ if (config .hasProperty (HEADER_PROPERTY )) {
76
+ header = config .getProperty (HEADER_PROPERTY );
77
+ }
78
+ else {
79
+ final Charset charsetToUse = Charset .forName (config
80
+ .getPropertyOrDefault (CHARSET_PROPERTY , Charset .defaultCharset ().name ()));
81
+ final String headerFilePath = config .getProperty (HEADER_FILE_PROPERTY );
82
+ try {
92
83
header = Files .readString (Path .of (headerFilePath ), charsetToUse );
93
84
}
94
- }
95
- catch (CheckstyleException | IOException exception ) {
96
- throw new IllegalArgumentException ("Failed to extract header from config" , exception );
85
+ catch (IOException exception ) {
86
+ throw new IllegalArgumentException ("Failed to extract header from config" ,
87
+ exception );
88
+ }
97
89
}
98
90
return header ;
99
91
}
100
92
101
- private static List <Integer > extractIgnoreLines (Configuration config ) {
93
+ private static List <Integer > extractIgnoreLines (CheckConfiguration config ) {
102
94
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
- }
95
+ if (config .hasProperty (IGNORE_LINES_PROPERTY )) {
96
+ ignoreLinesList = Arrays .stream (config .getIntArray (IGNORE_LINES_PROPERTY ))
97
+ .boxed ()
98
+ .toList ();
114
99
}
115
- catch (CheckstyleException exception ) {
116
- throw new IllegalArgumentException (
117
- "Failed to extract ignore lines from config" , exception );
100
+ else {
101
+ ignoreLinesList = new ArrayList <>();
118
102
}
119
103
return ignoreLinesList ;
120
104
}
121
105
122
- private static boolean hasProperty (Configuration config , String propertyName ) {
123
- return Arrays .asList (config .getPropertyNames ()).contains (propertyName );
124
- }
125
-
126
106
private static class HeaderVisitor extends JavaIsoVisitor <ExecutionContext > {
127
107
private final List <CheckstyleViolation > violations ;
128
108
private final String licenseHeader ;
@@ -146,7 +126,7 @@ public J visit(Tree tree, ExecutionContext ctx) {
146
126
if (hasViolation (filePath )) {
147
127
final String currentHeader = extractCurrentHeader (sourceFile );
148
128
final String fixedHeader = fixHeaderLines (licenseHeader ,
149
- currentHeader , ignoreLines );
129
+ currentHeader , ignoreLines );
150
130
151
131
sourceFile = sourceFile .withPrefix (
152
132
Space .format (fixedHeader + System .lineSeparator ()));
0 commit comments