-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept for preconditions on declarative recipes
- Loading branch information
Showing
5 changed files
with
222 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
rewrite-core/src/main/java/org/openrewrite/config/PreconditionBellwether.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.openrewrite.config; | ||
|
||
import org.openrewrite.Recipe; | ||
|
||
public class PreconditionBellwether extends Recipe { | ||
@Override | ||
public String getDisplayName() { | ||
return "Precondition bellwether"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Evaluates a precondition as an implementation detail"; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
rewrite-core/src/main/java/org/openrewrite/config/PreconditionDecoratedRecipe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.openrewrite.config; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
|
||
@EqualsAndHashCode(callSuper = true) | ||
@Value | ||
public class PreconditionDecoratedRecipe extends Recipe { | ||
|
||
TreeVisitor<?, ExecutionContext> precondition; | ||
Recipe delegate; | ||
|
||
@Override | ||
public String getName() { | ||
return delegate.getName(); | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return delegate.getDisplayName(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return delegate.getDescription(); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check(precondition, delegate.getVisitor()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
rewrite-core/src/main/java/org/openrewrite/config/PreconditionDecoratedScanningRecipe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.openrewrite.config; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.*; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = true) | ||
public class PreconditionDecoratedScanningRecipe<T> extends ScanningRecipe<T> { | ||
|
||
TreeVisitor<?, ExecutionContext> precondition; | ||
ScanningRecipe<T> delegate; | ||
|
||
@Override | ||
public String getName() { | ||
return delegate.getName(); | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return delegate.getDisplayName(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return delegate.getDescription(); | ||
} | ||
|
||
@Override | ||
public T getInitialValue(ExecutionContext ctx) { | ||
return delegate.getInitialValue(ctx); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getScanner(T acc) { | ||
return delegate.getScanner(acc); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor(T acc) { | ||
return Preconditions.check(precondition, delegate.getVisitor(acc)); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
rewrite-core/src/test/java/org/openrewrite/config/DeclarativeRecipeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.openrewrite.config; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.marker.SearchResult; | ||
import org.openrewrite.test.RewriteTest; | ||
import org.openrewrite.text.ChangeText; | ||
import org.openrewrite.text.PlainText; | ||
import org.openrewrite.text.PlainTextVisitor; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.openrewrite.test.SourceSpecs.text; | ||
import static org.openrewrite.test.RewriteTest.toRecipe; | ||
|
||
public class DeclarativeRecipeTest implements RewriteTest { | ||
|
||
@Test | ||
void precondition() { | ||
rewriteRun( | ||
spec -> { | ||
spec.validateRecipeSerialization(false); | ||
DeclarativeRecipe dr = new DeclarativeRecipe("test", "test", "test", null, | ||
null, null, true, null); | ||
dr.addPrecondition( | ||
toRecipe(() -> new PlainTextVisitor<>() { | ||
@Override | ||
public PlainText visitText(PlainText text, ExecutionContext executionContext) { | ||
if("1".equals(text.getText())) { | ||
return SearchResult.found(text); | ||
} | ||
return text; | ||
} | ||
}) | ||
); | ||
dr.addUninitialized( | ||
new ChangeText("2") | ||
); | ||
dr.addUninitialized( | ||
new ChangeText("3") | ||
); | ||
dr.initialize(List.of(), Map.of()); | ||
spec.recipe(dr); | ||
}, | ||
text("1","3"), | ||
text("2") | ||
); | ||
} | ||
} |