diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java index 20eaf08de..7f96bc5b6 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TBoolean.java @@ -88,6 +88,6 @@ public class TBoolean extends TObject implements TSerializable, TComparable { } public static TInteger getInteger(TString nm, TInteger val) { - TString result = TSystem.getProperty(nm); + TString result = TString.wrap(TSystem.getProperty(nm.toString())); return result != null ? TInteger.valueOf(result) : val; } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java index c40ab282a..0f66dad19 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TLong.java @@ -225,7 +225,7 @@ public class TLong extends TNumber implements TComparable { } public static TLong getLong(TString nm, TLong val) { - TString result = TSystem.getProperty(nm); + TString result = TString.wrap(TSystem.getProperty(nm.toString())); return result != null ? TLong.valueOf(result) : val; } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java index 8ad18df67..743f8e371 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java @@ -15,6 +15,8 @@ */ package org.teavm.classlib.java.lang; +import java.util.Enumeration; +import java.util.Properties; import org.teavm.backend.javascript.spi.GeneratedBy; import org.teavm.classlib.java.io.TConsole; import org.teavm.classlib.java.io.TInputStream; @@ -34,6 +36,7 @@ public final class TSystem extends TObject { public static final TPrintStream out = new TPrintStream(new TConsoleOutputStreamStdout(), false); public static final TPrintStream err = new TPrintStream(new TConsoleOutputStreamStderr(), false); public static final TInputStream in = new TConsoleInputStream(); + private static Properties properties; private TSystem() { } @@ -109,16 +112,60 @@ public final class TSystem extends TObject { @Import(name = "currentTimeMillis", module = "runtime") private static native double currentTimeMillisImpl(); - public static TString getProperty(@SuppressWarnings("unused") TString key) { - // TODO: make implementation - return null; + private static void initPropertiesIfNeeded() { + if (properties == null) { + Properties defaults = new Properties(); + defaults.put("java.version", "1.8"); + defaults.put("os.name", "TeaVM"); + defaults.put("file.separator", "/"); + defaults.put("path.separator", ":"); + defaults.put("line.separator", lineSeparator()); + properties = new Properties(defaults); + } } - public static TString getProperty(TString key, TString def) { - TString value = getProperty(key); + public static String getProperty(@SuppressWarnings("unused") String key) { + initPropertiesIfNeeded(); + return properties.getProperty(key); + } + + public static String getProperty(String key, String def) { + String value = getProperty(key); return value != null ? value : def; } + public static Properties getProperties() { + initPropertiesIfNeeded(); + Properties result = new Properties(); + copyProperties(properties, result); + return result; + } + + public static void setProperties(Properties props) { + initPropertiesIfNeeded(); + copyProperties(props, properties); + } + + private static void copyProperties(Properties from, Properties to) { + to.clear(); + if (from != null) { + Enumeration e = from.propertyNames(); + while (e.hasMoreElements()) { + String key = (String) e.nextElement(); + to.setProperty(key, from.getProperty(key)); + } + } + } + + public static String setProperty(String key, String value) { + initPropertiesIfNeeded(); + return (String) properties.put(key, value); + } + + public static String clearProperty(String key) { + return (String) properties.remove(key); + } + @GeneratedBy(SystemNativeGenerator.class) @PluggableDependency(SystemNativeGenerator.class) public static native void setErr(TPrintStream err); @@ -148,7 +195,7 @@ public final class TSystem extends TObject { return ((TObject) x).identity(); } - public static TString lineSeparator() { - return TString.wrap("\n"); + public static String lineSeparator() { + return "\n"; } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/SystemTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/SystemTest.java index d7ea62ad1..339fe8aa4 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/SystemTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/SystemTest.java @@ -16,13 +16,16 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.Properties; import org.junit.Test; import org.junit.runner.RunWith; +import org.teavm.junit.SkipJVM; import org.teavm.junit.TeaVMTestRunner; @RunWith(TeaVMTestRunner.class) @@ -125,4 +128,25 @@ public class SystemTest { assertEquals("err overridden\n", new String(err.toByteArray())); assertEquals("out overridden\n", new String(out.toByteArray())); } + + @Test + @SkipJVM + public void propertiesWork() { + Properties properties = System.getProperties(); + assertNotNull(properties); + assertNotNull(properties.getProperty("java.version")); + + System.setProperty("myprop", "foo"); + assertNull(properties.getProperty("foo")); + assertEquals("foo", System.getProperty("myprop")); + properties = System.getProperties(); + assertEquals("foo", properties.getProperty("myprop")); + + Properties newProps = new Properties(); + newProps.setProperty("myprop2", "bar"); + System.setProperties(newProps); + assertNotNull(System.getProperty("java.version")); + assertNull(System.getProperty("myprop")); + assertEquals("bar", System.getProperty("myprop2")); + } }