Skip to content

Commit d33c99a

Browse files
committed
implemented checkstyle in project
1 parent 9c20bb1 commit d33c99a

File tree

8 files changed

+120
-32
lines changed

8 files changed

+120
-32
lines changed

config/checkstyle.properties

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

config/import-control-test.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
</import-control>

config/import-control.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+
<!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="org.checkstyle"/>
12+
<allow pkg="java.util"/>
13+
</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: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<recipe.bom.version>3.9.0</recipe.bom.version>
2525
<junit.version>5.13.0</junit.version>
2626
<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>
2731
</properties>
2832

2933
<dependencyManagement>
@@ -97,9 +101,46 @@
97101
</systemPropertyVariables>
98102
</configuration>
99103
</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>
100143
</plugins>
101144
</build>
102145

103-
104-
105146
</project>

src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package org.checkstyle.autofix;
22

3-
import org.openrewrite.Recipe;
4-
53
import java.util.Collections;
64
import java.util.List;
75

86
import org.checkstyle.autofix.recipe.UpperEllRecipe;
7+
import org.openrewrite.Recipe;
98

109
/**
11-
* Main recipe that automatically fixes all supported Checkstyle violations
10+
* Main recipe that automatically fixes all supported Checkstyle violations.
1211
*/
1312
public class CheckstyleAutoFix extends Recipe {
1413

@@ -26,7 +25,7 @@ public String getDescription() {
2625
public List<Recipe> getRecipeList() {
2726
return Collections.singletonList(
2827

29-
new UpperEllRecipe()
28+
new UpperEllRecipe()
3029
);
3130
}
32-
}
31+
}

src/main/java/org/checkstyle/autofix/recipe/UpperEllRecipe.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,29 @@ public String getDisplayName() {
2020

2121
@Override
2222
public String getDescription() {
23-
return "Replace lowercase 'l' suffix in long literals with uppercase 'L' to improve readability.";
23+
return "Replace lowercase 'l' suffix in long literals with uppercase 'L' "
24+
+ "to improve readability.";
2425
}
2526

2627
@Override
2728
public TreeVisitor<?, ExecutionContext> getVisitor() {
2829
return new UpperEllVisitor();
2930
}
3031

31-
private static class UpperEllVisitor extends JavaIsoVisitor<ExecutionContext> {
32+
/**
33+
* Visitor that replaces lowercase 'l' suffixes in long literals with uppercase 'L'.
34+
*/
35+
private static final class UpperEllVisitor extends JavaIsoVisitor<ExecutionContext> {
3236
@Override
3337
public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
3438
J.Literal result = super.visitLiteral(literal, ctx);
35-
String valueSource = result.getValueSource();
39+
final String valueSource = result.getValueSource();
3640

3741
if (valueSource != null && valueSource.endsWith("l")
38-
&& result.getType() == JavaType.Primitive.Long) {
39-
String numericPart = valueSource.substring(0, valueSource.length() - 1);
40-
String newValueSource = numericPart + "L";
41-
result = result.withValueSource(newValueSource);
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);
4246
}
4347
return result;
4448
}

src/test/java/org/checkstyle/autofix/recipe/UpperEllRecipeTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package org.checkstyle.autofix.recipe;
22

3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
import static org.openrewrite.java.Assertions.java;
5+
36
import org.junit.jupiter.api.Test;
47
import org.openrewrite.test.RecipeSpec;
58
import org.openrewrite.test.RewriteTest;
69

7-
import static org.junit.jupiter.api.Assertions.assertTrue;
8-
import static org.openrewrite.java.Assertions.java;
9-
1010
public class UpperEllRecipeTest implements RewriteTest {
1111

1212
@Override
@@ -15,24 +15,24 @@ public void defaults(RecipeSpec spec) {
1515
}
1616

1717
@Test
18-
void fixesLowercaseLInLongLiteralsFromResources() {
18+
void fixesLowercase() {
1919
rewriteRun(
2020

2121
java(
22-
"class Test {\n" +
23-
" int value1 = 123l;\n" +
24-
" long value2 = 0x123l;\n" +
25-
" long value3 = 0123l;\n" +
26-
" long value4 = 0b101l;\n" +
27-
" String value5 = null;\n" +
28-
"}\n",
29-
"class Test {\n" +
30-
" int value1 = 123L;\n" +
31-
" long value2 = 0x123L;\n" +
32-
" long value3 = 0123L;\n" +
33-
" long value4 = 0b101L;\n" +
34-
" String value5 = null;\n" +
35-
"}\n"
22+
"class Test {\n"
23+
+ " int value1 = 123l;\n"
24+
+ " long value2 = 0x123l;\n"
25+
+ " long value3 = 0123l;\n"
26+
+ " long value4 = 0b101l;\n"
27+
+ " String value5 = null;\n"
28+
+ "}\n",
29+
"class Test {\n"
30+
+ " int value1 = 123L;\n"
31+
+ " long value2 = 0x123L;\n"
32+
+ " long value3 = 0123L;\n"
33+
+ " long value4 = 0b101L;\n"
34+
+ " String value5 = null;\n"
35+
+ "}\n"
3636
)
3737
);
3838
assertTrue(true, "Test completed successfully");

0 commit comments

Comments
 (0)