Skip to content

Commit

Permalink
fix: 🚑️ Fix FileSystemnotFound for resource files (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt committed Jun 9, 2023
1 parent a370d2b commit d39076d
Showing 1 changed file with 5 additions and 11 deletions.
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

0 comments on commit d39076d

Please sign in to comment.