Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Result;
import org.openrewrite.SourceFile;
import org.openrewrite.config.DeclarativeRecipe;
import org.openrewrite.config.YamlResourceLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.util.ReflectionUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
Expand All @@ -48,7 +44,7 @@ public class OpenRewriteDeclarativeRecipeAdapter extends AbstractAction {

@JsonIgnore
@Autowired
private RewriteMigrationResultMerger resultMerger;
private OpenRewriteRecipeRunner openRewriteRecipeRunner;

@Override
public boolean isApplicable(ProjectContext context) {
Expand All @@ -71,11 +67,7 @@ public void apply(ProjectContext context) {
throw new RuntimeException(String.format("Ambiguous number of recipes found. Expected exactly one, found %s", rewriteYamlRecipe.size()));
}
Recipe recipe = rewriteYamlRecipe.iterator().next();
List<? extends SourceFile> rewriteSourceFiles = context.search(new OpenRewriteSourceFilesFinder());
List<Result> results = recipe.run(rewriteSourceFiles, new InMemoryExecutionContext((t) -> {
throw new RuntimeException(t);
}));
resultMerger.mergeResults(context, results);
openRewriteRecipeRunner.run(context, recipe);
}

private void initializeRecipe(DeclarativeRecipe recipe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.openrewrite.Recipe;
import org.openrewrite.Result;
import org.openrewrite.SourceFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.sbm.engine.context.ProjectContext;

import java.util.List;

@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -43,13 +39,11 @@ public class OpenRewriteNamedRecipeAdapter extends AbstractAction {

@JsonIgnore
@Autowired
private RewriteMigrationResultMerger resultMerger;
private OpenRewriteRecipeRunner openRewriteRecipeRunner;

@Override
public void apply(ProjectContext context) {
Recipe recipe = rewriteRecipeLoader.loadRewriteRecipe(openRewriteRecipeName);
List<? extends SourceFile> rewriteSourceFiles = context.search(new OpenRewriteSourceFilesFinder());
List<Result> results = recipe.run(rewriteSourceFiles);
resultMerger.mergeResults(context, results);
openRewriteRecipeRunner.run(context, recipe);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.springframework.sbm.engine.recipe;

import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Result;
import org.openrewrite.SourceFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.sbm.engine.context.ProjectContext;
/*
* Copyright 2021 - 2022 the original author or authors.
*
* 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class OpenRewriteRecipeRunner {
@Autowired
private RewriteMigrationResultMerger resultMerger;

public void run(ProjectContext context, Recipe recipe) {
List<? extends SourceFile> rewriteSourceFiles = context.search(new OpenRewriteSourceFilesFinder());
List<Result> results = recipe.run(rewriteSourceFiles, new InMemoryExecutionContext(
(t) -> {
throw new RuntimeException(t);
}
));
resultMerger.mergeResults(context, results);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@

package org.springframework.sbm.engine.recipe;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.sbm.engine.context.ProjectContext;
import org.springframework.sbm.project.RewriteSourceFileWrapper;
import org.springframework.sbm.project.resource.ResourceHelper;
import org.springframework.sbm.project.resource.TestProjectContext;
import org.springframework.stereotype.Component;
import org.springframework.validation.beanvalidation.CustomValidatorBean;

import java.io.IOException;
Expand All @@ -41,6 +37,7 @@
ResourceHelper.class,
ActionDeserializerRegistry.class,
RewriteMigrationResultMerger.class,
OpenRewriteRecipeRunner.class,
RewriteSourceFileWrapper.class,
CustomValidatorBean.class
})
Expand Down Expand Up @@ -88,7 +85,6 @@ void recipeFromYaml() throws IOException {
);
}


@Test
public void propagateExceptionFromOpenRewriteRecipe() throws IOException {

Expand Down Expand Up @@ -123,6 +119,8 @@ public void propagateExceptionFromOpenRewriteRecipe() throws IOException {
.addJavaSource("src/main/java", javaSource)
.build();

assertThrows(RuntimeException.class, () -> recipeAdapter.apply(context));
RuntimeException thrown = assertThrows(RuntimeException.class, () -> recipeAdapter.apply(context));

assertThat(thrown).hasRootCauseMessage("A problem happened whilst visiting");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest(classes = {
RecipeParser.class,
Expand All @@ -36,6 +37,7 @@
ResourceHelper.class,
ActionDeserializerRegistry.class,
RewriteMigrationResultMerger.class,
OpenRewriteRecipeRunner.class,
RewriteSourceFileWrapper.class,
RewriteRecipeLoader.class,
CustomValidatorBean.class
Expand Down Expand Up @@ -76,4 +78,33 @@ void recipeFromYaml() throws IOException {
"}\n"
);
}

@Test
public void propagateExceptionFromOpenRewriteRecipe() throws IOException {

String actionDescription =
"- name: test-recipe\n" +
" description: Replace deprecated spring.datasource.* properties\n" +
" condition:\n" +
" type: org.springframework.sbm.common.migration.conditions.TrueCondition\n" +
" actions:\n" +
" - type: org.springframework.sbm.engine.recipe.OpenRewriteNamedRecipeAdapter\n" +
" description: Test recipe producing exception\n" +
" openRewriteRecipeName: org.springframework.sbm.engine.recipe.ErrorClass\n";

Recipe[] recipes = recipeParser.parseRecipe(actionDescription);
assertThat(recipes[0].getActions().get(0)).isInstanceOf(OpenRewriteNamedRecipeAdapter.class);
OpenRewriteNamedRecipeAdapter recipeAdapter = (OpenRewriteNamedRecipeAdapter) recipes[0].getActions().get(0);

String javaSource = "@java.lang.Deprecated\n" +
"public class Foo {}";

ProjectContext context = TestProjectContext.buildProjectContext()
.addJavaSource("src/main/java", javaSource)
.build();

RuntimeException thrown = assertThrows(RuntimeException.class, () -> recipeAdapter.apply(context));

assertThat(thrown).hasRootCauseMessage("A problem happened whilst visiting");
}
}