Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse inline checkstyle config from maven-checkstyle-plugin #768

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,31 @@
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.*;
import java.util.stream.Collectors;
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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -248,26 +257,28 @@ protected List<NamedStyles> 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()));
}
}
}
}
Expand All @@ -278,6 +289,18 @@ protected List<NamedStyles> 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();
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
serializer.setOutput(stringWriter);
serializer.docdecl(CHECKSTYLE_DOCTYPE);
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

dom.writeToSerializer("", serializer);

return stringWriter.toString();
}

protected Set<String> getRecipeArtifactCoordinates() {
if (computedRecipeArtifactCoordinates == null) {
synchronized (this) {
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/openrewrite/maven/RewriteRunIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.openrewrite.maven</groupId>
<artifactId>checkstyle_inline_rules</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>RewriteRunIT#checkstyle_inline_rules</name>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.format.AutoFormat</recipe>
</activeRecipes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>verify-style</id>
<phase>process-classes</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<failsOnError>false</failsOnError>
<checkstyleRules>
<module name="Checker">
<module name="FileLength">
<property name="max" value="3500" />
<property name="fileExtensions" value="java"/>
</module>
<module name="FileTabCharacter"/>
<module name="TreeWalker">
<module name="StaticVariableName"/>
<module name="TypeName">
<property name="format" value="^_?[A-Z][a-zA-Z0-9]*$"/>
</module>
</module>
</module>
</checkstyleRules>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
}