Skip to content

Commit

Permalink
add a reference to JavaTemplates documentation on first usage in `N…
Browse files Browse the repository at this point in the history
…oGuavaListsNewArrayList` (#69)

* add a reference to `JavaTemplate`s documentation on first usage in `NoGuavaListsNewArrayList`

* migrate to new rewrite-rewrite for best practices
  • Loading branch information
MBoegers authored Feb 3, 2025
1 parent b8be50d commit 7abd255
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-recommendations</artifactId>
<version>1.14.1</version>
<artifactId>rewrite-rewrite</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>
</plugin>
Expand Down
2 changes: 1 addition & 1 deletion rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ name: com.yourorg.ApplyRecipeBestPractices
displayName: Apply Recipe best practices
description: Apply best practices to your recipes, through OpenRewrite's best practices recipe & IntelliJ IDEA plugin.
recipeList:
- org.openrewrite.recipes.OpenRewriteBestPractices
- org.openrewrite.recipes.rewrite.OpenRewriteRecipeBestPractices
4 changes: 4 additions & 0 deletions src/main/java/com/yourorg/NoGuavaListsNewArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
// To avoid stale state persisting between cycles, getVisitor() should always return a new instance of
// its visitor
new JavaVisitor<ExecutionContext>() {
// Java Templates are used to generate Java code easily.
// They use a syntax that expand Java with possible type-safe insertions points.
// See https://docs.openrewrite.org/concepts-and-explanations/javatemplate for full documentation
private final JavaTemplate newArrayList = JavaTemplate.builder("new ArrayList<>()")
.imports("java.util.ArrayList")
.build();
Expand All @@ -84,6 +87,7 @@ public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
System.out.printf(printed);

// You must always delegate to the super method to ensure the visitor continues to visit deeper
// return cu; // this leads to a recipe that makes no changes at all
return super.visitCompilationUnit(cu, ctx);
}

Expand Down

0 comments on commit 7abd255

Please sign in to comment.