Fix System.setErr/setOut methods

This commit is contained in:
Alexey Andreev 2017-05-07 21:32:35 +03:00
parent 293e82a3c0
commit e884bb35c9
2 changed files with 25 additions and 9 deletions

View File

@ -17,17 +17,17 @@ package org.teavm.classlib.java.lang;
import java.io.IOException; import java.io.IOException;
import org.teavm.backend.javascript.codegen.SourceWriter; import org.teavm.backend.javascript.codegen.SourceWriter;
import org.teavm.dependency.*;
import org.teavm.backend.javascript.spi.Generator; import org.teavm.backend.javascript.spi.Generator;
import org.teavm.backend.javascript.spi.GeneratorContext; 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.CallLocation;
import org.teavm.model.FieldReference; import org.teavm.model.FieldReference;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
/**
*
* @author Alexey Andreev
*/
public class SystemNativeGenerator implements Generator, DependencyPlugin { public class SystemNativeGenerator implements Generator, DependencyPlugin {
@Override @Override
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException { public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException {
@ -39,13 +39,11 @@ public class SystemNativeGenerator implements Generator, DependencyPlugin {
generateCurrentTimeMillis(writer); generateCurrentTimeMillis(writer);
break; break;
case "setOut": case "setOut":
writer.appendClass("java.lang.System").append('.') writer.appendStaticField(new FieldReference("java.lang.System", "out"))
.appendField(new FieldReference("java.lang.System", "out"))
.ws().append('=').ws().append(context.getParameterName(1)).append(";").softNewLine(); .ws().append('=').ws().append(context.getParameterName(1)).append(";").softNewLine();
break; break;
case "setErr": case "setErr":
writer.appendClass("java.lang.System").append('.') writer.appendStaticField(new FieldReference("java.lang.System", "err"))
.appendField(new FieldReference("java.lang.System", "err"))
.ws().append('=').ws().append(context.getParameterName(1)).append(";").softNewLine(); .ws().append('=').ws().append(context.getParameterName(1)).append(";").softNewLine();
break; break;
} }

View File

@ -16,6 +16,8 @@
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner; import org.teavm.junit.TeaVMTestRunner;
@ -104,4 +106,20 @@ public class SystemTest {
public void failsToCopyToNullTarget() { public void failsToCopyToNullTarget() {
System.arraycopy(new Object[1], 0, null, 0, 1); 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()));
}
} }