1717
1818package org .checkstyle .autofix ;
1919
20+ import java .io .FileInputStream ;
21+ import java .io .FileNotFoundException ;
22+ import java .io .IOException ;
2023import java .nio .file .Path ;
2124import java .util .List ;
25+ import java .util .Properties ;
2226
2327import org .checkstyle .autofix .parser .CheckstyleReportParser ;
2428import org .checkstyle .autofix .parser .CheckstyleViolation ;
29+ import org .checkstyle .autofix .parser .Configuration ;
2530import org .openrewrite .Option ;
2631import org .openrewrite .Recipe ;
2732
33+ import com .puppycrawl .tools .checkstyle .ConfigurationLoader ;
34+ import com .puppycrawl .tools .checkstyle .PropertiesExpander ;
35+ import com .puppycrawl .tools .checkstyle .api .CheckstyleException ;
36+
2837/**
2938 * Main recipe that automatically fixes all supported Checkstyle violations.
3039 */
@@ -35,6 +44,17 @@ public class CheckstyleAutoFix extends Recipe {
3544 example = "target/checkstyle/checkstyle-report.xml" )
3645 private String violationReportPath ;
3746
47+ @ Option (displayName = "Checkstyle config path" ,
48+ description = "Path to the file containing Checkstyle configuration." ,
49+ example = "config/checkstyle.xml" )
50+ private String checkstyleConfigurationPath ;
51+
52+ @ Option (displayName = "Checkstyle properties file path" ,
53+ description = "Path to the file containing the Checkstyle Properties." ,
54+ example = "config/checkstyle.properties" ,
55+ required = false )
56+ private String propertiesPath ;
57+
3858 @ Override
3959 public String getDisplayName () {
4060 return "Checkstyle autoFix" ;
@@ -49,12 +69,38 @@ public String getViolationReportPath() {
4969 return violationReportPath ;
5070 }
5171
72+ public String getCheckstyleConfigurationPath () {
73+ return checkstyleConfigurationPath ;
74+ }
75+
76+ public String getPropertiesPath () {
77+ return propertiesPath ;
78+ }
79+
5280 @ Override
5381 public List <Recipe > getRecipeList () {
54-
5582 final List <CheckstyleViolation > violations = CheckstyleReportParser
5683 .parse (Path .of (getViolationReportPath ()));
5784
5885 return CheckstyleRecipeRegistry .getRecipes (violations );
5986 }
87+
88+ private Configuration loadCheckstyleConfiguration () throws CheckstyleException , IOException {
89+ Properties props = new Properties ();
90+ final String propFile = getPropertiesPath ();
91+
92+ if (propFile == null ) {
93+ props = System .getProperties ();
94+ }
95+ else {
96+ try (FileInputStream input = new FileInputStream (propFile )) {
97+ props .load (input );
98+ }
99+ catch (FileNotFoundException exception ) {
100+ throw new IllegalArgumentException ("Failed to read: " + propFile , exception );
101+ }
102+ }
103+ return (Configuration ) ConfigurationLoader .loadConfiguration (
104+ checkstyleConfigurationPath , new PropertiesExpander (props ));
105+ }
60106}
0 commit comments