From d39076de0042fe963707abca8fb472e2e880a144 Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Sat, 10 Jun 2023 01:24:58 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=9A=91=EF=B8=8F=20Fix=20FileSystem?= =?UTF-8?q?notFound=20for=20resource=20files=20(#723)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../analyzer/spoon/TemplateHelper.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/TemplateHelper.java b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/TemplateHelper.java index c44c0d1db..df7a05aaf 100644 --- a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/TemplateHelper.java +++ b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/TemplateHelper.java @@ -1,9 +1,7 @@ package xyz.keksdose.spoon.code_solver.analyzer.spoon; import com.google.common.flogger.FluentLogger; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; +import java.io.InputStream; import spoon.Launcher; import spoon.reflect.declaration.CtType; import spoon.support.compiler.VirtualFile; @@ -15,15 +13,11 @@ private TemplateHelper() {} public static CtType fromResource(String resource) { ClassLoader classLoader = TemplateHelper.class.getClassLoader(); - URL resourceUrl = classLoader.getResource(resource); - if (resourceUrl == null) { - logger.atSevere().log("Resource not found: %s", resource); - throw new IllegalArgumentException("Resource not found: " + resource); - } - try { - String content = Files.readString(Path.of(resourceUrl.toURI())); + try (InputStream inputStream = classLoader.getResourceAsStream(resource); ) { + byte[] content = inputStream.readAllBytes(); + String contentString = new String(content); Launcher launcher = new Launcher(); - VirtualFile file = new VirtualFile(content, "Test"); + VirtualFile file = new VirtualFile(contentString, "Test"); launcher.addInputResource(file); var model = launcher.buildModel(); return model.getAllTypes().iterator().next();