From 11ddbd7894d2c3f63dc6fa83906214df00c1080a Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Thu, 2 Jan 2025 11:35:06 +0100 Subject: [PATCH] Read Sdkman candidates through `getResourceAsStream` --- .../java/migrate/UpdateSdkMan.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java b/src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java index 5acbc8b8b..0b53a7b09 100644 --- a/src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java +++ b/src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java @@ -25,16 +25,18 @@ import org.openrewrite.text.PlainText; import org.openrewrite.text.PlainTextParser; +import java.io.BufferedReader; import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Paths; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static java.util.Objects.requireNonNull; +import static java.util.stream.Collectors.toList; @Value @EqualsAndHashCode(callSuper = false) @@ -102,15 +104,14 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { } private List readSdkmanJavaCandidates() { - URL resource = getClass().getResource("/sdkman-java.csv"); - if (resource != null) { - try { - return Files.readAllLines(Paths.get(resource.toURI())); - } catch (IOException | URISyntaxException e) { - throw new RuntimeException(e); - } + try (InputStream resourceAsStream = UpdateSdkMan.class.getResourceAsStream("/sdkman-java.csv"); + InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) { + return bufferedReader.lines().collect(toList()); + } catch (IOException e) { + throw new RuntimeException(e); } - throw new IllegalStateException("Could not find /sdkman-java.csv file"); + } }; return Preconditions.check(new FindSourceFiles(".sdkmanrc"), visitor);