Skip to content

Commit

Permalink
Find JavaVersion markers on any source file
Browse files Browse the repository at this point in the history
Necessary for proper functioning of this recipe & tests:
- openrewrite/rewrite-spring#460
  • Loading branch information
timtebeek committed Dec 8, 2023
1 parent 3770f85 commit 679b5e5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.openrewrite.test.RewriteTest;

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

class HasJavaVersionTest implements RewriteTest {
Expand All @@ -31,18 +31,16 @@ class HasJavaVersionTest implements RewriteTest {
void matches(String version) {
rewriteRun(
spec -> spec.recipe(new HasJavaVersion(version, false)),
version(
java(
"""
class Test {
}
""",
"""
/*~~>*/class Test {
}
"""
),
11
java(
"""
class Test {
}
""",
"""
/*~~>*/class Test {
}
""",
spec -> spec.markers(javaVersion(11))
)
);
}
Expand All @@ -52,14 +50,12 @@ class Test {
void noMatch(String version) {
rewriteRun(
spec -> spec.recipe(new HasJavaVersion(version, false)),
version(
java(
"""
class Test {
}
"""
),
17
java(
"""
class Test {
}
""",
spec -> spec.markers(javaVersion(17))
)
);
}
Expand All @@ -82,4 +78,22 @@ void declarativePrecondition() {
);
}

@Test
void declarativePreconditionMatch() {
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", "2", spec -> spec.markers(javaVersion(11)))
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.marker.JavaVersion;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
Expand Down Expand Up @@ -69,20 +66,19 @@ public Validated<Object> validate() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
VersionComparator versionComparator = requireNonNull(Semver.validate(version, null).getValue());
return new JavaIsoVisitor<ExecutionContext>() {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
return cu.getMarkers().findFirst(JavaVersion.class)
public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree != null) {
return tree.getMarkers().findFirst(JavaVersion.class)
.filter(version -> versionComparator.isValid(null, Integer.toString(
Boolean.TRUE.equals(checkTargetCompatibility) ?
version.getMajorReleaseVersion() :
version.getMajorVersion())))
.map(version -> SearchResult.found(cu))
.orElse(cu);
.map(version -> SearchResult.found(tree))
.orElse(tree);
}
return (J) tree;
return tree;
}
};
}
Expand Down

0 comments on commit 679b5e5

Please sign in to comment.