Skip to content

Commit

Permalink
Update refactoring guide with OpenRewrite8
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-solomon committed Jun 20, 2023
1 parent f7dad57 commit 5ce6120
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions authoring-recipes/writing-a-java-refactoring-recipe.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.*;
import org.openrewrite.internal.lang.NonNull;

// Making your recipe immutable helps make them idempotent and eliminates a variety of possible bugs.
Expand Down Expand Up @@ -215,7 +214,7 @@ public class SayHelloRecipe extends Recipe {
// ...

@Override
public JavaIsoVisitor<ExecutionContext> getVisitor() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
// getVisitor() should always return a new instance of the visitor to avoid any state leaking between cycles
return new SayHelloVisitor();
}
Expand Down Expand Up @@ -312,7 +311,7 @@ public class SayHelloRecipe extends Recipe {

public class SayHelloVisitor extends JavaIsoVisitor<ExecutionContext> {
private final JavaTemplate helloTemplate =
JavaTemplate.builder(this::getCursor, "public String hello() { return \"Hello from #{}!\"; }")
JavaTemplate.builder( "public String hello() { return \"Hello from #{}!\"; }")
.build();

@Override
Expand All @@ -332,7 +331,7 @@ public class SayHelloRecipe extends Recipe {

public class SayHelloVisitor extends JavaIsoVisitor<ExecutionContext> {
private final JavaTemplate helloTemplate =
JavaTemplate.builder(this::getCursor, "public String hello() { return \"Hello from #{}!\"; }")
JavaTemplate.builder( "public String hello() { return \"Hello from #{}!\"; }")
.build();

@Override
Expand All @@ -354,12 +353,9 @@ public class SayHelloRecipe extends Recipe {
}

// Interpolate the fullyQualifiedClassName into the template and use the resulting LST to update the class body
classDecl = classDecl.withBody(
classDecl.getBody().withTemplate(
helloTemplate,
classDecl.getBody().getCoordinates().lastStatement(),
fullyQualifiedClassName
));
classDecl = classDecl.withBody( helloTemplate.apply(new Cursor(getCursor(), classDecl.getBody()),
classDecl.getBody().getCoordinates().lastStatement(),
fullyQualifiedClassName ));

return classDecl;
}
Expand All @@ -374,11 +370,11 @@ With all of that done, the complete `SayHelloRecipe` looks like this:
```java
package com.yourorg;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.*;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
Expand All @@ -396,6 +392,12 @@ public class SayHelloRecipe extends Recipe {
@NonNull
String fullyQualifiedClassName;

// All recipes must be serializable. This is verified by RewriteTest.rewriteRun() in your tests.
@JsonCreator
public SayHelloRecipe(@NonNull @JsonProperty("fullyQualifiedClassName") String fullyQualifiedClassName) {
this.fullyQualifiedClassName = fullyQualifiedClassName;
}

@Override
public String getDisplayName() {
return "Say Hello";
Expand All @@ -407,14 +409,14 @@ public class SayHelloRecipe extends Recipe {
}

@Override
protected JavaIsoVisitor<ExecutionContext> getVisitor() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
// getVisitor() should always return a new instance of the visitor to avoid any state leaking between cycles
return new SayHelloVisitor();
}

public class SayHelloVisitor extends JavaIsoVisitor<ExecutionContext> {
private final JavaTemplate helloTemplate =
JavaTemplate.builder(this::getCursor, "public String hello() { return \"Hello from #{}!\"; }")
JavaTemplate.builder( "public String hello() { return \"Hello from #{}!\"; }")
.build();

@Override
Expand All @@ -436,12 +438,9 @@ public class SayHelloRecipe extends Recipe {
}

// Interpolate the fullyQualifiedClassName into the template and use the resulting LST to update the class body
classDecl = classDecl.withBody(
classDecl.getBody().withTemplate(
helloTemplate,
classDecl.getBody().getCoordinates().lastStatement(),
fullyQualifiedClassName
));
classDecl = classDecl.withBody( helloTemplate.apply(new Cursor(getCursor(), classDecl.getBody()),
classDecl.getBody().getCoordinates().lastStatement(),
fullyQualifiedClassName ));

return classDecl;
}
Expand Down

0 comments on commit 5ce6120

Please sign in to comment.