Attempt to avoid NPE in OR recipes
Create new recipe instance rather than re-use from the Environment
This commit is contained in:
@@ -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<Integer, String> 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<String> 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<String, String> e : versionsToRecipeId.entrySet()) {
|
||||
if (recipeId.equals(e.getValue())) {
|
||||
return e.getKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Optional<Recipe> 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);
|
||||
|
||||
Reference in New Issue
Block a user