Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump intrinsics to marshallers file too #486

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,16 @@ class LazyCompanionMarshaller<T : Any>(
val classLoader: ClassLoader,
val fqn: String
) : IMarshaller<T> {
companion object {
private val possibleFields = listOf<String>("Companion", "INSTANCE")
}
private val lazy = lazy(LazyThreadSafetyMode.PUBLICATION) {
Class.forName(fqn, true, classLoader).getDeclaredField("Companion").get(null) as IMarshaller<T>
val clazz = Class.forName(fqn, true, classLoader)
val declaredFields = clazz.declaredFields

declaredFields.firstOrNull { possibleFields.contains(it.name) }?.get(null) as? IMarshaller<T> ?: run {
error("There are none of the fields ${possibleFields.joinToString()} in $clazz")
}
}

override val _type: KClass<*>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.jetbrains.rd.generator.nova

import com.jetbrains.rd.util.hash.getPlatformIndependentHash
import java.io.File


interface MarshallersCollector {
val shouldGenerateRegistrations: Boolean

fun addMarshaller(namespace: String, name: String)
fun addMarshaller(fqn: String, rdid: Long)
}

object DisabledMarshallersCollector : MarshallersCollector {
override val shouldGenerateRegistrations: Boolean
get() = true

override fun addMarshaller(namespace: String, name: String) {
override fun addMarshaller(fqn: String, rdid: Long) {
}
}

Expand All @@ -24,8 +23,8 @@ class RealMarshallersCollector(val marshallersFile: File) : MarshallersCollector
override val shouldGenerateRegistrations: Boolean
get() = false // We may want to add a separate setting here, but for now just disable it

override fun addMarshaller(namespace: String, name: String) {
marshallers.add("${name.getPlatformIndependentHash()}:${namespace}.${name}")
override fun addMarshaller(fqn: String, rdid: Long) {
marshallers.add("${rdid}:${fqn}")
}

fun close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.jetbrains.rd.generator.nova.kotlin
import com.jetbrains.rd.generator.nova.*
import com.jetbrains.rd.generator.nova.Enum
import com.jetbrains.rd.generator.nova.FlowKind.*
import com.jetbrains.rd.generator.nova.getSetting
import com.jetbrains.rd.generator.nova.kotlin.KotlinSanitizer.sanitize
import com.jetbrains.rd.generator.nova.util.capitalizeInvariant
import com.jetbrains.rd.generator.nova.util.decapitalizeInvariant
Expand Down Expand Up @@ -36,6 +37,9 @@ open class Kotlin11Generator(

object Intrinsic : SettingWithDefault<KotlinIntrinsicMarshaller, Declaration>(KotlinIntrinsicMarshaller.default)

val IDeclaration.marshallerRdid: Long get() = getSetting(Intrinsic)?.rdid ?: name.getPlatformIndependentHash()
val IDeclaration.marshallerFqn: String get() = getSetting(Intrinsic)?.marshallerObjectFqn ?: "${namespace}.${name}"

object Attributes : ISetting<Array<String>, SettingsHolder>
object PublicCtors: ISetting<Unit, Declaration>

Expand Down Expand Up @@ -514,12 +518,13 @@ open class Kotlin11Generator(
}

protected fun PrettyPrinter.companionTrait(decl: Declaration, collector: MarshallersCollector) {
val rdid = decl.marshallerRdid
if (decl.isConcrete) {
println()
collector.addMarshaller(decl.namespace, decl.name)
collector.addMarshaller(decl.marshallerFqn, rdid)
block("companion object : IMarshaller<${decl.name}>") {
+ "override val _type: KClass<${decl.name}> = ${decl.name}::class"
+ "override val id: RdId get() = RdId(${decl.name.getPlatformIndependentHash()})"
+ "override val id: RdId get() = RdId(${rdid})"
println()
readerTrait(decl)
println()
Expand All @@ -542,10 +547,10 @@ open class Kotlin11Generator(
}
else if (decl.isOpen) {
println()
collector.addMarshaller(decl.namespace, decl.name)
collector.addMarshaller(decl.marshallerFqn, rdid)
block("companion object : IMarshaller<${decl.name}>, IAbstractDeclaration<${decl.name}>") {
+"override val _type: KClass<${decl.name}> = ${decl.name}::class"
+"override val id: RdId get() = RdId(${decl.name.getPlatformIndependentHash()})"
+"override val id: RdId get() = RdId($rdid)"
println()
readerTrait(decl)
println()
Expand Down Expand Up @@ -614,15 +619,17 @@ open class Kotlin11Generator(
block("override fun registerSerializersCore(serializers: ISerializers) ") {
var first = true
types.filter { !it.isAbstract }.filterIsInstance<IType>().forEach {
if (it is Declaration && it.getSetting(Intrinsic) == null) {
if (it is Declaration) {
if (first && collector.shouldGenerateRegistrations) {
+"val classLoader = javaClass.classLoader"
first = false
}
val rdid = it.marshallerRdid
val fqn = it.marshallerFqn

collector.addMarshaller(it.namespace, it.name)
collector.addMarshaller(fqn, rdid)
if (collector.shouldGenerateRegistrations) {
println("serializers.register(LazyCompanionMarshaller(RdId(${it.name.getPlatformIndependentHash()}), classLoader, \"${it.namespace}.${it.name}\"))")
println("serializers.register(LazyCompanionMarshaller(RdId(${rdid}), classLoader, \"${fqn}\"))")
}
} else {
println("serializers.register(${it.serializerRef(decl, true)})")
Expand All @@ -631,7 +638,7 @@ open class Kotlin11Generator(

if (decl is Root) {
decl.toplevels.forEach {
collector.addMarshaller(decl.namespace, decl.name)
collector.addMarshaller(decl.marshallerFqn, it.marshallerRdid)
if (collector.shouldGenerateRegistrations) {
println(it.sanitizedName(decl) + ".register(serializers)")
}
Expand Down Expand Up @@ -1114,7 +1121,7 @@ open class Kotlin11Generator(
constantTrait(decl)
println()
+ "override val _type: KClass<${decl.name}> = ${decl.name}::class"
+ "override val id: RdId get() = RdId(${decl.name.getPlatformIndependentHash()})"
+ "override val id: RdId get() = RdId(${decl.marshallerRdid})"
println()
block("override fun read(ctx: SerializationCtx, buffer: AbstractBuffer): ${decl.substitutedName(decl)}") {
+"return marshaller.read(ctx, buffer)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.jetbrains.rd.generator.nova.kotlin
/**
* [marshallerObjectFqn] Fqn of object: inheritor of IMarshaller
*/
data class KotlinIntrinsicMarshaller(val marshallerObjectFqn:String?) {
data class KotlinIntrinsicMarshaller(val marshallerObjectFqn:String?, val rdid: Long? = null) {
companion object {
val default = KotlinIntrinsicMarshaller(null)
}
Expand Down
Loading