From 3571917a2fb2ff50c4942127e2da7c220898d969 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 6 Aug 2022 14:07:53 +0300 Subject: [PATCH] Fix issue in record toString method generator --- .../impl/record/ObjectMethodsSubstitutor.java | 3 +++ tests/src/test/java/org/teavm/vm/RecordTest.java | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/classlib/src/main/java/org/teavm/classlib/impl/record/ObjectMethodsSubstitutor.java b/classlib/src/main/java/org/teavm/classlib/impl/record/ObjectMethodsSubstitutor.java index f6494ee5a..ac2036785 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/record/ObjectMethodsSubstitutor.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/record/ObjectMethodsSubstitutor.java @@ -171,6 +171,9 @@ public class ObjectMethodsSubstitutor implements BootstrapMethodSubstitutor { String fieldTitle = (index == 0 ? "" : ", ") + fieldName + "="; resultVar = resultVar.invokeVirtual("append", StringBuilder.class, pe.constant(fieldTitle)); ValueEmitter thisField = InvokeDynamicUtil.invoke(pe, getter, thisVar); + if (!(getter.getValueType() instanceof ValueType.Primitive)) { + thisField = thisField.cast(Object.class); + } resultVar = resultVar.invokeVirtual("append", StringBuilder.class, thisField); index = next + 1; diff --git a/tests/src/test/java/org/teavm/vm/RecordTest.java b/tests/src/test/java/org/teavm/vm/RecordTest.java index 06e09c98e..27993501c 100644 --- a/tests/src/test/java/org/teavm/vm/RecordTest.java +++ b/tests/src/test/java/org/teavm/vm/RecordTest.java @@ -38,7 +38,7 @@ public class RecordTest { @Test public void toStringMethod() { - String s = new A(2, "q").toString(); + String s = new B(2, "q", 3L).toString(); int index = 0; @@ -56,8 +56,19 @@ public class RecordTest { index = s.indexOf("q", index); assertTrue(index > 0); + ++index; + + index = s.indexOf("z", index); + assertTrue(index > 0); + ++index; + + index = s.indexOf("3", index); + assertTrue(index > 0); } record A(int x, String y) { } + + record B(int x, String y, Long z) { + } }