diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TClassLoader.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TClassLoader.java index b7b310512..be243a747 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TClassLoader.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TClassLoader.java @@ -17,15 +17,12 @@ package org.teavm.classlib.java.lang; import java.io.ByteArrayInputStream; import java.io.InputStream; -import org.teavm.classlib.impl.Base64; import org.teavm.backend.javascript.spi.InjectedBy; +import org.teavm.classlib.impl.Base64; +import org.teavm.jso.JSBody; import org.teavm.jso.JSIndexer; import org.teavm.jso.JSObject; -/** - * - * @author Alexey Andreev - */ public abstract class TClassLoader extends TObject { private TClassLoader parent; private static TSystemClassLoader systemClassLoader = new TSystemClassLoader(); @@ -51,15 +48,19 @@ public abstract class TClassLoader extends TObject { if (resources == null) { resources = supplyResources(); } - String data = resources.getResource(name); - return data == null ? null : new ByteArrayInputStream(Base64.decode(data)); + JSObject data = resources.getResource(name); + String dataString = resourceToString(data); + return dataString == null ? null : new ByteArrayInputStream(Base64.decode(dataString)); } + @JSBody(params = "resource", script = "return resource !== null && resource !== void 0 ? resource : null;") + private static native String resourceToString(JSObject resource); + @InjectedBy(ClassLoaderNativeGenerator.class) private static native ResourceContainer supplyResources(); interface ResourceContainer extends JSObject { @JSIndexer - String getResource(String name); + JSObject getResource(String name); } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/ClassLoaderTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/ClassLoaderTest.java index a891474fd..24c65332a 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/ClassLoaderTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/ClassLoaderTest.java @@ -18,6 +18,7 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.*; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import org.junit.Test; import org.junit.runner.RunWith; @@ -37,6 +38,12 @@ public class ClassLoaderTest { assertEquals("qwertyui", loadResource("8")); } + @Test + public void returnsNullForNonExistentResource() { + InputStream input = ClassLoader.getSystemClassLoader().getResourceAsStream("non-existent-resource.txt"); + assertNull(input); + } + private static String loadResource(String name) { ClassLoader classLoader = ClassLoader.getSystemClassLoader(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream(