Skip to content

Commit 00ff3ba

Browse files
timtebeekromani
authored andcommitted
Pull checkstyle#87: Apply OpenRewrite best practices
1 parent 8d4fdf6 commit 00ff3ba

File tree

7 files changed

+120
-28
lines changed

7 files changed

+120
-28
lines changed

pom.xml

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
<maven.compiler.plugin>3.11.0</maven.compiler.plugin>
2121
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2222

23-
<rewrite.version>8.54.0</rewrite.version>
24-
<rewrite.maven.plugin>6.11.0</rewrite.maven.plugin>
25-
<recipe.bom.version>3.9.0</recipe.bom.version>
26-
<junit.version>5.13.0</junit.version>
27-
<assertj.version>3.24.2</assertj.version>
23+
<rewrite.maven.plugin>6.16.0</rewrite.maven.plugin>
24+
<recipe.bom.version>3.13.0</recipe.bom.version>
25+
<junit.version>5.13.4</junit.version>
26+
<assertj.version>3.27.4</assertj.version>
2827

2928
<!-- Checkstyle properties -->
3029
<maven.checkstyle.plugin.version>3.6.0</maven.checkstyle.plugin.version>
@@ -56,12 +55,10 @@
5655
<dependency>
5756
<groupId>org.openrewrite</groupId>
5857
<artifactId>rewrite-java</artifactId>
59-
<version>${rewrite.version}</version>
6058
</dependency>
6159
<dependency>
6260
<groupId>org.openrewrite</groupId>
6361
<artifactId>rewrite-java-17</artifactId>
64-
<version>${rewrite.version}</version>
6562
</dependency>
6663

6764
<!-- JGit for generating diffs without external Git dependency -->
@@ -88,7 +85,6 @@
8885
<dependency>
8986
<groupId>org.openrewrite</groupId>
9087
<artifactId>rewrite-test</artifactId>
91-
<version>${rewrite.version}</version>
9288
<scope>test</scope>
9389
</dependency>
9490
<dependency>
@@ -182,10 +178,19 @@
182178
<exclusions>
183179
<exclusion>src/test/resources/**</exclusion>
184180
</exclusions>
185-
<recipeArtifactCoordinates>
186-
<coordinate>org.checkstyle.autofix:checkstyle-openrewrite-recipes:1.0.0</coordinate>
187-
</recipeArtifactCoordinates>
188181
</configuration>
182+
<dependencies>
183+
<dependency>
184+
<groupId>org.checkstyle.autofix</groupId>
185+
<artifactId>checkstyle-openrewrite-recipes</artifactId>
186+
<version>${project.version}</version>
187+
</dependency>
188+
<dependency>
189+
<groupId>org.openrewrite.recipe</groupId>
190+
<artifactId>rewrite-rewrite</artifactId>
191+
<version>RELEASE</version>
192+
</dependency>
193+
</dependencies>
189194
<executions>
190195
<execution>
191196
<id>checkstyle-autofix</id>

rewrite.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# mvn rewrite:run -Drewrite.activeRecipes=org.checkstyle.recipes.OpenRewriteRecipeBestPractices
2+
---
3+
type: specs.openrewrite.org/v1beta/recipe
4+
name: org.checkstyle.recipes.OpenRewriteRecipeBestPractices
5+
displayName: OpenRewrite recipe best practices
6+
description: Best practices for OpenRewrite recipe development.
7+
recipeList:
8+
- org.checkstyle.recipes.JavaRecipeBestPractices
9+
- org.checkstyle.recipes.RecipeTestingBestPracticesSubset
10+
- org.openrewrite.java.recipes.RecipeNullabilityBestPractices
11+
- org.openrewrite.java.recipes.ExamplesExtractor
12+
# - org.openrewrite.java.OrderImports
13+
- org.openrewrite.java.RemoveUnusedImports
14+
- org.openrewrite.java.SimplifySingleElementAnnotation
15+
- org.openrewrite.java.format.EmptyNewlineAtEndOfFile
16+
- org.openrewrite.java.format.RemoveTrailingWhitespace
17+
- org.openrewrite.staticanalysis.CompareEnumsWithEqualityOperator
18+
- org.openrewrite.staticanalysis.EqualsAvoidsNull
19+
- org.openrewrite.staticanalysis.InlineVariable
20+
# - org.openrewrite.staticanalysis.LambdaBlockToExpression
21+
- org.openrewrite.staticanalysis.MissingOverrideAnnotation
22+
- org.openrewrite.staticanalysis.OperatorWrap:
23+
wrapOption: NL # Differs from the stock OpenRewrite recipe which uses EOL
24+
- org.openrewrite.staticanalysis.RemoveRedundantNullCheckBeforeInstanceof
25+
- org.openrewrite.staticanalysis.RemoveUnusedLocalVariables
26+
- org.openrewrite.staticanalysis.RemoveUnusedPrivateFields
27+
- org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods
28+
#- org.openrewrite.staticanalysis.UnnecessaryThrows
29+
- org.openrewrite.staticanalysis.UnwrapElseAfterReturn
30+
- org.openrewrite.staticanalysis.UseDiamondOperator
31+
- org.openrewrite.java.NoStaticImport:
32+
methodPattern: java.util.stream.Collectors *(..)
33+
- org.openrewrite.java.NoStaticImport:
34+
methodPattern: java.util.Collections *(..)
35+
---
36+
type: specs.openrewrite.org/v1beta/recipe
37+
name: org.checkstyle.recipes.JavaRecipeBestPractices
38+
displayName: Java Recipe best practices
39+
description: Best practices for Java recipe development.
40+
preconditions:
41+
- org.openrewrite.java.search.FindTypes:
42+
fullyQualifiedTypeName: org.openrewrite.Recipe
43+
checkAssignability: true
44+
recipeList:
45+
- org.openrewrite.java.recipes.BlankLinesAroundFieldsWithAnnotations
46+
- org.openrewrite.java.recipes.CorrectlySpacedDescriptions
47+
# - org.openrewrite.java.recipes.ExecutionContextParameterName
48+
- org.openrewrite.java.recipes.IsLiteralNullRecipe
49+
- org.openrewrite.java.recipes.MissingOptionExample
50+
- org.openrewrite.java.recipes.NoMutableStaticFieldsInRecipes
51+
- org.openrewrite.java.recipes.RecipeEqualsAndHashCodeCallSuper
52+
- org.openrewrite.java.recipes.UseTreeRandomId
53+
- org.openrewrite.java.recipes.migrate.RemoveTraitsUsageRecipes
54+
- org.openrewrite.staticanalysis.NeedBraces
55+
- org.openrewrite.staticanalysis.RemoveSystemOutPrintln
56+
- org.openrewrite.java.RemoveAnnotation:
57+
annotationPattern: '@org.openrewrite.NlsRewrite.DisplayName'
58+
- org.openrewrite.java.RemoveAnnotation:
59+
annotationPattern: '@org.openrewrite.NlsRewrite.Description'
60+
---
61+
type: specs.openrewrite.org/v1beta/recipe
62+
name: org.checkstyle.recipes.RecipeTestingBestPracticesSubset
63+
displayName: Recipe testing best practices
64+
description: Best practices for testing recipes.
65+
preconditions:
66+
- org.openrewrite.java.search.FindTypes:
67+
fullyQualifiedTypeName: org.openrewrite.test.RewriteTest
68+
checkAssignability: true
69+
recipeList:
70+
- org.openrewrite.java.migrate.util.ReplaceStreamCollectWithToList:
71+
convertToList: true
72+
# - org.openrewrite.java.recipes.RewriteTestClassesShouldNotBePublic
73+
- org.openrewrite.java.recipes.SelectRecipeExamples
74+
- org.openrewrite.java.recipes.SingleDocumentExample
75+
- org.openrewrite.java.recipes.ReorderTestMethods
76+
- org.openrewrite.java.recipes.ReplaceNullWithDoesNotExist
77+
- org.openrewrite.java.recipes.SourceSpecTextBlockNewLine
78+
- org.openrewrite.java.recipes.SourceSpecTextBlockIndentation
79+
- org.openrewrite.java.recipes.UseRewriteTestDefaults
80+
- org.openrewrite.java.testing.cleanup.RemoveTestPrefix
81+
# - org.openrewrite.java.testing.cleanup.TestsShouldNotBePublic
82+
- org.openrewrite.staticanalysis.NeedBraces
83+
- org.openrewrite.staticanalysis.RemoveSystemOutPrintln
84+
- org.openrewrite.staticanalysis.ReorderAnnotations
85+
- org.openrewrite.java.UseStaticImport:
86+
methodPattern: 'org.openrewrite..Assertions *(..)'

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,18 @@ private final class LocalVariableVisitor extends JavaIsoVisitor<ExecutionContext
6464
private Path sourcePath;
6565

6666
@Override
67-
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
67+
public J.CompilationUnit visitCompilationUnit(
68+
J.CompilationUnit cu, ExecutionContext executionContext) {
6869
this.sourcePath = cu.getSourcePath();
69-
return super.visitCompilationUnit(cu, ctx);
70+
return super.visitCompilationUnit(cu, executionContext);
7071
}
7172

7273
@Override
7374
public J.VariableDeclarations visitVariableDeclarations(
74-
J.VariableDeclarations multiVariable, ExecutionContext ctx) {
75+
J.VariableDeclarations multiVariable, ExecutionContext executionContext) {
7576

7677
J.VariableDeclarations declarations = super.visitVariableDeclarations(multiVariable,
77-
ctx);
78+
executionContext);
7879

7980
if (!(getCursor().getParentTreeCursor().getValue() instanceof J.ClassDeclaration)
8081
&& declarations.getVariables().size() == 1

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ private static class HeaderVisitor extends JavaIsoVisitor<ExecutionContext> {
9999
}
100100

101101
@Override
102-
public J visit(Tree tree, ExecutionContext ctx) {
103-
J result = super.visit(tree, ctx);
102+
public J visit(Tree tree, ExecutionContext executionContext) {
103+
J result = super.visit(tree, executionContext);
104104

105105
if (tree instanceof JavaSourceFile) {
106106
JavaSourceFile sourceFile = (JavaSourceFile) tree;
@@ -113,7 +113,7 @@ public J visit(Tree tree, ExecutionContext ctx) {
113113
sourceFile = sourceFile.withPrefix(
114114
Space.format(fixedHeader));
115115
}
116-
result = super.visit(sourceFile, ctx);
116+
result = super.visit(sourceFile, executionContext);
117117
}
118118
return result;
119119
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ private final class UpperEllVisitor extends JavaIsoVisitor<ExecutionContext> {
6565
private Path sourcePath;
6666

6767
@Override
68-
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
68+
public J.CompilationUnit visitCompilationUnit(
69+
J.CompilationUnit cu, ExecutionContext executionContext) {
6970
this.sourcePath = cu.getSourcePath().toAbsolutePath();
70-
return super.visitCompilationUnit(cu, ctx);
71+
return super.visitCompilationUnit(cu, executionContext);
7172
}
7273

7374
@Override
74-
public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
75-
J.Literal result = super.visitLiteral(literal, ctx);
75+
public J.Literal visitLiteral(J.Literal literal, ExecutionContext executionContext) {
76+
J.Literal result = super.visitLiteral(literal, executionContext);
7677
final String valueSource = result.getValueSource();
7778

7879
if (valueSource != null && valueSource.endsWith(LOWERCASE_L)

src/test/java/org/checkstyle/autofix/RemoveViolationComments.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4747

4848
private static final class ViolationCommentRemover extends JavaIsoVisitor<ExecutionContext> {
4949
@Override
50-
public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
50+
public Space visitSpace(
51+
Space space, Space.Location loc, ExecutionContext executionContext) {
5152
String suffixAccumulator = null;
5253
final List<Comment> filteredComments = new ArrayList<>();
5354

@@ -86,7 +87,7 @@ public Space visitSpace(Space space, Space.Location loc, ExecutionContext ctx) {
8687
result = result.withWhitespace(suffixAccumulator);
8788
}
8889
}
89-
return super.visitSpace(result, loc, ctx);
90+
return super.visitSpace(result, loc, executionContext);
9091
}
9192
}
9293
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,8 @@ private Configuration getCheckConfigurations(String inputPath) throws Exception
175175
private String[] convertToExpectedMessages(List<CheckstyleViolation> violations) {
176176
return violations.stream()
177177
.map(violation -> {
178-
final String message;
179-
message = violation.getLine() + ":"
180-
+ violation.getColumn() + ": " + violation.getMessage();
181-
return message;
178+
return violation.getLine() + ":"
179+
+ violation.getColumn() + ": " + violation.getMessage();
182180
})
183181
.toArray(String[]::new);
184182
}

0 commit comments

Comments
 (0)