From ecfdcd34c3b96e43699b62ea7f531cd75c787d28 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 24 Dec 2015 07:53:36 +0100 Subject: [PATCH] Class.getResourceAsStream can find the generated resource --- .../org/teavm/classlib/java/lang/TClass.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java index d5bf22ead..22277a27a 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java @@ -22,6 +22,7 @@ import java.util.Map; import org.teavm.classlib.impl.DeclaringClassMetadataGenerator; import org.teavm.classlib.java.lang.annotation.TAnnotation; import org.teavm.classlib.java.lang.reflect.TAnnotatedElement; +import org.teavm.jso.JSBody; import org.teavm.platform.Platform; import org.teavm.platform.PlatformClass; import org.teavm.platform.metadata.ClassResource; @@ -268,7 +269,22 @@ public class TClass extends TObject implements TAnnotatedElement { } } - public InputStream getResourceAsStream(String name) { - throw new IllegalStateException("Cannot read resource " + name); - } + @JSBody(params = "res", script = + "if (!window.teaVMResources) return null;\n" + + "var data = window.teaVMResources[res];\n" + + "return data ? data : null;\n" + ) + private static native String readResource(String message); + + public InputStream getResourceAsStream(String name) { + TString clazzName = getName(); + int lastDot = clazzName.lastIndexOf('.'); + String resName; + if (lastDot == -1) { + resName = name; + } else { + resName = clazzName.substring(0, lastDot).replace('.', '/') + "/" + name; + } + throw new IllegalStateException("Read resource " + resName + " yields " + readResource(resName)); + } }