Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🚑️ Fix FileSystemnotFound for resource files #723

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down