Writing recipes over multiple source file types

When creating new recipes, you may find it desirable to examine multiple source files, potentially of different types, to make key decisions in your visitor. For example, you may want to look for a particular condition to be present in a Maven POM file and, if that condition is met, alter an application property in a YAML file.
You should use the visit(List<SourceFile> before, ExecutionContext ctx) method in the Recipe class to iterate over and maintain the decision state, and then to execute the transformation.
package org.openrewrite;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.maven.tree.Maven;
import org.openrewrite.yaml.YamlVisitor;
import java.util.List;
public class MavenYamlRecipe extends Recipe {
public String getDisplayName() {
return "Something with Maven and something with YAML";
protected List<SourceFile> visit(List<SourceFile> before, ExecutionContext ctx) {
boolean pomConditionMet = false;
for (SourceFile sourceFile : before) {
if(sourceFile instanceof Maven) {
Maven m = (Maven) sourceFile;
// pomConditionMet = ...
return, sourceFile -> (SourceFile) new YamlVisitor<Integer>() {
// whatever your YAML visitor does can now respond to pomConditionMet
}.visitNonNull(sourceFile, 0));
To write a unit test that tests multiple source file types, use their respective parsers:
package org.openrewrite
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.openrewrite.maven.MavenParser
import org.openrewrite.yaml.YamlParser
class MavenYamlTest {
fun mavenYaml() {
val sources =
key: value
val results = MavenYaml().run(sources)
assertThat( { it.after!!.printAll() }).containsExactly(
key: changedValue