From 6b14234962a87ec02ea65735883b812a11945946 Mon Sep 17 00:00:00 2001 From: aboyko Date: Thu, 26 Jan 2023 23:51:41 -0500 Subject: [PATCH] Attempt to avoid NPE in OR recipes Create new recipe instance rather than re-use from the Environment --- .../boot/java/rewrite/SpringBootUpgrade.java | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgrade.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgrade.java index 964af116d..81de96fea 100644 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgrade.java +++ b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/SpringBootUpgrade.java @@ -28,6 +28,7 @@ import org.springframework.ide.vscode.commons.java.SpringProjectUtil; import org.springframework.ide.vscode.commons.java.Version; import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer; +import org.springframework.ide.vscode.commons.rewrite.LoadUtils; import org.springframework.ide.vscode.commons.util.Assert; import com.google.gson.JsonElement; @@ -48,11 +49,6 @@ public class SpringBootUpgrade { "3.0", "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0" ); -// private static final Map MAJOR_VERSION_TO_RECIPE_ID = Map.of( -// 2, "org.openrewrite.java.spring.boot2.SpringBoot1To2Migration", -// 3, "org.openrewrite.java.spring.boot3.SpringBoot2To3Migration" -// ); - private RewriteRecipeRepository recipeRepo; public SpringBootUpgrade(SimpleLanguageServer server, RewriteRecipeRepository recipeRepo, JavaProjectFinder projectFinder) { @@ -112,11 +108,17 @@ public class SpringBootUpgrade { List recipedIds = createRecipeIdsChain(version.getMajor(), version.getMinor(), targetVersion.getMajor(), targetVersion.getMinor(), versionsToRecipeId); if (!recipedIds.isEmpty()) { String recipeId = recipedIds.get(recipedIds.size() - 1); - recipeRepo.getRecipe(recipeId).ifPresent(recipe::doNext); + // TODO: Review after new rewrite adoption + // Special case is UpgradeSpringBoot_3_0 which doesn't upgrade project to the latest 2.x if necessary. Therefore add 2.(latest) recipe (it chains all previous 2.x) + if (targetVersion.getMajor() == 3) { + int i = recipedIds.size() - 1; + for (; i>=0 && !getVersionFromRecipeId(recipedIds.get(i)).startsWith("2."); i--) {} + if (i >= 0) { + getRecipeFromId(recipedIds.get(i)).ifPresent(recipe::doNext); + } + } + getRecipeFromId(recipeId).ifPresent(recipe::doNext); } -// } else { -// String recipeId = MAJOR_VERSION_TO_RECIPE_ID.get(targetVersion.getMajor()); -// recipeRepo.getRecipe(recipeId).ifPresent(recipe::doNext); } if (recipe.getRecipeList().isEmpty()) { @@ -128,6 +130,24 @@ public class SpringBootUpgrade { } } + private String getVersionFromRecipeId(String recipeId) { + for (Map.Entry e : versionsToRecipeId.entrySet()) { + if (recipeId.equals(e.getValue())) { + return e.getKey(); + } + } + return null; + } + + private Optional getRecipeFromId(String recipeId) { + // TODO: not sure what's wrong with Recipes coming from environment but FindDependency (maven) recipe sometimes throws NPEs on cursor +// return recipeRepo.getRecipe(recipeId); + // Instead convert recipe to descriptor and then create new one from descriptor - this seems to work better. + return recipeRepo.getRecipe(recipeId) + .map(r -> r.getDescriptor()) + .map(d -> LoadUtils.createRecipe(d, id -> recipeRepo.getRecipe(id).map(r -> r.getClass()).orElse(null))); + } + private static String createVersionString(int major, int minor) { StringBuilder sb = new StringBuilder(); sb.append(major);