Add some properties-related methods to System class

This commit is contained in:
Alexey Andreev 2017-10-10 12:56:51 +03:00
parent c057c7b78f
commit 26824f1399
5 changed files with 81 additions and 10 deletions

View File

@ -88,6 +88,6 @@ public class TBoolean extends TObject implements TSerializable, TComparable<TBoo
} }
public boolean getBoolean(TString key) { public boolean getBoolean(TString key) {
return valueOf(TSystem.getProperty(key)).booleanValue(); return valueOf(TString.wrap(TSystem.getProperty(key.toString()))).booleanValue();
} }
} }

View File

@ -172,7 +172,7 @@ public class TInteger extends TNumber implements TComparable<TInteger> {
} }
public static TInteger getInteger(TString nm, TInteger val) { 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; return result != null ? TInteger.valueOf(result) : val;
} }

View File

@ -225,7 +225,7 @@ public class TLong extends TNumber implements TComparable<TLong> {
} }
public static TLong getLong(TString nm, TLong val) { 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; return result != null ? TLong.valueOf(result) : val;
} }

View File

@ -15,6 +15,8 @@
*/ */
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import java.util.Enumeration;
import java.util.Properties;
import org.teavm.backend.javascript.spi.GeneratedBy; import org.teavm.backend.javascript.spi.GeneratedBy;
import org.teavm.classlib.java.io.TConsole; import org.teavm.classlib.java.io.TConsole;
import org.teavm.classlib.java.io.TInputStream; 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 out = new TPrintStream(new TConsoleOutputStreamStdout(), false);
public static final TPrintStream err = new TPrintStream(new TConsoleOutputStreamStderr(), false); public static final TPrintStream err = new TPrintStream(new TConsoleOutputStreamStderr(), false);
public static final TInputStream in = new TConsoleInputStream(); public static final TInputStream in = new TConsoleInputStream();
private static Properties properties;
private TSystem() { private TSystem() {
} }
@ -109,16 +112,60 @@ public final class TSystem extends TObject {
@Import(name = "currentTimeMillis", module = "runtime") @Import(name = "currentTimeMillis", module = "runtime")
private static native double currentTimeMillisImpl(); private static native double currentTimeMillisImpl();
public static TString getProperty(@SuppressWarnings("unused") TString key) { private static void initPropertiesIfNeeded() {
// TODO: make implementation if (properties == null) {
return 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) { public static String getProperty(@SuppressWarnings("unused") String key) {
TString value = getProperty(key); initPropertiesIfNeeded();
return properties.getProperty(key);
}
public static String getProperty(String key, String def) {
String value = getProperty(key);
return value != null ? value : def; 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) @GeneratedBy(SystemNativeGenerator.class)
@PluggableDependency(SystemNativeGenerator.class) @PluggableDependency(SystemNativeGenerator.class)
public static native void setErr(TPrintStream err); public static native void setErr(TPrintStream err);
@ -148,7 +195,7 @@ public final class TSystem extends TObject {
return ((TObject) x).identity(); return ((TObject) x).identity();
} }
public static TString lineSeparator() { public static String lineSeparator() {
return TString.wrap("\n"); return "\n";
} }
} }

View File

@ -16,13 +16,16 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Properties;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.teavm.junit.SkipJVM;
import org.teavm.junit.TeaVMTestRunner; import org.teavm.junit.TeaVMTestRunner;
@RunWith(TeaVMTestRunner.class) @RunWith(TeaVMTestRunner.class)
@ -125,4 +128,25 @@ public class SystemTest {
assertEquals("err overridden\n", new String(err.toByteArray())); assertEquals("err overridden\n", new String(err.toByteArray()));
assertEquals("out overridden\n", new String(out.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"));
}
} }