Skip to content

Commit 2db435f

Browse files
committed
Proper recipe class lookup mechanism for plugged in recipes
1 parent 7b34fcb commit 2db435f

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

eclipse-language-servers/org.springframework.tooling.boot.ls/src/org/springframework/tooling/boot/ls/commands/RewriteRefactoringsHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.eclipse.core.runtime.IAdaptable;
2828
import org.eclipse.core.runtime.IProgressMonitor;
2929
import org.eclipse.jdt.annotation.NonNull;
30+
import org.eclipse.jface.dialogs.MessageDialog;
3031
import org.eclipse.jface.viewers.IStructuredSelection;
3132
import org.eclipse.jface.window.Window;
3233
import org.eclipse.lsp4e.LanguageServiceAccessor;
@@ -36,7 +37,6 @@
3637
import org.eclipse.ui.PlatformUI;
3738
import org.eclipse.ui.handlers.HandlerUtil;
3839
import org.springframework.tooling.boot.ls.BootLanguageServerPlugin;
39-
import org.springsource.ide.eclipse.commons.livexp.util.Log;
4040

4141
import com.google.gson.Gson;
4242
import com.google.gson.GsonBuilder;
@@ -130,14 +130,15 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
130130
.toArray(CompletableFuture[]::new)).get();
131131
}
132132
} catch (Exception e) {
133-
Log.log(e);
133+
throw new InvocationTargetException(e);
134134
} finally {
135135
monitor.done();
136136
}
137137
});
138138

139139
} catch (CoreException | InvocationTargetException | InterruptedException e) {
140140
BootLanguageServerPlugin.getDefault().getLog().error(e.getMessage(), e);
141+
MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error", "Failed to apply Rewrite recipe(s). See error log for more details.");
141142
}
142143
}
143144
});

headless-services/commons/commons-rewrite-test/src/main/java/org/springframework/rewrite/test/HelloMethodRenameRecipe.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class HelloMethodRenameRecipe extends Recipe {
2323
public String getDisplayName() {
2424
return "Rename hello method into bye";
2525
}
26+
27+
@Override
28+
public String getDescription() {
29+
return "Renames methods with name 'hello' into same signature methods named 'bye'";
30+
}
2631

2732
@Override
2833
protected TreeVisitor<?, ExecutionContext> getVisitor() {

headless-services/commons/commons-rewrite/src/main/java/org/springframework/ide/vscode/commons/rewrite/LoadUtils.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Collection;
2121
import java.util.List;
22+
import java.util.function.Function;
2223

2324
import org.openrewrite.Recipe;
2425
import org.openrewrite.config.DeclarativeRecipe;
@@ -50,18 +51,17 @@ public Duration deserialize(JsonElement json, Type type, JsonDeserializationCont
5051
}
5152
}
5253

53-
@SuppressWarnings("unchecked")
54-
public static Recipe createRecipe(RecipeDescriptor d) {
55-
try {
56-
Class<? extends Recipe> recipeClazz = (Class<? extends Recipe>) Class.forName(d.getName());
57-
Recipe recipe = constructRecipe(recipeClazz, d.getOptions());
58-
return recipe;
59-
} catch (ClassNotFoundException e) {
60-
DeclarativeRecipe recipe = new DeclarativeRecipe(d.getName(), d.getDisplayName(), d.getDescription(), d.getTags(), d.getEstimatedEffortPerOccurrence(), d.getSource(), false);
54+
public static Recipe createRecipe(RecipeDescriptor d, Function<String, Class<? extends Recipe>> getRecipeClass) {
55+
Class<? extends Recipe> recipeClazz = getRecipeClass.apply(d.getName());
56+
if (recipeClazz == null) {
57+
DeclarativeRecipe recipe = new DeclarativeRecipe(d.getName(), d.getDisplayName(), d.getDescription(),
58+
d.getTags(), d.getEstimatedEffortPerOccurrence(), d.getSource(), false);
6159
for (RecipeDescriptor subDescriptor : d.getRecipeList()) {
62-
recipe.doNext(createRecipe(subDescriptor));
60+
recipe.doNext(createRecipe(subDescriptor, getRecipeClass));
6361
}
6462
return recipe;
63+
} else {
64+
return constructRecipe(recipeClazz, d.getOptions());
6565
}
6666
}
6767

headless-services/commons/commons-rewrite/src/test/java/org/springframework/ide/vscode/commons/rewrite/LoadUtilsTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@ public static void setupAll() {
3131
env = Environment.builder().scanRuntimeClasspath().build();
3232
}
3333

34+
@SuppressWarnings("unchecked")
3435
@Test
3536
public void createRecipeTest() throws Exception {
3637
Recipe r = env.listRecipes().stream().filter(d -> "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0".equals(d.getName())).findFirst().orElse(null);
3738
RecipeDescriptor recipeDescriptor = r.getDescriptor();
3839
assertNotNull(recipeDescriptor);
39-
r = LoadUtils.createRecipe(recipeDescriptor);
40+
r = LoadUtils.createRecipe(recipeDescriptor, id -> {
41+
try {
42+
return (Class<Recipe>) Class.forName(id);
43+
} catch (ClassNotFoundException e) {
44+
return null;
45+
}
46+
});
4047

4148
assertTrue(r instanceof DeclarativeRecipe);
4249
assertEquals("org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0", r.getName());

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/RewriteRecipeRepository.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.openrewrite.SourceFile;
4949
import org.openrewrite.TreeVisitor;
5050
import org.openrewrite.Validated;
51+
import org.openrewrite.config.DeclarativeRecipe;
5152
import org.openrewrite.config.RecipeDescriptor;
5253
import org.openrewrite.config.YamlResourceLoader;
5354
import org.openrewrite.java.JavaParser;
@@ -338,10 +339,10 @@ private void registerCommands() {
338339

339340
RecipeDescriptor d = serializationGson.fromJson(recipesJson, RecipeDescriptor.class);
340341

341-
Recipe aggregateRecipe = LoadUtils.createRecipe(d);
342+
Recipe aggregateRecipe = LoadUtils.createRecipe(d, id -> getRecipe(id).map(r -> r.getClass()).orElse(null));
342343

343-
if (aggregateRecipe.getRecipeList().isEmpty()) {
344-
throw new RuntimeException("Not recipes to execute!");
344+
if (aggregateRecipe instanceof DeclarativeRecipe && aggregateRecipe.getRecipeList().isEmpty()) {
345+
throw new RuntimeException("No recipes found to perform!");
345346
} else if (aggregateRecipe.getRecipeList().size() == 1) {
346347
Recipe r = aggregateRecipe.getRecipeList().get(0);
347348
String progressToken = params.getWorkDoneToken() == null || params.getWorkDoneToken().getLeft() == null ? r.getName() : params.getWorkDoneToken().getLeft();

0 commit comments

Comments
 (0)