diff --git a/.idea/compiler.xml b/.idea/compiler.xml index d8fafdea9..1dab4bc59 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -53,8 +53,7 @@ - - + diff --git a/core/src/main/java/org/teavm/javascript/Renderer.java b/core/src/main/java/org/teavm/javascript/Renderer.java index a97fb0524..6b76d2bfe 100644 --- a/core/src/main/java/org/teavm/javascript/Renderer.java +++ b/core/src/main/java/org/teavm/javascript/Renderer.java @@ -126,6 +126,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext private final List cachedVariableNames = new ArrayList<>(); private boolean end; private int currentPart; + private List postponedFieldInitializers = new ArrayList<>(); private static class InjectorHolder { public final Injector injector; @@ -227,6 +228,17 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext } } + public void renderStringConstants() throws RenderingException { + try { + for (PostponedFieldInitializer initializer : postponedFieldInitializers) { + writer.appendStaticField(initializer.field).ws().append("=").ws() + .append(constantToString(initializer.value)).append(";").softNewLine(); + } + } catch (IOException e) { + throw new RenderingException("IO error", e); + } + } + public void renderRuntime() throws RenderingException { try { renderRuntimeCls(); @@ -403,6 +415,11 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext value = getDefaultValue(field.getType()); } FieldReference fieldRef = new FieldReference(cls.getName(), field.getName()); + if (value instanceof String) { + constantToString(value); + postponedFieldInitializers.add(new PostponedFieldInitializer(fieldRef, (String) value)); + value = null; + } writer.append("var ").appendStaticField(fieldRef).ws().append("=").ws() .append(constantToString(value)).append(";").softNewLine(); } @@ -2391,4 +2408,14 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext public T getService(Class type) { return services.getService(type); } + + private static class PostponedFieldInitializer { + FieldReference field; + String value; + + public PostponedFieldInitializer(FieldReference field, String value) { + this.field = field; + this.value = value; + } + } } diff --git a/core/src/main/java/org/teavm/vm/TeaVM.java b/core/src/main/java/org/teavm/vm/TeaVM.java index e2c2f4dfb..7c74df104 100644 --- a/core/src/main/java/org/teavm/vm/TeaVM.java +++ b/core/src/main/java/org/teavm/vm/TeaVM.java @@ -513,6 +513,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository { renderer.renderRuntime(); renderer.render(clsNodes); renderer.renderStringPool(); + renderer.renderStringConstants(); for (Map.Entry entry : entryPoints.entrySet()) { sourceWriter.append("var ").append(entry.getKey()).ws().append("=").ws(); MethodReference ref = entry.getValue().reference; diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/VMTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/VMTest.java index c85382ad3..98abb6aad 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/VMTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/VMTest.java @@ -21,10 +21,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.teavm.junit.TeaVMTestRunner; -/** - * - * @author Alexey Andreev - */ @RunWith(TeaVMTestRunner.class) public class VMTest { @Test @@ -115,6 +111,24 @@ public class VMTest { assertNotNull(obj.getValue()); } + // See https://github.com/konsoletyper/teavm/issues/196 + @Test + public void stringConstantsInitializedProperly() { + assertEquals("FIRST ", ClassWithStaticField.foo(true)); + assertEquals("SECOND ", ClassWithStaticField.foo(false)); + } + + private static class ClassWithStaticField { + public final static String CONST1 = "FIRST"; + public final static String CONST2 = "SECOND"; + + public static String foo(boolean value) { + StringBuilder sb = new StringBuilder(); + sb.append(value ? CONST1 : CONST2).append(" "); + return sb.toString(); + } + } + static class SuperClass { static final Integer ONE = new Integer(1);