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

feat: Add support for Checkstyle Configuration file properties #700

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
20c1561
test: Added a blank test class for Checkstyle Properties
SaptarshiSarkar12 Dec 29, 2023
871b629
Merge branch 'main' into 699-add-checkstyle-props-support
SaptarshiSarkar12 Jan 3, 2024
e29924c
feat: Added checkstyle config files and created a test method for the…
SaptarshiSarkar12 Jan 3, 2024
15352d3
Merge branch 'main' into 699-add-checkstyle-props-support
SaptarshiSarkar12 Jan 10, 2024
615c228
Merge branch 'main' into 699-add-checkstyle-props-support
timtebeek Jan 16, 2024
63899e0
Update src/test/java/org/openrewrite/maven/CheckstylePropertiesTest.java
timtebeek Jan 16, 2024
7752c02
Ensure build is successful
timtebeek Jan 16, 2024
74b3256
Rename to CheckstylePropertiesIT
timtebeek Jan 16, 2024
cd835fc
Use RELEASE for rewrite-static-analysis version
timtebeek Jan 16, 2024
df3b2d2
Merge branch 'main' into 699-add-checkstyle-props-support
SaptarshiSarkar12 Jan 20, 2024
07730de
Merge branch 'main' into 699-add-checkstyle-props-support
SaptarshiSarkar12 Jan 28, 2024
a3050d8
fix: Added suppressions file
SaptarshiSarkar12 Jan 28, 2024
22f67d8
Merge branch 'main' into 699-add-checkstyle-props-support
SaptarshiSarkar12 Feb 10, 2024
c6a8be7
Merge branch 'main' of github.com:openrewrite/rewrite-maven-plugin in…
SaptarshiSarkar12 Apr 12, 2024
11bca1e
feat: Added methods to extract and replace Checkstyle Properties from…
SaptarshiSarkar12 Apr 12, 2024
b8fa209
Rename resources-its folder to match test class
timtebeek Apr 25, 2024
42d11cb
Merge branch 'main' into 699-add-checkstyle-props-support
SaptarshiSarkar12 Apr 25, 2024
6f5bf37
Merge branch 'main' into 699-add-checkstyle-props-support
timtebeek May 20, 2024
8c148be
Do not fail test project build on known violation
timtebeek May 20, 2024
3471a25
Use configuration instead of execution in test pom.xml
timtebeek May 20, 2024
689b08e
Merge branch 'main' into 699-add-checkstyle-props-support
SaptarshiSarkar12 Aug 27, 2024
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
27 changes: 26 additions & 1 deletion src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.io.StringWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -95,6 +96,17 @@ public abstract class ConfigurableRewriteMojo extends AbstractMojo {
@Parameter(property = "rewrite.checkstyleDetectionEnabled", alias = "checkstyleDetectionEnabled", defaultValue = "true")
protected boolean checkstyleDetectionEnabled;

@Nullable
@Parameter(property = "rewrite.checkstyleProperties", alias = "checkstyleProperties")
private LinkedHashSet<String> checkstyleProperties;
/**
* @deprecated Use {@code rewrite.checkstyleProperties} instead.
*/
@Nullable
@Parameter(property = "checkstyleProperties")
@Deprecated
private LinkedHashSet<String> deprecatedCheckstyleProperties;

@Nullable
@Parameter(property = "rewrite.exclusions")
private LinkedHashSet<String> exclusions;
Expand All @@ -103,6 +115,10 @@ protected Set<String> getExclusions() {
return getCleanedSet(exclusions);
}

protected Set<String> getCheckstyleProperties() {
return getMergedAndCleaned(checkstyleProperties, deprecatedCheckstyleProperties);
}

@Nullable
@Parameter(property = "rewrite.plainTextMasks")
private LinkedHashSet<String> plainTextMasks;
Expand Down Expand Up @@ -235,7 +251,16 @@ 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(loadCheckstyleConfig(Paths.get(checkstyleConfigFile), emptyMap()));
// Convert the checkstyle config file contents to a String
String checkstyleConfig = new String(Files.readAllBytes(Paths.get(checkstyleConfigFile)));
Set<String> checkstyleProperties = getCheckstyleProperties();
if (!checkstyleProperties.isEmpty()) {
checkstyleConfig = checkstyleProperties.stream()
.map(s -> "-P" + s)
.collect(Collectors.joining("\n", "", "\n")) + checkstyleConfig;
}
styles.add(loadCheckstyleConfig(checkstyleConfig, emptyMap()));
// styles.add(loadCheckstyleConfig(Paths.get(checkstyleConfigFile), emptyMap()));
} else if (checkstyleDetectionEnabled && checkstylePlugin != null) {
Object checkstyleConfRaw = checkstylePlugin.getConfiguration();
if (checkstyleConfRaw instanceof Xpp3Dom) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/openrewrite/maven/CheckstylePropertiesIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.maven;

import com.soebes.itf.jupiter.extension.*;
import com.soebes.itf.jupiter.maven.MavenExecutionResult;

import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat;

@MavenJupiterExtension
@MavenOption(MavenCLIOptions.NO_TRANSFER_PROGRESS)
@MavenOption(MavenCLIExtra.MUTE_PLUGIN_VALIDATION_WARNING)
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:run")
class CheckstylePropertiesIT {
@MavenTest
void checkstyle_properties(MavenExecutionResult result) {
assertThat(result)
.isSuccessful();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress files="ImportOrderSample.java" checks="AvoidEscapedUnicodeCharacters"/>
</suppressions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">

<module name="Checker">

<property name="localeLanguage" value="en"/>

<module name="SuppressWarningsFilter" />
<module name="TreeWalker">
<!-- code cleanup -->
<module name="UnusedImports">
<property name="processJavadoc" value="true" />
</module>
<module name="RedundantImport"/>
<module name="IllegalImport" />
<module name="EqualsHashCode"/>
<module name="SimplifyBooleanExpression"/>
<module name="OneStatementPerLine"/>
<module name="UnnecessaryParentheses" />
<module name="SimplifyBooleanReturn"/>

<!-- style -->
<module name="DefaultComesLast"/>
<module name="EmptyStatement"/>
<module name="ArrayTypeStyle"/>
<module name="UpperEll"/>
<module name="LeftCurly"/>
<module name="RightCurly"/>
<module name="EmptyStatement"/>
<module name="ConstantName">
<property name="format" value="(^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$)"/>
</module>
<module name="LocalVariableName"/>
<module name="LocalFinalVariableName"/>
<module name="MemberName"/>
<module name="ClassTypeParameterName">
<property name="format" value="^[A-Z][a-zA-Z0-9]*$$"/>
</module>
<module name="MethodTypeParameterName">
<property name="format" value="^[A-Z][a-zA-Z0-9]*$$"/>
</module>
<module name="InterfaceTypeParameterName">
<property name="format" value="^[A-Z][a-zA-Z0-9]*$$"/>
</module>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName"/>
<module name="AvoidStarImport"/>

<!-- dependencies and import control -->
<module name="ImportControl">
<property name="file" value="${importControlFile}"/>
<!-- we do not want to validate test classes -->
<property name="path" value="^.*[\\/]src[\\/]main[\\/].*$"/>
</module>

<!-- whitespace -->
<module name="GenericWhitespace"/>
<module name="NoWhitespaceBefore"/>
<module name="WhitespaceAfter" />
<module name="NoWhitespaceAfter"/>
<module name="WhitespaceAround">
<property name="allowEmptyConstructors" value="true"/>
<property name="allowEmptyMethods" value="true"/>
</module>
<module name="Indentation"/>
<module name="MethodParamPad"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>

<!-- locale-sensitive methods should specify locale -->
<module name="Regexp">
<property name="format" value="\.to(Lower|Upper)Case\(\)"/>
<property name="illegalPattern" value="true"/>
<property name="ignoreComments" value="true"/>
</module>

<!-- code quality -->
<module name="MethodLength"/>
<module name="ParameterNumber">
<!-- default is 8 -->
<property name="max" value="13"/>
</module>
<module name="ClassDataAbstractionCoupling">
<!-- default is 7 -->
<property name="max" value="20"/>
</module>
<module name="BooleanExpressionComplexity">
<!-- default is 3 -->
<property name="max" value="5"/>
</module>

<module name="ClassFanOutComplexity">
<!-- default is 20 -->
<property name="max" value="44"/>
</module>
<module name="CyclomaticComplexity">
<!-- default is 10-->
<property name="max" value="19"/>
</module>
<module name="JavaNCSS">
<!-- default is 50 -->
<property name="methodMaximum" value="100"/>
</module>
<module name="NPathComplexity">
<!-- default is 200 -->
<property name="max" value="5832"/>
</module>

<module name="IllegalToken">
<property name="tokens" value="LITERAL_ASSERT"/>
</module>

<!-- Make the @SuppressWarnings annotations available to Checkstyle -->
<module name="SuppressWarningsHolder" />
</module>

<module name="SuppressionFilter">
<property name="file" value="${checkstyle.suppressions.file}"/>
</module>

<!-- Filter out Checkstyle warnings that have been suppressed with the @SuppressWarnings annotation -->
<module name="SuppressWarningsFilter" />
</module>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE import-control PUBLIC
"-//Checkstyle//DTD ImportControl Configuration 1.4//EN"
"https://checkstyle.org/dtds/import_control_1_4.dtd">

<import-control pkg="io.strimzi" strategyOnMismatch="allowed">
<allow pkg="java.util"/>
<allow pkg="java.net"/>
</import-control>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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_properties</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>CheckstylePropertiesTest#checkstyle_properties</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.staticanalysis.CodeCleanup</recipe>
</activeRecipes>
<checkstyleProperties>
<property name="importControlFile" value="${project.basedir}/.checkstyle/import-control.xml"/>
</checkstyleProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-static-analysis</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<configLocation>.checkstyle/checkstyle.xml</configLocation>
<consoleOutput>true</consoleOutput>
<failsOnError>false</failsOnError>
<propertyExpansion>importControlFile=${project.basedir}/.checkstyle/import-control.xml</propertyExpansion>
<suppressionsLocation>.checkstyle/checkstyle-suppressions.xml</suppressionsLocation>
</configuration>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.12.7</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package sample;

import java.net.URL;
import java.util.ArrayList;

public class ImportOrderSample {
public void useUtilPackage() {
ArrayList<String> list = new ArrayList<>();
list.add("Hello");
list.add("World");
for (String s : list) {
System.out.println(s);
}
}

public void useNetPackage() {
try {
URL url = new URL("https://github.com");
url.openConnection();
} catch (Exception e) {
System.out.println("Failed to connect to github.com! " + e.getMessage());
}
}
}