[KxSerialization] Fix "IllegalAccessError: Update to static final field"

Fixes #KT-57647

For value classes, if you add code to companion anonymous init block in IR, it will be executed in the instance constructor. This causes an error when initializing static fields, because writing to them can only occur from the <clinit> method.

The solution is to transfer the static field initialization code from an anonymous init block to the IR initializer of this field

Merge-request: KT-MR-9633
Merged-by: Sergey Shanshin <Sergey.Shanshin@jetbrains.com>
This commit is contained in:
Sergey.Shanshin
2023-04-21 12:37:32 +00:00
committed by Space Team
parent 803a11003f
commit d8d643b7d7
11 changed files with 126 additions and 19 deletions
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlinx.serialization.runners;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlinx.serialization.TestGeneratorKt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("plugins/kotlinx-serialization/testData/jdk11BoxIr")
@TestDataPath("$PROJECT_ROOT")
public class SerializationJdk11IrBoxTestGenerated extends AbstractSerializationJdk11IrBoxTest {
@Test
public void testAllFilesPresentInJdk11BoxIr() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("plugins/kotlinx-serialization/testData/jdk11BoxIr"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("kt57647.kt")
public void testKt57647() throws Exception {
runTest("plugins/kotlinx-serialization/testData/jdk11BoxIr/kt57647.kt");
}
}