diff --git a/classlib/src/main/java/org/teavm/classlib/impl/ResourceBundleImpl.java b/classlib/src/main/java/org/teavm/classlib/impl/ResourceBundleImpl.java index 0b38e6475..4d562b562 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/ResourceBundleImpl.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/ResourceBundleImpl.java @@ -20,6 +20,7 @@ import static org.teavm.metaprogramming.Metaprogramming.exit; import static org.teavm.metaprogramming.Metaprogramming.findClass; import static org.teavm.metaprogramming.Metaprogramming.getClassLoader; import static org.teavm.metaprogramming.Metaprogramming.lazy; +import static org.teavm.metaprogramming.Metaprogramming.lazyFragment; import static org.teavm.metaprogramming.Metaprogramming.proxy; import java.io.BufferedReader; import java.io.IOException; @@ -31,12 +32,12 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; +import java.util.ListResourceBundle; import java.util.Map; import java.util.Properties; +import java.util.ResourceBundle; import java.util.Set; import java.util.function.Supplier; -import org.teavm.classlib.java.util.TListResourceBundle; -import org.teavm.classlib.java.util.TResourceBundle; import org.teavm.metaprogramming.CompileTime; import org.teavm.metaprogramming.Meta; import org.teavm.metaprogramming.ReflectClass; @@ -49,7 +50,7 @@ public class ResourceBundleImpl { } @Meta - public static native Map> createBundleMap(boolean b); + public static native Map> createBundleMap(boolean b); private static void createBundleMap(Value b) throws IOException { ClassLoader loader = getClassLoader(); @@ -73,42 +74,46 @@ public class ResourceBundleImpl { } } - Value>> result = emit(() -> new HashMap<>()); + Value>> result = emit(() -> new HashMap<>()); for (String implementation : implementations) { String path = implementation.replace('.', '/'); ReflectClass cls = findClass(implementation); - Value lazyResource; + Value lazyResource; if (cls != null) { ReflectMethod constructor = cls.getMethod(""); if (constructor != null) { - lazyResource = lazy(() -> (TResourceBundle) constructor.construct()); + lazyResource = lazy(() -> (ResourceBundle) constructor.construct()); } else { continue; } } else if (loader.getResource(path + ".properties") != null) { - Properties properties = new Properties(); - try (InputStream input = loader.getResourceAsStream(path + ".properties")) { - properties.load(input); - } - - lazyResource = lazy(() -> proxy(TListResourceBundle.class, (instance, methodName, args) -> { - Value> contentsBuilder = emit(() -> new ArrayList<>()); - for (Object propertyName : properties.keySet()) { - if (!(propertyName instanceof String)) { - continue; - } - String key = (String) propertyName; - String value = properties.getProperty(key); - if (value == null) { - continue; - } - - emit(() -> contentsBuilder.get().add(new Object[] { key, value })); + lazyResource = lazyFragment(() -> { + Properties properties = new Properties(); + try (InputStream input = loader.getResourceAsStream(path + ".properties")) { + properties.load(input); + } catch (IOException e) { + // do nothing } - exit(() -> contentsBuilder.get().toArray(new Object[0][])); - }).get()); + return proxy(ListResourceBundle.class, (instance, methodName, args) -> { + Value> contentsBuilder = emit(() -> new ArrayList<>()); + for (Object propertyName : properties.keySet()) { + if (!(propertyName instanceof String)) { + continue; + } + String key = (String) propertyName; + String value = properties.getProperty(key); + if (value == null) { + continue; + } + + emit(() -> contentsBuilder.get().add(new Object[] { key, value })); + } + + exit(() -> contentsBuilder.get().toArray(new Object[0][])); + }); + }); } else { continue; } @@ -119,7 +124,7 @@ public class ResourceBundleImpl { exit(() -> lazyResource.get()); }); @SuppressWarnings("unchecked") - Value> supplierValue = emit(() -> supplierValueRaw.get()); + Value> supplierValue = emit(() -> supplierValueRaw.get()); emit(() -> result.get().put(implementation, supplierValue.get())); } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TResourceBundle.java b/classlib/src/main/java/org/teavm/classlib/java/util/TResourceBundle.java index 6e3e9208e..a550295ab 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TResourceBundle.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TResourceBundle.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.MissingResourceException; +import java.util.ResourceBundle; import java.util.function.Supplier; import org.teavm.classlib.impl.ResourceBundleImpl; @@ -47,7 +48,7 @@ public abstract class TResourceBundle { private static final Map cache = new HashMap<>(); - private static final Map> bundleProviders = + private static final Map> bundleProviders = ResourceBundleImpl.createBundleMap(false); public TResourceBundle() { @@ -160,8 +161,8 @@ public abstract class TResourceBundle { return result; } - Supplier provider = bundleProviders.get(bundleName); - bundle = provider != null ? provider.get() : null; + Supplier provider = bundleProviders.get(bundleName); + bundle = provider != null ? (TResourceBundle) (Object) provider.get() : null; String extension = strip(locale); if (bundle != null) {