From b61849ce80631480cf06089f439304ae7a28519d Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 10 Dec 2015 23:05:17 +0300 Subject: [PATCH] Fix metaprogramming bugs --- .../java/org/teavm/javascript/Renderer.java | 10 +++---- .../teavm/model/util/BasicBlockMapper.java | 6 ---- .../org/teavm/model/util/ProgramUtils.java | 3 ++ .../org/teavm/classlib/java/lang/VMTest.java | 29 ++++++++++++++++++- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/teavm/javascript/Renderer.java b/core/src/main/java/org/teavm/javascript/Renderer.java index aeb7c28cd..7c39c4c57 100644 --- a/core/src/main/java/org/teavm/javascript/Renderer.java +++ b/core/src/main/java/org/teavm/javascript/Renderer.java @@ -419,16 +419,16 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext List nonInitMethods = new ArrayList<>(); MethodHolder clinit = classSource.get(cls.getName()).getMethod( new MethodDescriptor("", ValueType.VOID)); + boolean needsClinit = clinit != null; List clinitMethods = new ArrayList<>(); for (MethodNode method : cls.getMethods()) { - if (clinit == null || (!method.getModifiers().contains(NodeModifier.STATIC) - && !method.getReference().getName().equals(""))) { - nonInitMethods.add(method); - } else { + if (needsClinit && (method.getModifiers().contains(NodeModifier.STATIC) + || method.getReference().getName().equals(""))) { clinitMethods.add(method); + } else { + nonInitMethods.add(method); } } - boolean needsClinit = clinit != null; if (needsClinit) { writer.append("function ").appendClass(cls.getName()).append("_$clinit()").ws() diff --git a/core/src/main/java/org/teavm/model/util/BasicBlockMapper.java b/core/src/main/java/org/teavm/model/util/BasicBlockMapper.java index 7f21c4a83..19bbce401 100644 --- a/core/src/main/java/org/teavm/model/util/BasicBlockMapper.java +++ b/core/src/main/java/org/teavm/model/util/BasicBlockMapper.java @@ -198,15 +198,9 @@ public abstract class BasicBlockMapper implements InstructionVisitor { @Override public void visit(MonitorEnterInstruction insn) { - } @Override public void visit(MonitorExitInstruction insn) { - } - - - - } diff --git a/core/src/main/java/org/teavm/model/util/ProgramUtils.java b/core/src/main/java/org/teavm/model/util/ProgramUtils.java index 8107ebe96..cfd27f9b2 100644 --- a/core/src/main/java/org/teavm/model/util/ProgramUtils.java +++ b/core/src/main/java/org/teavm/model/util/ProgramUtils.java @@ -157,6 +157,9 @@ public final class ProgramUtils { } private Variable copyVar(VariableReader var) { + if (var == null) { + throw new NullPointerException(); + } return programCopy.variableAt(var.getIndex()); } 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 5b5ecb216..e569c021a 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 @@ -15,7 +15,7 @@ */ package org.teavm.classlib.java.lang; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import org.junit.Test; /** @@ -103,4 +103,31 @@ public class VMTest { } private int foo() { return 2; } private void bar() { throw new RuntimeException(); } + + // See https://github.com/konsoletyper/teavm/issues/167 + @Test + public void passesStaticFieldToSuperClassConstructor() { + SubClass obj = new SubClass(); + assertNotNull(obj.getValue()); + } + + static class SuperClass { + static final Integer ONE = new Integer(1); + + private Integer value; + + public SuperClass(Integer value) { + this.value = value; + } + + public Integer getValue() { + return value; + } + } + + static class SubClass extends SuperClass { + SubClass() { + super(ONE); + } + } }