Skip to content

Commit

Permalink
Rewrite 8.0 migration recipes (openrewrite#3241)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 authored May 16, 2023
1 parent e3116a6 commit a82248c
Show file tree
Hide file tree
Showing 12 changed files with 1,767 additions and 2 deletions.
54 changes: 54 additions & 0 deletions rewrite-core/src/main/java/org/openrewrite/Preconditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2022 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite;

/**
* This class is for rewrite8 migration purpose only
*/
public class Preconditions {

public static TreeVisitor<?, ExecutionContext> check(Recipe check, TreeVisitor<?, ExecutionContext> v) {
throw new RuntimeException("Not supported Rewrite8 feature");
}

public static TreeVisitor<?, ExecutionContext> check(TreeVisitor<?, ExecutionContext> check, TreeVisitor<?, ExecutionContext> v) {
throw new RuntimeException("Not supported Rewrite8 feature");
}

public static TreeVisitor<?, ExecutionContext> check(boolean check, TreeVisitor<?, ExecutionContext> v) {
throw new RuntimeException("Not supported Rewrite8 feature");
}

@Incubating(since = "8.0.0")
@SafeVarargs
public static TreeVisitor<?, ExecutionContext> firstAcceptable(TreeVisitor<?, ExecutionContext>... vs) {
throw new RuntimeException("Not supported Rewrite8 feature");
}

public static TreeVisitor<?, ExecutionContext> not(TreeVisitor<?, ExecutionContext> v) {
throw new RuntimeException("Not supported Rewrite8 feature");
}

@SafeVarargs
public static TreeVisitor<?, ExecutionContext> or(TreeVisitor<?, ExecutionContext>... vs) {
throw new RuntimeException("Not supported Rewrite8 feature");
}

@SafeVarargs
public static TreeVisitor<?, ExecutionContext> and(TreeVisitor<?, ExecutionContext>... vs) {
throw new RuntimeException("Not supported Rewrite8 feature");
}
}
2 changes: 2 additions & 0 deletions rewrite-java-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ dependencies {
testRuntimeOnly("org.mapstruct:mapstruct:latest.release")
testRuntimeOnly("org.projectlombok:lombok:latest.release")
testRuntimeOnly("org.apache.commons:commons-lang3:latest.release")
testRuntimeOnly(project(":rewrite-yaml"))
testRuntimeOnly("org.eclipse.jgit:org.eclipse.jgit:5.13.+")
}

tasks.withType<Javadoc> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.upgrade;

import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;


import static org.openrewrite.java.Assertions.java;

class MigrateMarkersSearchResultTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new MigrateMarkersSearchResult())
.parser(JavaParser.fromJavaVersion()
.classpath(JavaParser.runtimeClasspath())
);
}

@Test
void migrate() {
rewriteRun(
java(
"""
package org.openrewrite.kubernetes.resource;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.yaml.YamlIsoVisitor;
import org.openrewrite.yaml.tree.Yaml;
@Value
@EqualsAndHashCode(callSuper = true)
public class FindExceedsResourceRatio extends Recipe {
@Override
public String getDisplayName() {
return "Find exceeds resource ratio";
}
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new YamlIsoVisitor<ExecutionContext>() {
@Override
public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext ctx) {
Yaml.Mapping.Entry e = super.visitMappingEntry(entry, ctx);
return e.withMarkers(e.getMarkers().searchResult("foo"));
}
};
}
}
""",
"""
package org.openrewrite.kubernetes.resource;
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.yaml.YamlIsoVisitor;
import org.openrewrite.yaml.tree.Yaml;
@Value
@EqualsAndHashCode(callSuper = true)
public class FindExceedsResourceRatio extends Recipe {
@Override
public String getDisplayName() {
return "Find exceeds resource ratio";
}
@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new YamlIsoVisitor<ExecutionContext>() {
@Override
public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext ctx) {
Yaml.Mapping.Entry e = super.visitMappingEntry(entry, ctx);
return SearchResult.found(e, "foo");
}
};
}
}
"""
)
);
}
}
Loading

0 comments on commit a82248c

Please sign in to comment.