From 539a12227f79abdb4ce22aac2dbc647f1c09f083 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 28 Dec 2015 20:59:22 +0300 Subject: [PATCH] Improve ResourceSupplier API --- .../org/teavm/classlib/ResourceSupplier.java | 19 ++++++-- .../classlib/ResourceSupplierContext.java | 31 +++++++++++++ .../java/lang/ClassLoaderNativeGenerator.java | 29 ++++++++++++- .../teavm/html4j/HTML4jResourceSupplier.java | 43 ------------------- .../org.teavm.classlib.ResourceSupplier | 1 - .../java/lang/TestResourcesSupplier.java | 4 +- 6 files changed, 77 insertions(+), 50 deletions(-) create mode 100644 classlib/src/main/java/org/teavm/classlib/ResourceSupplierContext.java delete mode 100644 html4j/src/main/java/org/teavm/html4j/HTML4jResourceSupplier.java delete mode 100644 html4j/src/main/resources/META-INF/services/org.teavm.classlib.ResourceSupplier diff --git a/classlib/src/main/java/org/teavm/classlib/ResourceSupplier.java b/classlib/src/main/java/org/teavm/classlib/ResourceSupplier.java index 7daec97c02..896267cb66 100644 --- a/classlib/src/main/java/org/teavm/classlib/ResourceSupplier.java +++ b/classlib/src/main/java/org/teavm/classlib/ResourceSupplier.java @@ -1,11 +1,24 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.teavm.classlib; -import org.teavm.model.ListableClassReaderSource; - /** * * @author Alexey Andreev */ public interface ResourceSupplier { - String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource); + String[] supplyResources(ResourceSupplierContext context); } diff --git a/classlib/src/main/java/org/teavm/classlib/ResourceSupplierContext.java b/classlib/src/main/java/org/teavm/classlib/ResourceSupplierContext.java new file mode 100644 index 0000000000..26ea1401c4 --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/ResourceSupplierContext.java @@ -0,0 +1,31 @@ +/* + * Copyright 2015 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib; + +import java.util.Properties; +import org.teavm.model.ListableClassReaderSource; + +/** + * + * @author Alexey Andreev + */ +public interface ResourceSupplierContext { + ClassLoader getClassLoader(); + + ListableClassReaderSource getClassSource(); + + Properties getProperties(); +} diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java index 83cad8cd1a..28b9a01ae0 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java @@ -5,14 +5,17 @@ import java.util.Arrays; import java.util.Base64; import java.util.HashSet; +import java.util.Properties; import java.util.ServiceLoader; import java.util.Set; import org.apache.commons.io.IOUtils; import org.teavm.classlib.ResourceSupplier; +import org.teavm.classlib.ResourceSupplierContext; import org.teavm.codegen.SourceWriter; import org.teavm.javascript.Renderer; import org.teavm.javascript.spi.Injector; import org.teavm.javascript.spi.InjectorContext; +import org.teavm.model.ListableClassReaderSource; import org.teavm.model.MethodReference; /** @@ -35,8 +38,9 @@ private void generateSupplyResources(InjectorContext context) throws IOException ClassLoader classLoader = context.getClassLoader(); Set resourceSet = new HashSet<>(); + SupplierContextImpl supplierContext = new SupplierContextImpl(context); for (ResourceSupplier supplier : ServiceLoader.load(ResourceSupplier.class, classLoader)) { - String[] resources = supplier.supplyResources(classLoader, context.getClassSource()); + String[] resources = supplier.supplyResources(supplierContext); if (resources != null) { resourceSet.addAll(Arrays.asList(resources)); } @@ -65,4 +69,27 @@ private void generateSupplyResources(InjectorContext context) throws IOException } writer.outdent().append('}'); } + + static class SupplierContextImpl implements ResourceSupplierContext { + InjectorContext injectorContext; + + public SupplierContextImpl(InjectorContext injectorContext) { + this.injectorContext = injectorContext; + } + + @Override + public ClassLoader getClassLoader() { + return injectorContext.getClassLoader(); + } + + @Override + public ListableClassReaderSource getClassSource() { + return injectorContext.getClassSource(); + } + + @Override + public Properties getProperties() { + return injectorContext.getProperties(); + } + } } diff --git a/html4j/src/main/java/org/teavm/html4j/HTML4jResourceSupplier.java b/html4j/src/main/java/org/teavm/html4j/HTML4jResourceSupplier.java deleted file mode 100644 index 279f894e34..0000000000 --- a/html4j/src/main/java/org/teavm/html4j/HTML4jResourceSupplier.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2015 Alexey Andreev. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.teavm.html4j; - -import java.util.HashSet; -import java.util.Set; -import org.teavm.classlib.ResourceSupplier; -import org.teavm.model.ListableClassReaderSource; - -/** - * - * @author Alexey Andreev - */ -public class HTML4jResourceSupplier implements ResourceSupplier { - @Override - public String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource) { - Set resources = new HashSet<>(); - for (String className : classSource.getClassNames()) { - final int lastDot = className.lastIndexOf('.'); - if (lastDot == -1) { - continue; - } - String packageName = className.substring(0, lastDot); - String resourceName = packageName.replace('.', '/') + "/" + "jvm.txt"; - resources.add(resourceName); - } - - return resources.toArray(new String[0]); - } -} diff --git a/html4j/src/main/resources/META-INF/services/org.teavm.classlib.ResourceSupplier b/html4j/src/main/resources/META-INF/services/org.teavm.classlib.ResourceSupplier deleted file mode 100644 index be4d39dbe5..0000000000 --- a/html4j/src/main/resources/META-INF/services/org.teavm.classlib.ResourceSupplier +++ /dev/null @@ -1 +0,0 @@ -org.teavm.html4j.HTML4jResourceSupplier \ No newline at end of file diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/TestResourcesSupplier.java b/tests/src/test/java/org/teavm/classlib/java/lang/TestResourcesSupplier.java index e12f58014f..2a3156da29 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/TestResourcesSupplier.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/TestResourcesSupplier.java @@ -16,7 +16,7 @@ package org.teavm.classlib.java.lang; import org.teavm.classlib.ResourceSupplier; -import org.teavm.model.ListableClassReaderSource; +import org.teavm.classlib.ResourceSupplierContext; /** * @@ -24,7 +24,7 @@ */ public class TestResourcesSupplier implements ResourceSupplier { @Override - public String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource) { + public String[] supplyResources(ResourceSupplierContext context) { String[] result = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; for (int i = 0; i < result.length; ++i) { result[i] = "resources-for-test/" + result[i];