diff --git a/core/src/main/java/org/teavm/dependency/Linker.java b/core/src/main/java/org/teavm/dependency/Linker.java index cc4f1b377..7e9bbd639 100644 --- a/core/src/main/java/org/teavm/dependency/Linker.java +++ b/core/src/main/java/org/teavm/dependency/Linker.java @@ -15,8 +15,17 @@ */ package org.teavm.dependency; -import org.teavm.model.*; +import org.teavm.model.BasicBlock; +import org.teavm.model.ClassHolder; +import org.teavm.model.ElementModifier; +import org.teavm.model.FieldHolder; +import org.teavm.model.FieldReference; +import org.teavm.model.Instruction; +import org.teavm.model.MethodHolder; +import org.teavm.model.MethodReference; +import org.teavm.model.Program; import org.teavm.model.instructions.GetFieldInstruction; +import org.teavm.model.instructions.InitClassInstruction; import org.teavm.model.instructions.InvokeInstruction; import org.teavm.model.instructions.PutFieldInstruction; @@ -50,7 +59,9 @@ public class Linker { Program program = method.getProgram(); for (int i = 0; i < program.basicBlockCount(); ++i) { BasicBlock block = program.basicBlockAt(i); - for (Instruction insn : block.getInstructions()) { + for (int j = 0; j < block.getInstructions().size(); ++j) { + Instruction insn = block.getInstructions().get(j); + if (insn instanceof InvokeInstruction) { InvokeInstruction invoke = (InvokeInstruction) insn; MethodDependencyInfo linkedMethod = dependency.getMethodImplementation(invoke.getMethod()); @@ -63,6 +74,14 @@ public class Linker { if (linkedField != null) { getField.setField(linkedField.getReference()); } + + FieldReference fieldRef = getField.getField(); + if (!fieldRef.getClassName().equals(method.getOwnerName())) { + InitClassInstruction initInsn = new InitClassInstruction(); + initInsn.setClassName(fieldRef.getClassName()); + block.getInstructions().add(j++, initInsn); + } + } else if (insn instanceof PutFieldInstruction) { PutFieldInstruction getField = (PutFieldInstruction) insn; FieldDependencyInfo linkedField = dependency.getField(getField.getField()); diff --git a/core/src/main/java/org/teavm/parsing/ProgramParser.java b/core/src/main/java/org/teavm/parsing/ProgramParser.java index 04978953c..c6aee1898 100644 --- a/core/src/main/java/org/teavm/parsing/ProgramParser.java +++ b/core/src/main/java/org/teavm/parsing/ProgramParser.java @@ -1679,11 +1679,6 @@ public class ProgramParser implements VariableDebugInformation { case Opcodes.GETSTATIC: { ValueType type = ValueType.parse(desc); int value = desc.equals("D") || desc.equals("J") ? pushDouble() : pushSingle(); - if (!owner.equals(currentClassName)) { - InitClassInstruction initInsn = new InitClassInstruction(); - initInsn.setClassName(ownerCls); - addInstruction(initInsn); - } GetFieldInstruction insn = new GetFieldInstruction(); insn.setField(new FieldReference(ownerCls, name)); insn.setFieldType(type);