Skip to content

Commit 0d03e9c

Browse files
committed
Implemented Checkstyle to project
1 parent 44f96fe commit 0d03e9c

File tree

15 files changed

+509
-0
lines changed

15 files changed

+509
-0
lines changed

.github/workflows/ci.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
strategy:
12+
matrix:
13+
platform: [ubuntu-latest, macos-latest, windows-latest]
14+
runs-on: ${{ matrix.platform }}
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Setup Maven cache
20+
uses: actions/cache@v4
21+
with:
22+
path: ~/.m2/repository
23+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
24+
restore-keys: |
25+
${{ runner.os }}-maven-
26+
27+
- name: Set up JDK 11
28+
uses: actions/setup-java@v4
29+
with:
30+
java-version: '11'
31+
distribution: 'corretto'
32+
33+
- name: Build with Maven
34+
run: mvn -e --no-transfer-progress clean install

.gitignore

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Eclipse project files
2+
.settings
3+
.externalToolBuilders
4+
.classpath
5+
.project
6+
7+
# m2e-code-quality Eclipse IDE plugin temporary configuration files for Eclipse CS Checkstyle / PMD / SpotBugs Plug-Ins
8+
.checkstyle
9+
.pmd
10+
.pmdruleset.xml
11+
.fbExcludeFilterFile
12+
13+
# NetBeans project files
14+
nbactions.xml
15+
nb-configuration.xml
16+
17+
# Maven build folder
18+
target
19+
bin
20+
21+
# Maven Wrapper (modern setup — do not include jar/downloader)
22+
.mvn/wrapper/maven-wrapper.jar
23+
.mvn/wrapper/MavenWrapperDownloader.java
24+
25+
# IDEA project files
26+
checkstyle.iml
27+
checkstyle.ipr
28+
checkstyle.iws
29+
.idea
30+
31+
# Vscode project files
32+
.vscode
33+
34+
# Temp files
35+
*~
36+
37+
# Java Virtual machine crash logs
38+
hs_err_pid*
39+
replay_pid*
40+
41+
# Apple MAC OSX hidden file
42+
.DS_Store
43+
44+
# NonDex files
45+
.nondex
46+
47+
# temp folder for files/folders of ci validations
48+
.ci-temp
49+
50+
# OpenRewrite-generated impl files
51+
*.iml

config/checkstyle.properties

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
checkstyle.suppressions.file=config/suppressions.xml
2+
checkstyle.suppressions-xpath.file=config/suppressions.xml
3+
checkstyle.header.file=https://raw.githubusercontent.com/checkstyle/checkstyle/master/config/java.header
4+
checkstyle.regexp.header.file=https://raw.githubusercontent.com/checkstyle/checkstyle/master/config/java-regexp.header
5+
checkstyle.importcontrol.file=config/import-control.xml
6+
checkstyle.importcontroltest.file=config/import-control-test.xml
7+
checkstyle.java.version=11

config/import-control-test.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE import-control PUBLIC
3+
"-//Checkstyle//DTD ImportControl Configuration 1.3//EN"
4+
"http://checkstyle.sourceforge.net/dtds/import_control_1_3.dtd">
5+
6+
<import-control pkg="org.checkstyle.autofix">
7+
<allow pkg="org.junit"/>
8+
<allow pkg="org.openrewrite"/>
9+
<allow pkg="org.checkstyle"/>
10+
<allow pkg="java.io"/>
11+
<allow pkg="java.nio"/>
12+
</import-control>

config/import-control.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE import-control PUBLIC
3+
"-//Checkstyle//DTD ImportControl Configuration 1.2//EN"
4+
"http://checkstyle.sourceforge.net/dtds/import_control_1_2.dtd">
5+
6+
<import-control pkg="org.checkstyle.autofix" regex="true">
7+
<allow pkg="org.openrewrite"/>
8+
<allow pkg="org.openrewrite.java"/>
9+
<allow pkg="org.openrewrite.java.tree"/>
10+
<allow pkg="java"/>
11+
<allow pkg="javax"/>
12+
<allow pkg="org.checkstyle"/>
13+
<allow pkg="java.io"/>
14+
<allow pkg="java.net"/>
15+
<allow pkg="java.nio"/>
16+
<allow pkg="java.util"/>
17+
<allow pkg="javax.xml.parsers"/>
18+
<allow pkg="org.apache.commons.beanutils"/>
19+
<allow pkg="org.apache.commons.logging"/>
20+
<allow pkg="org.xml.sax"/>
21+
22+
23+
</import-control>

config/suppressions.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
3+
<!DOCTYPE suppressions PUBLIC
4+
"-//Checkstyle//DTD SuppressionFilter Configuration 1.1//EN"
5+
"https://checkstyle.org/dtds/suppressions_1_1.dtd">
6+
7+
<suppressions>
8+
<suppress checks="HeaderCheck" files=".*"/>
9+
<suppress checks="RegexpHeader" files=".*"/>
10+
<suppress checks="header" files=".*\.java"/>
11+
<suppress checks="multiFileRegexpHeader" files=".*"/>
12+
<suppress checks="JavadocPackage" files=".*"/>
13+
</suppressions>

pom.xml

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>org.checkstyle.autofix</groupId>
9+
<artifactId>checkstyle-openrewrite-recipes</artifactId>
10+
<version>1.0.0</version>
11+
<packaging>jar</packaging>
12+
13+
<name>checkstyle openrewrite recipes</name>
14+
<description>Automatically fix Checkstyle violations with OpenRewrite</description>
15+
<url>https://github.com/checkstyle/checkstyle-openrewrite-recipes</url>
16+
17+
<properties>
18+
<maven.compiler.source>11</maven.compiler.source>
19+
<maven.compiler.target>11</maven.compiler.target>
20+
<maven.compiler.plugin>3.11.0</maven.compiler.plugin>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
23+
<rewrite.version>8.54.0</rewrite.version>
24+
<recipe.bom.version>3.9.0</recipe.bom.version>
25+
<junit.version>5.13.0</junit.version>
26+
<assertj.version>3.24.2</assertj.version>
27+
28+
<!-- Checkstyle properties -->
29+
<maven.checkstyle.plugin.version>3.6.0</maven.checkstyle.plugin.version>
30+
<checkstyle.version>10.25.0</checkstyle.version>
31+
</properties>
32+
33+
<dependencyManagement>
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.openrewrite.recipe</groupId>
37+
<artifactId>rewrite-recipe-bom</artifactId>
38+
<version>${recipe.bom.version}</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
</dependencies>
43+
</dependencyManagement>
44+
45+
<dependencies>
46+
<!-- OpenRewrite core dependencies - using consistent versions -->
47+
<dependency>
48+
<groupId>org.openrewrite</groupId>
49+
<artifactId>rewrite-java</artifactId>
50+
<version>${rewrite.version}</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.openrewrite</groupId>
54+
<artifactId>rewrite-java-11</artifactId>
55+
<version>${rewrite.version}</version>
56+
</dependency>
57+
58+
<!-- Test dependencies -->
59+
<dependency>
60+
<groupId>org.openrewrite</groupId>
61+
<artifactId>rewrite-test</artifactId>
62+
<version>${rewrite.version}</version>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.junit.jupiter</groupId>
67+
<artifactId>junit-jupiter</artifactId>
68+
<version>${junit.version}</version>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.assertj</groupId>
73+
<artifactId>assertj-core</artifactId>
74+
<version>${assertj.version}</version>
75+
<scope>test</scope>
76+
</dependency>
77+
</dependencies>
78+
79+
<build>
80+
<plugins>
81+
<!-- Compiler plugin -->
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-compiler-plugin</artifactId>
85+
<version>${maven.compiler.plugin}</version>
86+
<configuration>
87+
<source>${maven.compiler.source}</source>
88+
<target>${maven.compiler.target}</target>
89+
<encoding>${project.build.sourceEncoding}</encoding>
90+
</configuration>
91+
</plugin>
92+
93+
<!-- Surefire plugin for running tests -->
94+
<plugin>
95+
<groupId>org.apache.maven.plugins</groupId>
96+
<artifactId>maven-surefire-plugin</artifactId>
97+
<version>3.2.2</version>
98+
<configuration>
99+
<systemPropertyVariables>
100+
<org.slf4j.simpleLogger.log.org.openrewrite>debug</org.slf4j.simpleLogger.log.org.openrewrite>
101+
</systemPropertyVariables>
102+
</configuration>
103+
</plugin>
104+
105+
<!-- Checkstyle plugin for code style regulation -->
106+
<plugin>
107+
<groupId>org.apache.maven.plugins</groupId>
108+
<artifactId>maven-checkstyle-plugin</artifactId>
109+
<version>${maven.checkstyle.plugin.version}</version>
110+
<dependencies>
111+
<dependency>
112+
<groupId>com.puppycrawl.tools</groupId>
113+
<artifactId>checkstyle</artifactId>
114+
<version>${checkstyle.version}</version>
115+
</dependency>
116+
</dependencies>
117+
<executions>
118+
<execution>
119+
<id>check</id>
120+
<goals>
121+
<goal>check</goal>
122+
</goals>
123+
<configuration>
124+
<includeResources>false</includeResources>
125+
<includeTestResources>false</includeTestResources>
126+
<includeTestSourceDirectory>true</includeTestSourceDirectory>
127+
<configLocation>
128+
https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-${checkstyle.version}/config/checkstyle-checks.xml
129+
</configLocation>
130+
<propertiesLocation>config/checkstyle.properties</propertiesLocation>
131+
<failOnViolation>true</failOnViolation>
132+
<logViolationsToConsole>true</logViolationsToConsole>
133+
<maxAllowedViolations>0</maxAllowedViolations>
134+
<violationSeverity>error</violationSeverity>
135+
<outputFileFormat>xml</outputFileFormat>
136+
<outputFile>
137+
${project.build.directory}/checkstyle/checkstyle-report.xml
138+
</outputFile>
139+
</configuration>
140+
</execution>
141+
</executions>
142+
</plugin>
143+
</plugins>
144+
</build>
145+
146+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.checkstyle.autofix;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
import org.checkstyle.autofix.recipe.UpperEllRecipe;
7+
import org.openrewrite.Recipe;
8+
9+
/**
10+
* Main recipe that automatically fixes all supported Checkstyle violations.
11+
*/
12+
public class CheckstyleAutoFix extends Recipe {
13+
14+
@Override
15+
public String getDisplayName() {
16+
return "Checkstyle autoFix";
17+
}
18+
19+
@Override
20+
public String getDescription() {
21+
return "Automatically fixes Checkstyle violations.";
22+
}
23+
24+
@Override
25+
public List<Recipe> getRecipeList() {
26+
return Collections.singletonList(
27+
28+
new UpperEllRecipe()
29+
);
30+
}
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.checkstyle.autofix.recipe;
2+
3+
import org.openrewrite.ExecutionContext;
4+
import org.openrewrite.Recipe;
5+
import org.openrewrite.TreeVisitor;
6+
import org.openrewrite.java.JavaIsoVisitor;
7+
import org.openrewrite.java.tree.J;
8+
import org.openrewrite.java.tree.JavaType;
9+
10+
/**
11+
* Fixes Checkstyle UpperEll violations by replacing lowercase 'l' suffix
12+
* in long literals with uppercase 'L'.
13+
*/
14+
public class UpperEllRecipe extends Recipe {
15+
16+
@Override
17+
public String getDisplayName() {
18+
return "UpperEll recipe";
19+
}
20+
21+
@Override
22+
public String getDescription() {
23+
return "Replace lowercase 'l' suffix in long literals with uppercase 'L' "
24+
+ "to improve readability.";
25+
}
26+
27+
@Override
28+
public TreeVisitor<?, ExecutionContext> getVisitor() {
29+
return new UpperEllVisitor();
30+
}
31+
32+
/**
33+
* Visitor that replaces lowercase 'l' suffixes in long literals with uppercase 'L'.
34+
*/
35+
private static final class UpperEllVisitor extends JavaIsoVisitor<ExecutionContext> {
36+
@Override
37+
public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
38+
J.Literal result = super.visitLiteral(literal, ctx);
39+
final String valueSource = result.getValueSource();
40+
41+
if (valueSource != null && valueSource.endsWith("l")
42+
&& result.getType() == JavaType.Primitive.Long) {
43+
final String numericPart = valueSource.substring(0, valueSource.length() - 1);
44+
final String newValueSource = numericPart + "L";
45+
result = result.withValueSource(newValueSource);
46+
}
47+
return result;
48+
}
49+
50+
}
51+
}

0 commit comments

Comments
 (0)