diff --git a/classlib/src/main/java/org/teavm/classlib/ResourceSupplier.java b/classlib/src/main/java/org/teavm/classlib/ResourceSupplier.java index 7daec97c0..4499c0fc4 100644 --- a/classlib/src/main/java/org/teavm/classlib/ResourceSupplier.java +++ b/classlib/src/main/java/org/teavm/classlib/ResourceSupplier.java @@ -1,11 +1,20 @@ +/* + * 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 000000000..f0db6d23c --- /dev/null +++ b/classlib/src/main/java/org/teavm/classlib/ResourceSupplierContext.java @@ -0,0 +1,27 @@ +/* + * 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; + +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 1d854f1e8..ba5cf5f0e 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 @@ -1,3 +1,18 @@ +/* + * Copyright 2017 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.java.lang; import java.io.IOException; @@ -5,21 +20,19 @@ import java.io.InputStream; 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.backend.javascript.rendering.RenderingUtil; -import org.teavm.classlib.ResourceSupplier; import org.teavm.backend.javascript.codegen.SourceWriter; -import org.teavm.backend.javascript.rendering.Renderer; +import org.teavm.backend.javascript.rendering.RenderingUtil; import org.teavm.backend.javascript.spi.Injector; import org.teavm.backend.javascript.spi.InjectorContext; +import org.teavm.classlib.ResourceSupplier; +import org.teavm.classlib.ResourceSupplierContext; +import org.teavm.model.ListableClassReaderSource; import org.teavm.model.MethodReference; -/** - * - * @author Alexey Andreev - */ public class ClassLoaderNativeGenerator implements Injector { @Override public void generate(InjectorContext context, MethodReference methodRef) throws IOException { @@ -36,8 +49,9 @@ public class ClassLoaderNativeGenerator implements Injector { 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)); } @@ -66,4 +80,27 @@ public class ClassLoaderNativeGenerator implements Injector { } 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 279f894e3..000000000 --- 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 be4d39dbe..000000000 --- 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 ed4196747..0b9efa5bd 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,11 +16,11 @@ package org.teavm.classlib.java.lang; import org.teavm.classlib.ResourceSupplier; -import org.teavm.model.ListableClassReaderSource; +import org.teavm.classlib.ResourceSupplierContext; 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" }; for (int i = 0; i < result.length; ++i) { result[i] = "resources-for-test/" + result[i];