Alexey Andreev 2016-07-28 22:23:39 +03:00
parent 125ccf48e9
commit a3f60996e2
4 changed files with 47 additions and 6 deletions

View File

@ -53,8 +53,7 @@
<module name="teavm-tooling" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="impl" target="1.8" />
<bytecodeTargetLevel target="1.8">
<module name="teavm" target="1.5" />
<module name="teavm-chrome-rdp" target="1.8" />
<module name="teavm-classlib" target="1.8" />

View File

@ -126,6 +126,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
private final List<String> cachedVariableNames = new ArrayList<>();
private boolean end;
private int currentPart;
private List<PostponedFieldInitializer> 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> T getService(Class<T> 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;
}
}
}

View File

@ -513,6 +513,7 @@ public class TeaVM implements TeaVMHost, ServiceRepository {
renderer.renderRuntime();
renderer.render(clsNodes);
renderer.renderStringPool();
renderer.renderStringConstants();
for (Map.Entry<String, TeaVMEntryPoint> entry : entryPoints.entrySet()) {
sourceWriter.append("var ").append(entry.getKey()).ws().append("=").ws();
MethodReference ref = entry.getValue().reference;

View File

@ -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);