Skip to content

Commit

Permalink
PreconditionBellwether must call isAcceptable() (#3780)
Browse files Browse the repository at this point in the history
When a precondition visitor extends an `XyzIsoVisitor` class (like `JavaIsoVisitor`) and there overrides the `visit()` method, then this will result in a `ClassCastException` when the visitor is given a source file which is not of that type.
  • Loading branch information
knutwannheden authored Dec 6, 2023
1 parent 1babd95 commit a928dc2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
return precondition.isAcceptable(sourceFile, ctx);
}

@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
Tree t = precondition.visit(tree, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
*/
package org.openrewrite.java.search;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;
import static org.openrewrite.test.SourceSpecs.text;

class HasJavaVersionTest implements RewriteTest {

Expand Down Expand Up @@ -61,4 +63,23 @@ class Test {
)
);
}

@Test
void declarativePrecondition() {
rewriteRun(
spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.PreconditionTest
preconditions:
- org.openrewrite.java.search.HasJavaVersion:
version: 11
recipeList:
- org.openrewrite.text.ChangeText:
toText: 2
""", "org.openrewrite.PreconditionTest"),
text("1")
);
}

}

0 comments on commit a928dc2

Please sign in to comment.