Fix issue in record toString method generator

This commit is contained in:
Alexey Andreev 2022-08-06 14:07:53 +03:00
parent b6837340e5
commit 3571917a2f
2 changed files with 15 additions and 1 deletions

View File

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

View File

@ -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) {
}
}