diff --git a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java index 1eb0e45b..ff4ad106 100644 --- a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java @@ -23,12 +23,15 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.pull.MXSerializer; +import org.intellij.lang.annotations.Language; import org.openrewrite.config.Environment; import org.openrewrite.internal.lang.Nullable; -import org.openrewrite.java.style.CheckstyleConfigLoader; import org.openrewrite.style.NamedStyles; +import java.io.IOException; import java.io.InputStream; +import java.io.StringWriter; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; @@ -36,10 +39,15 @@ import java.util.stream.Stream; import static java.util.Collections.emptyMap; +import static org.openrewrite.java.style.CheckstyleConfigLoader.loadCheckstyleConfig; @SuppressWarnings("FieldMayBeFinal") public abstract class ConfigurableRewriteMojo extends AbstractMojo { + private static final String CHECKSTYLE_DOCTYPE = "module PUBLIC " + + "\"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN\" " + + "\"https://checkstyle.org/dtds/configuration_1_3.dtd\""; + @Parameter(property = "rewrite.configLocation", alias = "configLocation", defaultValue = "${maven.multiModuleProjectDirectory}/rewrite.yml") protected String configLocation; @@ -192,6 +200,7 @@ protected enum State { PROCESSED, TO_BE_PROCESSED } + private static final String OPENREWRITE_PROCESSED_MARKER = "openrewrite.processed"; protected void putState(State state) { @@ -248,26 +257,28 @@ protected List loadStyles(MavenProject project, Environment env) { try { Plugin checkstylePlugin = project.getPlugin("org.apache.maven.plugins:maven-checkstyle-plugin"); if (checkstyleConfigFile != null && !checkstyleConfigFile.isEmpty()) { - styles.add(CheckstyleConfigLoader.loadCheckstyleConfig(Paths.get(checkstyleConfigFile), emptyMap())); + styles.add(loadCheckstyleConfig(Paths.get(checkstyleConfigFile), emptyMap())); } else if (checkstyleDetectionEnabled && checkstylePlugin != null) { Object checkstyleConfRaw = checkstylePlugin.getConfiguration(); if (checkstyleConfRaw instanceof Xpp3Dom) { Xpp3Dom xmlCheckstyleConf = (Xpp3Dom) checkstyleConfRaw; Xpp3Dom xmlConfigLocation = xmlCheckstyleConf.getChild("configLocation"); - - if (xmlConfigLocation == null) { - // When no config location is specified, the maven-checkstyle-plugin falls back on sun_checks.xml - try (InputStream is = Checker.class.getResourceAsStream("/sun_checks.xml")) { - if (is != null) { - styles.add(CheckstyleConfigLoader.loadCheckstyleConfig(is, emptyMap())); - } - } - } else { + Xpp3Dom xmlCheckstyleRules = xmlCheckstyleConf.getChild("checkstyleRules"); + if (xmlConfigLocation != null) { // resolve location against plugin location (could be in parent pom) Path configPath = Paths.get(checkstylePlugin.getLocation("").getSource().getLocation()) .resolveSibling(xmlConfigLocation.getValue()); if (configPath.toFile().exists()) { - styles.add(CheckstyleConfigLoader.loadCheckstyleConfig(configPath, emptyMap())); + styles.add(loadCheckstyleConfig(configPath, emptyMap())); + } + } else if (xmlCheckstyleRules != null && xmlCheckstyleRules.getChildCount() > 0) { + styles.add(loadCheckstyleConfig(toCheckStyleDocument(xmlCheckstyleRules.getChild(0)), emptyMap())); + } else { + // When no config location is specified, the maven-checkstyle-plugin falls back on sun_checks.xml + try (InputStream is = Checker.class.getResourceAsStream("/sun_checks.xml")) { + if (is != null) { + styles.add(loadCheckstyleConfig(is, emptyMap())); + } } } } @@ -278,6 +289,18 @@ protected List loadStyles(MavenProject project, Environment env) { return styles; } + private @Language("XML") String toCheckStyleDocument(final Xpp3Dom dom) throws IOException { + StringWriter stringWriter = new StringWriter(); + + MXSerializer serializer = new MXSerializer(); + serializer.setOutput(stringWriter); + serializer.docdecl(CHECKSTYLE_DOCTYPE); + + dom.writeToSerializer("", serializer); + + return stringWriter.toString(); + } + protected Set getRecipeArtifactCoordinates() { if (computedRecipeArtifactCoordinates == null) { synchronized (this) { diff --git a/src/test/java/org/openrewrite/maven/RewriteRunIT.java b/src/test/java/org/openrewrite/maven/RewriteRunIT.java index a3d7da96..2c94b03a 100644 --- a/src/test/java/org/openrewrite/maven/RewriteRunIT.java +++ b/src/test/java/org/openrewrite/maven/RewriteRunIT.java @@ -49,6 +49,15 @@ void single_project(MavenExecutionResult result) { .anySatisfy(line -> assertThat(line).contains("org.openrewrite.java.format.AutoFormat")); } + @MavenTest + void checkstyle_inline_rules(MavenExecutionResult result) { + assertThat(result) + .isSuccessful() + .out() + .warn() + .noneSatisfy(line -> assertThat(line).contains("Unable to parse checkstyle configuration")); + } + @MavenTest void recipe_project(MavenExecutionResult result) { assertThat(result) diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/checkstyle_inline_rules/pom.xml b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/checkstyle_inline_rules/pom.xml new file mode 100644 index 00000000..171ae17a --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/checkstyle_inline_rules/pom.xml @@ -0,0 +1,64 @@ + + 4.0.0 + + org.openrewrite.maven + checkstyle_inline_rules + 1.0 + jar + RewriteRunIT#checkstyle_inline_rules + + + 1.8 + 1.8 + UTF-8 + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + org.openrewrite.java.format.AutoFormat + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.3.1 + + + verify-style + process-classes + + check + + + + + false + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/checkstyle_inline_rules/src/main/java/sample/SimplifyBooleanSample.java b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/checkstyle_inline_rules/src/main/java/sample/SimplifyBooleanSample.java new file mode 100644 index 00000000..6eed11ed --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/RewriteRunIT/checkstyle_inline_rules/src/main/java/sample/SimplifyBooleanSample.java @@ -0,0 +1,20 @@ +package sample; + +public class SimplifyBooleanSample { + boolean ifNoElse() { + if (isOddMillis()) { + return true; + } + return false; + } + + static boolean isOddMillis() { + boolean even = System.currentTimeMillis() % 2 == 0; + if (even == true) { + return false; + } + else { + return true; + } + } +} \ No newline at end of file