# HG changeset patch # User Jaroslav Tulach # Date 1359052600 -3600 # Node ID b679a7dad2974dc768ec2ee0374557b803b1ef78 # Parent 0751e63c5a9494ba9023ea72dda5f4a753e87058 Getting getResources(...) to work and return URLs with streams. Currently fails in register allocation. diff -r 0751e63c5a94 -r b679a7dad297 emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ServiceLoaderTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ServiceLoaderTest.java Thu Jan 24 19:36:40 2013 +0100 @@ -0,0 +1,61 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.compact.tck; + +import java.io.IOException; +import java.util.ServiceLoader; +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.openide.util.lookup.ServiceProvider; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class ServiceLoaderTest { + @Compare//(scripting = false) + public Object findsIOException() { +// delayStart(); + for (IOException e : ServiceLoader.load(IOException.class)) { + return "Found service: " + e.getClass().getName(); + } + return null; + } +/* + @org.apidesign.bck2brwsr.core.JavaScriptBody(args = { "a" }, body = "alert(a);") + private static void alert(String a) { + } + private void delayStart() { + for (int i = 0; i < 10; i++) { + alert("State: " + i); + for (int j = 0; j < 493208409; j++) ; + } + } +*/ + + @Factory + public static Object[] create() { + return VMTest.create(ServiceLoaderTest.class); + } + + + @ServiceProvider(service = IOException.class) + public static class MyException extends IOException { + } +} diff -r 0751e63c5a94 -r b679a7dad297 emul/mini/src/main/java/java/lang/Class.java --- a/emul/mini/src/main/java/java/lang/Class.java Thu Jan 24 19:32:27 2013 +0100 +++ b/emul/mini/src/main/java/java/lang/Class.java Thu Jan 24 19:36:40 2013 +0100 @@ -32,6 +32,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.TypeVariable; +import java.net.URL; import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.bck2brwsr.emul.reflect.MethodImpl; @@ -1028,15 +1029,16 @@ * @since JDK1.1 */ public java.net.URL getResource(String name) { - name = resolveName(name); - ClassLoader cl = null; - if (cl==null) { - // A system class. - return ClassLoader.getSystemResource(name); - } - return cl.getResource(name); + InputStream is = getResourceAsStream(name); + return is == null ? null : newResourceURL(URL.class, "res:/" + name, is); } - + + @JavaScriptBody(args = { "url", "spec", "is" }, body = + "var u = url.cnstr(true);\n" + + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n" + + "return u;" + ) + private static native URL newResourceURL(Class url, String spec, InputStream is); /** * Add a package name prefix if the name is not absolute Remove leading "/" diff -r 0751e63c5a94 -r b679a7dad297 emul/mini/src/main/java/java/lang/ClassLoader.java --- a/emul/mini/src/main/java/java/lang/ClassLoader.java Thu Jan 24 19:32:27 2013 +0100 +++ b/emul/mini/src/main/java/java/lang/ClassLoader.java Thu Jan 24 19:36:40 2013 +0100 @@ -703,7 +703,7 @@ public static Enumeration getSystemResources(String name) throws IOException { - ClassLoader system = getSystemClassLoader(); + ClassLoader system = null; // getSystemClassLoader(); if (system == null) { return getBootstrapResources(name); } @@ -874,7 +874,29 @@ } private static Enumeration getBootstrapResources(String name) { - throw new UnsupportedOperationException(); + URL u = Object.class.getResource("/" + name); + return new OneOrZeroEnum(u); + } + + private static class OneOrZeroEnum implements Enumeration { + private URL u; + + public OneOrZeroEnum(URL u) { + this.u = u; + } + + public boolean hasMoreElements() { + return u != null; + } + + public URL nextElement() { + URL r = u; + if (r == null) { + throw new NoSuchElementException(); + } + u = null; + return r; + } } private static class CompoundEnumeration implements Enumeration { diff -r 0751e63c5a94 -r b679a7dad297 emul/mini/src/main/java/java/net/URL.java --- a/emul/mini/src/main/java/java/net/URL.java Thu Jan 24 19:32:27 2013 +0100 +++ b/emul/mini/src/main/java/java/net/URL.java Thu Jan 24 19:36:40 2013 +0100 @@ -29,6 +29,7 @@ import java.io.InputStream; import org.apidesign.bck2brwsr.core.JavaScriptBody; + /** * Class URL represents a Uniform Resource * Locator, a pointer to a "resource" on the World @@ -212,6 +213,9 @@ * @serial */ private int hashCode = -1; + + /** input stream associated with the URL */ + private InputStream is; /** * Creates a URL object from the specified @@ -421,6 +425,11 @@ public URL(String spec) throws MalformedURLException { this(null, spec); } + + private URL(String spec, InputStream is) throws MalformedURLException { + this(null, spec); + this.is = is; + } /** * Creates a URL by parsing the given spec within a specified context. @@ -950,8 +959,10 @@ * @see java.net.URLConnection#getInputStream() */ public final InputStream openStream() throws java.io.IOException { + if (is != null) { + return is; + } throw new IOException(); -// return openConnection().getInputStream(); } /** @@ -1006,7 +1017,6 @@ } } - class Parts { String path, query, ref;