Proper recipe class lookup mechanism for plugged in recipes
This commit is contained in:
@@ -27,6 +27,7 @@ import org.eclipse.core.runtime.CoreException;
|
|||||||
import org.eclipse.core.runtime.IAdaptable;
|
import org.eclipse.core.runtime.IAdaptable;
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
import org.eclipse.jdt.annotation.NonNull;
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
|
import org.eclipse.jface.dialogs.MessageDialog;
|
||||||
import org.eclipse.jface.viewers.IStructuredSelection;
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
||||||
import org.eclipse.jface.window.Window;
|
import org.eclipse.jface.window.Window;
|
||||||
import org.eclipse.lsp4e.LanguageServiceAccessor;
|
import org.eclipse.lsp4e.LanguageServiceAccessor;
|
||||||
@@ -36,7 +37,6 @@ import org.eclipse.swt.widgets.Display;
|
|||||||
import org.eclipse.ui.PlatformUI;
|
import org.eclipse.ui.PlatformUI;
|
||||||
import org.eclipse.ui.handlers.HandlerUtil;
|
import org.eclipse.ui.handlers.HandlerUtil;
|
||||||
import org.springframework.tooling.boot.ls.BootLanguageServerPlugin;
|
import org.springframework.tooling.boot.ls.BootLanguageServerPlugin;
|
||||||
import org.springsource.ide.eclipse.commons.livexp.util.Log;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
@@ -130,7 +130,7 @@ public class RewriteRefactoringsHandler extends AbstractHandler {
|
|||||||
.toArray(CompletableFuture[]::new)).get();
|
.toArray(CompletableFuture[]::new)).get();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.log(e);
|
throw new InvocationTargetException(e);
|
||||||
} finally {
|
} finally {
|
||||||
monitor.done();
|
monitor.done();
|
||||||
}
|
}
|
||||||
@@ -138,6 +138,7 @@ public class RewriteRefactoringsHandler extends AbstractHandler {
|
|||||||
|
|
||||||
} catch (CoreException | InvocationTargetException | InterruptedException e) {
|
} catch (CoreException | InvocationTargetException | InterruptedException e) {
|
||||||
BootLanguageServerPlugin.getDefault().getLog().error(e.getMessage(), e);
|
BootLanguageServerPlugin.getDefault().getLog().error(e.getMessage(), e);
|
||||||
|
MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error", "Failed to apply Rewrite recipe(s). See error log for more details.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ public class HelloMethodRenameRecipe extends Recipe {
|
|||||||
return "Rename hello method into bye";
|
return "Rename hello method into bye";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "Renames methods with name 'hello' into same signature methods named 'bye'";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected TreeVisitor<?, ExecutionContext> getVisitor() {
|
protected TreeVisitor<?, ExecutionContext> getVisitor() {
|
||||||
return new JavaIsoVisitor<>() {
|
return new JavaIsoVisitor<>() {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import java.time.Duration;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.openrewrite.Recipe;
|
import org.openrewrite.Recipe;
|
||||||
import org.openrewrite.config.DeclarativeRecipe;
|
import org.openrewrite.config.DeclarativeRecipe;
|
||||||
@@ -50,18 +51,17 @@ public class LoadUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
public static Recipe createRecipe(RecipeDescriptor d, Function<String, Class<? extends Recipe>> getRecipeClass) {
|
||||||
public static Recipe createRecipe(RecipeDescriptor d) {
|
Class<? extends Recipe> recipeClazz = getRecipeClass.apply(d.getName());
|
||||||
try {
|
if (recipeClazz == null) {
|
||||||
Class<? extends Recipe> recipeClazz = (Class<? extends Recipe>) Class.forName(d.getName());
|
DeclarativeRecipe recipe = new DeclarativeRecipe(d.getName(), d.getDisplayName(), d.getDescription(),
|
||||||
Recipe recipe = constructRecipe(recipeClazz, d.getOptions());
|
d.getTags(), d.getEstimatedEffortPerOccurrence(), d.getSource(), false);
|
||||||
return recipe;
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
DeclarativeRecipe recipe = new DeclarativeRecipe(d.getName(), d.getDisplayName(), d.getDescription(), d.getTags(), d.getEstimatedEffortPerOccurrence(), d.getSource(), false);
|
|
||||||
for (RecipeDescriptor subDescriptor : d.getRecipeList()) {
|
for (RecipeDescriptor subDescriptor : d.getRecipeList()) {
|
||||||
recipe.doNext(createRecipe(subDescriptor));
|
recipe.doNext(createRecipe(subDescriptor, getRecipeClass));
|
||||||
}
|
}
|
||||||
return recipe;
|
return recipe;
|
||||||
|
} else {
|
||||||
|
return constructRecipe(recipeClazz, d.getOptions());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,12 +31,19 @@ public class LoadUtilsTest {
|
|||||||
env = Environment.builder().scanRuntimeClasspath().build();
|
env = Environment.builder().scanRuntimeClasspath().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Test
|
@Test
|
||||||
public void createRecipeTest() throws Exception {
|
public void createRecipeTest() throws Exception {
|
||||||
Recipe r = env.listRecipes().stream().filter(d -> "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0".equals(d.getName())).findFirst().orElse(null);
|
Recipe r = env.listRecipes().stream().filter(d -> "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0".equals(d.getName())).findFirst().orElse(null);
|
||||||
RecipeDescriptor recipeDescriptor = r.getDescriptor();
|
RecipeDescriptor recipeDescriptor = r.getDescriptor();
|
||||||
assertNotNull(recipeDescriptor);
|
assertNotNull(recipeDescriptor);
|
||||||
r = LoadUtils.createRecipe(recipeDescriptor);
|
r = LoadUtils.createRecipe(recipeDescriptor, id -> {
|
||||||
|
try {
|
||||||
|
return (Class<Recipe>) Class.forName(id);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
assertTrue(r instanceof DeclarativeRecipe);
|
assertTrue(r instanceof DeclarativeRecipe);
|
||||||
assertEquals("org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0", r.getName());
|
assertEquals("org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0", r.getName());
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ import org.openrewrite.Result;
|
|||||||
import org.openrewrite.SourceFile;
|
import org.openrewrite.SourceFile;
|
||||||
import org.openrewrite.TreeVisitor;
|
import org.openrewrite.TreeVisitor;
|
||||||
import org.openrewrite.Validated;
|
import org.openrewrite.Validated;
|
||||||
|
import org.openrewrite.config.DeclarativeRecipe;
|
||||||
import org.openrewrite.config.RecipeDescriptor;
|
import org.openrewrite.config.RecipeDescriptor;
|
||||||
import org.openrewrite.config.YamlResourceLoader;
|
import org.openrewrite.config.YamlResourceLoader;
|
||||||
import org.openrewrite.java.JavaParser;
|
import org.openrewrite.java.JavaParser;
|
||||||
@@ -338,10 +339,10 @@ public class RewriteRecipeRepository implements ApplicationContextAware {
|
|||||||
|
|
||||||
RecipeDescriptor d = serializationGson.fromJson(recipesJson, RecipeDescriptor.class);
|
RecipeDescriptor d = serializationGson.fromJson(recipesJson, RecipeDescriptor.class);
|
||||||
|
|
||||||
Recipe aggregateRecipe = LoadUtils.createRecipe(d);
|
Recipe aggregateRecipe = LoadUtils.createRecipe(d, id -> getRecipe(id).map(r -> r.getClass()).orElse(null));
|
||||||
|
|
||||||
if (aggregateRecipe.getRecipeList().isEmpty()) {
|
if (aggregateRecipe instanceof DeclarativeRecipe && aggregateRecipe.getRecipeList().isEmpty()) {
|
||||||
throw new RuntimeException("Not recipes to execute!");
|
throw new RuntimeException("No recipes found to perform!");
|
||||||
} else if (aggregateRecipe.getRecipeList().size() == 1) {
|
} else if (aggregateRecipe.getRecipeList().size() == 1) {
|
||||||
Recipe r = aggregateRecipe.getRecipeList().get(0);
|
Recipe r = aggregateRecipe.getRecipeList().get(0);
|
||||||
String progressToken = params.getWorkDoneToken() == null || params.getWorkDoneToken().getLeft() == null ? r.getName() : params.getWorkDoneToken().getLeft();
|
String progressToken = params.getWorkDoneToken() == null || params.getWorkDoneToken().getLeft() == null ? r.getName() : params.getWorkDoneToken().getLeft();
|
||||||
|
|||||||
Reference in New Issue
Block a user