Improve ResourceSupplier API

(cherry picked from commit 539a12227f79abdb4ce22aac2dbc647f1c09f083)

# Conflicts:
#	classlib/src/main/java/org/teavm/classlib/java/lang/ClassLoaderNativeGenerator.java
#	tests/src/test/java/org/teavm/classlib/java/lang/TestResourcesSupplier.java
This commit is contained in:
Alexey Andreev 2017-05-29 23:22:59 +03:00
parent 25b298b1d0
commit b1b98097ee
6 changed files with 90 additions and 61 deletions

View File

@ -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; package org.teavm.classlib;
import org.teavm.model.ListableClassReaderSource;
/**
*
* @author Alexey Andreev
*/
public interface ResourceSupplier { public interface ResourceSupplier {
String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource); String[] supplyResources(ResourceSupplierContext context);
} }

View File

@ -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();
}

View File

@ -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; package org.teavm.classlib.java.lang;
import java.io.IOException; import java.io.IOException;
@ -5,21 +20,19 @@ import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.HashSet; import java.util.HashSet;
import java.util.Properties;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.Set; import java.util.Set;
import org.apache.commons.io.IOUtils; 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.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.Injector;
import org.teavm.backend.javascript.spi.InjectorContext; 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; import org.teavm.model.MethodReference;
/**
*
* @author Alexey Andreev
*/
public class ClassLoaderNativeGenerator implements Injector { public class ClassLoaderNativeGenerator implements Injector {
@Override @Override
public void generate(InjectorContext context, MethodReference methodRef) throws IOException { public void generate(InjectorContext context, MethodReference methodRef) throws IOException {
@ -36,8 +49,9 @@ public class ClassLoaderNativeGenerator implements Injector {
ClassLoader classLoader = context.getClassLoader(); ClassLoader classLoader = context.getClassLoader();
Set<String> resourceSet = new HashSet<>(); Set<String> resourceSet = new HashSet<>();
SupplierContextImpl supplierContext = new SupplierContextImpl(context);
for (ResourceSupplier supplier : ServiceLoader.load(ResourceSupplier.class, classLoader)) { for (ResourceSupplier supplier : ServiceLoader.load(ResourceSupplier.class, classLoader)) {
String[] resources = supplier.supplyResources(classLoader, context.getClassSource()); String[] resources = supplier.supplyResources(supplierContext);
if (resources != null) { if (resources != null) {
resourceSet.addAll(Arrays.asList(resources)); resourceSet.addAll(Arrays.asList(resources));
} }
@ -66,4 +80,27 @@ public class ClassLoaderNativeGenerator implements Injector {
} }
writer.outdent().append('}'); 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();
}
}
} }

View File

@ -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<String> 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]);
}
}

View File

@ -1 +0,0 @@
org.teavm.html4j.HTML4jResourceSupplier

View File

@ -16,11 +16,11 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import org.teavm.classlib.ResourceSupplier; import org.teavm.classlib.ResourceSupplier;
import org.teavm.model.ListableClassReaderSource; import org.teavm.classlib.ResourceSupplierContext;
public class TestResourcesSupplier implements ResourceSupplier { public class TestResourcesSupplier implements ResourceSupplier {
@Override @Override
public String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource) { public String[] supplyResources(ResourceSupplierContext context) {
String[] result = { "1", "2", "3", "4", "5", "6", "7", "8" }; String[] result = { "1", "2", "3", "4", "5", "6", "7", "8" };
for (int i = 0; i < result.length; ++i) { for (int i = 0; i < result.length; ++i) {
result[i] = "resources-for-test/" + result[i]; result[i] = "resources-for-test/" + result[i];