diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java index f19484553..effde2a10 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java @@ -17,17 +17,17 @@ package org.teavm.classlib.java.lang; import java.io.IOException; import org.teavm.backend.javascript.codegen.SourceWriter; -import org.teavm.dependency.*; import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.GeneratorContext; +import org.teavm.dependency.DependencyAgent; +import org.teavm.dependency.DependencyNode; +import org.teavm.dependency.DependencyPlugin; +import org.teavm.dependency.FieldDependency; +import org.teavm.dependency.MethodDependency; import org.teavm.model.CallLocation; import org.teavm.model.FieldReference; import org.teavm.model.MethodReference; -/** - * - * @author Alexey Andreev - */ public class SystemNativeGenerator implements Generator, DependencyPlugin { @Override public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { @@ -39,13 +39,11 @@ public class SystemNativeGenerator implements Generator, DependencyPlugin { generateCurrentTimeMillis(writer); break; case "setOut": - writer.appendClass("java.lang.System").append('.') - .appendField(new FieldReference("java.lang.System", "out")) + writer.appendStaticField(new FieldReference("java.lang.System", "out")) .ws().append('=').ws().append(context.getParameterName(1)).append(";").softNewLine(); break; case "setErr": - writer.appendClass("java.lang.System").append('.') - .appendField(new FieldReference("java.lang.System", "err")) + writer.appendStaticField(new FieldReference("java.lang.System", "err")) .ws().append('=').ws().append(context.getParameterName(1)).append(";").softNewLine(); break; } 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 58d884804..4b3a044da 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,6 +16,8 @@ package org.teavm.classlib.java.lang; import static org.junit.Assert.*; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import org.junit.Test; import org.junit.runner.RunWith; import org.teavm.junit.TeaVMTestRunner; @@ -104,4 +106,20 @@ public class SystemTest { public void failsToCopyToNullTarget() { System.arraycopy(new Object[1], 0, null, 0, 1); } + + @Test + public void overridesStdErrAndOut() { + ByteArrayOutputStream err = new ByteArrayOutputStream(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setErr(new PrintStream(err)); + System.setOut(new PrintStream(out)); + + System.out.println("out overridden"); + System.out.flush(); + System.err.println("err overridden"); + System.err.flush(); + + assertEquals("err overridden\n", new String(err.toByteArray())); + assertEquals("out overridden\n", new String(out.toByteArray())); + } }