Getting getResources(...) to work and return URLs with streams. Currently fails in register allocation.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ServiceLoaderTest.java Thu Jan 24 19:36:40 2013 +0100
1.3 @@ -0,0 +1,61 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.compact.tck;
1.22 +
1.23 +import java.io.IOException;
1.24 +import java.util.ServiceLoader;
1.25 +import org.apidesign.bck2brwsr.vmtest.Compare;
1.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
1.27 +import org.openide.util.lookup.ServiceProvider;
1.28 +import org.testng.annotations.Factory;
1.29 +
1.30 +/**
1.31 + *
1.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.33 + */
1.34 +public class ServiceLoaderTest {
1.35 + @Compare//(scripting = false)
1.36 + public Object findsIOException() {
1.37 +// delayStart();
1.38 + for (IOException e : ServiceLoader.load(IOException.class)) {
1.39 + return "Found service: " + e.getClass().getName();
1.40 + }
1.41 + return null;
1.42 + }
1.43 +/*
1.44 + @org.apidesign.bck2brwsr.core.JavaScriptBody(args = { "a" }, body = "alert(a);")
1.45 + private static void alert(String a) {
1.46 + }
1.47 + private void delayStart() {
1.48 + for (int i = 0; i < 10; i++) {
1.49 + alert("State: " + i);
1.50 + for (int j = 0; j < 493208409; j++) ;
1.51 + }
1.52 + }
1.53 +*/
1.54 +
1.55 + @Factory
1.56 + public static Object[] create() {
1.57 + return VMTest.create(ServiceLoaderTest.class);
1.58 + }
1.59 +
1.60 +
1.61 + @ServiceProvider(service = IOException.class)
1.62 + public static class MyException extends IOException {
1.63 + }
1.64 +}
2.1 --- a/emul/mini/src/main/java/java/lang/Class.java Thu Jan 24 19:32:27 2013 +0100
2.2 +++ b/emul/mini/src/main/java/java/lang/Class.java Thu Jan 24 19:36:40 2013 +0100
2.3 @@ -32,6 +32,7 @@
2.4 import java.lang.reflect.Field;
2.5 import java.lang.reflect.Method;
2.6 import java.lang.reflect.TypeVariable;
2.7 +import java.net.URL;
2.8 import org.apidesign.bck2brwsr.core.JavaScriptBody;
2.9 import org.apidesign.bck2brwsr.emul.reflect.MethodImpl;
2.10
2.11 @@ -1028,15 +1029,16 @@
2.12 * @since JDK1.1
2.13 */
2.14 public java.net.URL getResource(String name) {
2.15 - name = resolveName(name);
2.16 - ClassLoader cl = null;
2.17 - if (cl==null) {
2.18 - // A system class.
2.19 - return ClassLoader.getSystemResource(name);
2.20 - }
2.21 - return cl.getResource(name);
2.22 + InputStream is = getResourceAsStream(name);
2.23 + return is == null ? null : newResourceURL(URL.class, "res:/" + name, is);
2.24 }
2.25 -
2.26 +
2.27 + @JavaScriptBody(args = { "url", "spec", "is" }, body =
2.28 + "var u = url.cnstr(true);\n"
2.29 + + "u.constructor.cons__VLjava_lang_String_2Ljava_io_InputStream_2.call(u, spec, is);\n"
2.30 + + "return u;"
2.31 + )
2.32 + private static native URL newResourceURL(Class<URL> url, String spec, InputStream is);
2.33
2.34 /**
2.35 * Add a package name prefix if the name is not absolute Remove leading "/"
3.1 --- a/emul/mini/src/main/java/java/lang/ClassLoader.java Thu Jan 24 19:32:27 2013 +0100
3.2 +++ b/emul/mini/src/main/java/java/lang/ClassLoader.java Thu Jan 24 19:36:40 2013 +0100
3.3 @@ -703,7 +703,7 @@
3.4 public static Enumeration<URL> getSystemResources(String name)
3.5 throws IOException
3.6 {
3.7 - ClassLoader system = getSystemClassLoader();
3.8 + ClassLoader system = null; // getSystemClassLoader();
3.9 if (system == null) {
3.10 return getBootstrapResources(name);
3.11 }
3.12 @@ -874,7 +874,29 @@
3.13 }
3.14
3.15 private static Enumeration<URL> getBootstrapResources(String name) {
3.16 - throw new UnsupportedOperationException();
3.17 + URL u = Object.class.getResource("/" + name);
3.18 + return new OneOrZeroEnum(u);
3.19 + }
3.20 +
3.21 + private static class OneOrZeroEnum implements Enumeration<URL> {
3.22 + private URL u;
3.23 +
3.24 + public OneOrZeroEnum(URL u) {
3.25 + this.u = u;
3.26 + }
3.27 +
3.28 + public boolean hasMoreElements() {
3.29 + return u != null;
3.30 + }
3.31 +
3.32 + public URL nextElement() {
3.33 + URL r = u;
3.34 + if (r == null) {
3.35 + throw new NoSuchElementException();
3.36 + }
3.37 + u = null;
3.38 + return r;
3.39 + }
3.40 }
3.41
3.42 private static class CompoundEnumeration implements Enumeration<URL> {
4.1 --- a/emul/mini/src/main/java/java/net/URL.java Thu Jan 24 19:32:27 2013 +0100
4.2 +++ b/emul/mini/src/main/java/java/net/URL.java Thu Jan 24 19:36:40 2013 +0100
4.3 @@ -29,6 +29,7 @@
4.4 import java.io.InputStream;
4.5 import org.apidesign.bck2brwsr.core.JavaScriptBody;
4.6
4.7 +
4.8 /**
4.9 * Class <code>URL</code> represents a Uniform Resource
4.10 * Locator, a pointer to a "resource" on the World
4.11 @@ -212,6 +213,9 @@
4.12 * @serial
4.13 */
4.14 private int hashCode = -1;
4.15 +
4.16 + /** input stream associated with the URL */
4.17 + private InputStream is;
4.18
4.19 /**
4.20 * Creates a <code>URL</code> object from the specified
4.21 @@ -421,6 +425,11 @@
4.22 public URL(String spec) throws MalformedURLException {
4.23 this(null, spec);
4.24 }
4.25 +
4.26 + private URL(String spec, InputStream is) throws MalformedURLException {
4.27 + this(null, spec);
4.28 + this.is = is;
4.29 + }
4.30
4.31 /**
4.32 * Creates a URL by parsing the given spec within a specified context.
4.33 @@ -950,8 +959,10 @@
4.34 * @see java.net.URLConnection#getInputStream()
4.35 */
4.36 public final InputStream openStream() throws java.io.IOException {
4.37 + if (is != null) {
4.38 + return is;
4.39 + }
4.40 throw new IOException();
4.41 -// return openConnection().getInputStream();
4.42 }
4.43
4.44 /**
4.45 @@ -1006,7 +1017,6 @@
4.46 }
4.47
4.48 }
4.49 -
4.50 class Parts {
4.51 String path, query, ref;
4.52