Skip to content
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
8 changes: 8 additions & 0 deletions config/checkstyle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

checkstyle.suppressions.file=config/suppressions.xml
checkstyle.suppressions-xpath.file=config/suppressions.xml
checkstyle.header.file=https://raw.githubusercontent.com/checkstyle/checkstyle/master/config/java.header
checkstyle.regexp.header.file=https://raw.githubusercontent.com/checkstyle/checkstyle/master/config/java-regexp.header
checkstyle.importcontrol.file=config/import-control.xml
checkstyle.importcontroltest.file=config/import-control-test.xml
checkstyle.java.version=11
10 changes: 10 additions & 0 deletions config/import-control-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!DOCTYPE import-control PUBLIC
"-//Checkstyle//DTD ImportControl Configuration 1.3//EN"
"http://checkstyle.sourceforge.net/dtds/import_control_1_3.dtd">

<import-control pkg="org.checkstyle.autofix">
<allow pkg="org.junit"/>
<allow pkg="org.openrewrite"/>
<allow pkg="org.checkstyle"/>
</import-control>
13 changes: 13 additions & 0 deletions config/import-control.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<!DOCTYPE import-control PUBLIC
"-//Checkstyle//DTD ImportControl Configuration 1.2//EN"
"http://checkstyle.sourceforge.net/dtds/import_control_1_2.dtd">

<import-control pkg="org.checkstyle.autofix" regex="true">
<allow pkg="org.openrewrite"/>
<allow pkg="org.openrewrite.java"/>
<allow pkg="org.openrewrite.java.tree"/>
<allow pkg="java"/>
<allow pkg="org.checkstyle"/>
<allow pkg="java.util"/>
</import-control>
13 changes: 13 additions & 0 deletions config/suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.1//EN"
"https://checkstyle.org/dtds/suppressions_1_1.dtd">

<suppressions>
<suppress checks="HeaderCheck" files=".*"/>
<suppress checks="RegexpHeader" files=".*"/>
<suppress checks="header" files=".*\.java"/>
<suppress checks="multiFileRegexpHeader" files=".*"/>
Comment on lines +8 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for now as we are in exploration mode. Please investigate whether we need to include a license header given the license for this project and create issue to update all files and remove this supressions. When project is stabilised, that will be a good time fix this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.
created: #17

<suppress checks="JavadocPackage" files=".*"/>
</suppressions>
45 changes: 43 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<recipe.bom.version>3.9.0</recipe.bom.version>
<junit.version>5.13.0</junit.version>
<assertj.version>3.24.2</assertj.version>

<!-- Checkstyle properties -->
<maven.checkstyle.plugin.version>3.6.0</maven.checkstyle.plugin.version>
<checkstyle.version>10.25.0</checkstyle.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -97,9 +101,46 @@
</systemPropertyVariables>
</configuration>
</plugin>

<!-- Checkstyle plugin for code style regulation -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven.checkstyle.plugin.version}</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<includeResources>false</includeResources>
<includeTestResources>false</includeTestResources>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<configLocation>
https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-${checkstyle.version}/config/checkstyle-checks.xml
</configLocation>
<propertiesLocation>config/checkstyle.properties</propertiesLocation>
<failOnViolation>true</failOnViolation>
<logViolationsToConsole>true</logViolationsToConsole>
<maxAllowedViolations>0</maxAllowedViolations>
<violationSeverity>error</violationSeverity>
<outputFileFormat>xml</outputFileFormat>
<outputFile>
${project.build.directory}/checkstyle/checkstyle-report.xml
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>



</project>
9 changes: 4 additions & 5 deletions src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.checkstyle.autofix;

import org.openrewrite.Recipe;

import java.util.Collections;
import java.util.List;

import org.checkstyle.autofix.recipe.UpperEllRecipe;
import org.openrewrite.Recipe;

/**
* Main recipe that automatically fixes all supported Checkstyle violations
* Main recipe that automatically fixes all supported Checkstyle violations.
*/
public class CheckstyleAutoFix extends Recipe {

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

new UpperEllRecipe()
new UpperEllRecipe()
);
}
}
}
18 changes: 11 additions & 7 deletions src/main/java/org/checkstyle/autofix/recipe/UpperEllRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,29 @@ public String getDisplayName() {

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

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new UpperEllVisitor();
}

private static class UpperEllVisitor extends JavaIsoVisitor<ExecutionContext> {
/**
* Visitor that replaces lowercase 'l' suffixes in long literals with uppercase 'L'.
*/
private static final class UpperEllVisitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
J.Literal result = super.visitLiteral(literal, ctx);
String valueSource = result.getValueSource();
final String valueSource = result.getValueSource();

if (valueSource != null && valueSource.endsWith("l")
&& result.getType() == JavaType.Primitive.Long) {
String numericPart = valueSource.substring(0, valueSource.length() - 1);
String newValueSource = numericPart + "L";
result = result.withValueSource(newValueSource);
&& result.getType() == JavaType.Primitive.Long) {
final String numericPart = valueSource.substring(0, valueSource.length() - 1);
final String newValueSource = numericPart + "L";
result = result.withValueSource(newValueSource);
}
return result;
}
Expand Down
36 changes: 18 additions & 18 deletions src/test/java/org/checkstyle/autofix/recipe/UpperEllRecipeTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.checkstyle.autofix.recipe;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openrewrite.java.Assertions.java;

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openrewrite.java.Assertions.java;

public class UpperEllRecipeTest implements RewriteTest {

@Override
Expand All @@ -15,24 +15,24 @@ public void defaults(RecipeSpec spec) {
}

@Test
void fixesLowercaseLInLongLiteralsFromResources() {
void fixesLowercase() {
rewriteRun(

java(
"class Test {\n" +
" int value1 = 123l;\n" +
" long value2 = 0x123l;\n" +
" long value3 = 0123l;\n" +
" long value4 = 0b101l;\n" +
" String value5 = null;\n" +
"}\n",
"class Test {\n" +
" int value1 = 123L;\n" +
" long value2 = 0x123L;\n" +
" long value3 = 0123L;\n" +
" long value4 = 0b101L;\n" +
" String value5 = null;\n" +
"}\n"
"class Test {\n"
+ " int value1 = 123l;\n"
+ " long value2 = 0x123l;\n"
+ " long value3 = 0123l;\n"
+ " long value4 = 0b101l;\n"
+ " String value5 = null;\n"
+ "}\n",
"class Test {\n"
+ " int value1 = 123L;\n"
+ " long value2 = 0x123L;\n"
+ " long value3 = 0123L;\n"
+ " long value4 = 0b101L;\n"
+ " String value5 = null;\n"
+ "}\n"
)
);
assertTrue(true, "Test completed successfully");
Expand Down