Skip to content

Commit 50cb400

Browse files
author
Andrei Shikov
committed
Move function name to hardcoded value
1 parent 0078ac7 commit 50cb400

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

kotlin-plugin/src/main/kotlin/me/shika/ComponentRegistrar.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import org.jetbrains.kotlin.com.intellij.mock.MockProject
99
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
1010
import org.jetbrains.kotlin.config.CompilerConfiguration
1111

12-
class ObjectSerializationComponentRegistrar @JvmOverloads constructor(
13-
private val enabled: Boolean = false
14-
): ComponentRegistrar {
12+
class ObjectSerializationComponentRegistrar(): ComponentRegistrar {
1513
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
16-
if (configuration[KEY_ENABLED] == false && !enabled) {
14+
if (configuration[KEY_ENABLED] == false) {
1715
return
1816
}
1917

kotlin-plugin/src/main/kotlin/me/shika/generation/ObjectSerializationJvmGeneration.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,22 @@ class ObjectSerializationJvmGeneration : ExpressionCodegenExtension {
1414
if (codegen.descriptor.needSerializableFix()) {
1515
val selfType = codegen.typeMapper.mapType(codegen.descriptor)
1616

17-
codegen.addFunction(SERIALIZABLE_READ.identifier, "()Ljava/lang/Object;") {
17+
codegen.addReadResolveFunction {
1818
getstatic(codegen.className, "INSTANCE", selfType.descriptor)
1919
areturn(selfType)
2020
}
2121
}
2222

2323
}
2424

25-
private fun ImplementationBodyCodegen.addFunction(
26-
name: String,
27-
asmDescriptor: String,
25+
private fun ImplementationBodyCodegen.addReadResolveFunction(
2826
block: InstructionAdapter.() -> Unit
2927
) {
3028
val visitor = v.newMethod(
3129
NO_ORIGIN,
3230
ACC_PUBLIC or ACC_SYNTHETIC,
33-
name,
34-
asmDescriptor,
31+
SERIALIZABLE_READ.identifier,
32+
"()Ljava/lang/Object;",
3533
null,
3634
EMPTY_STRING_ARRAY
3735
)

kotlin-plugin/src/main/kotlin/me/shika/generation/utils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.shika.generation
22

33
import org.jetbrains.kotlin.descriptors.ClassDescriptor
4+
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
45
import org.jetbrains.kotlin.name.FqName
56
import org.jetbrains.kotlin.name.Name
67
import org.jetbrains.kotlin.platform.has
@@ -19,7 +20,8 @@ fun ClassDescriptor.needSerializableFix() =
1920
&& !hasReadMethod()
2021

2122
fun ClassDescriptor.hasReadMethod() =
22-
unsubstitutedMemberScope.getFunctionNames().contains(SERIALIZABLE_READ)
23+
unsubstitutedMemberScope.getContributedFunctions(SERIALIZABLE_READ, NoLookupLocation.FROM_BACKEND)
24+
.any { it.name == SERIALIZABLE_READ && it.valueParameters.isEmpty() }
2325

2426
fun ClassDescriptor.isSerializable(): Boolean =
2527
getSuperInterfaces().any { it.fqNameSafe == SERIALIZABLE_FQ_NAME || it.isSerializable() }

kotlin-plugin/src/test/kotlin/me/shika/ObjectSerializationFixTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ObjectSerializationFixTest(enableIr: Boolean) {
1818
}
1919

2020
private val compiler = KotlinCompilation().apply {
21-
compilerPlugins = listOf(ObjectSerializationComponentRegistrar(enabled = true))
21+
compilerPlugins = listOf(ObjectSerializationComponentRegistrar())
2222
useIR = enableIr
2323
}
2424

0 commit comments

Comments
 (0)