diff --git a/build.gradle.kts b/build.gradle.kts index cbfdc3a4..2570a4f9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,6 +14,7 @@ plugins { alias(libs.plugins.conventions.kover) alias(libs.plugins.conventions.gradle.doctor) alias(libs.plugins.atomicfu) + id("build-util") } configureProjectReport() diff --git a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirVersionSpecificApi.kt b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirVersionSpecificApi.kt index a892e886..9ce9d212 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirVersionSpecificApi.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/FirVersionSpecificApi.kt @@ -5,6 +5,8 @@ package kotlinx.rpc.codegen import org.jetbrains.kotlin.KtSourceElement +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef @@ -16,10 +18,20 @@ interface FirVersionSpecificApi { delegatedTypeRef: FirTypeRef? = null, ): FirResolvedTypeRef + fun ConeKotlinType.toClassSymbolVS( + session: FirSession, + ): FirClassSymbol<*>? + var FirResolvedTypeRefBuilder.coneTypeVS: ConeKotlinType } -fun vsApi(body: FirVersionSpecificApi.() -> T) : T { - val klass = Class.forName("kotlinx.rpc.codegen.FirVersionSpecificApiImpl") - return (klass.kotlin.objectInstance as FirVersionSpecificApi).body() +val vsApiClass by lazy { + runCatching { + Class.forName("kotlinx.rpc.codegen.FirVersionSpecificApiImpl") + }.getOrNull() +} + +inline fun vsApi(body: FirVersionSpecificApi.() -> T): T { + val kClass = vsApiClass?.kotlin ?: error("FirVersionSpecificApi is not present") + return (kClass.objectInstance as FirVersionSpecificApi).body() } diff --git a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/FirCheckedAnnotationCheckers.kt b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/FirCheckedAnnotationCheckers.kt index 9e04ddcd..cc798f31 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/FirCheckedAnnotationCheckers.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/core/kotlinx/rpc/codegen/checkers/FirCheckedAnnotationCheckers.kt @@ -9,6 +9,7 @@ import kotlinx.rpc.codegen.FirRpcPredicates import kotlinx.rpc.codegen.checkers.FirCheckedAnnotationHelper.checkTypeArguments import kotlinx.rpc.codegen.checkers.diagnostics.FirRpcDiagnostics import kotlinx.rpc.codegen.common.RpcClassId +import kotlinx.rpc.codegen.vsApi import org.jetbrains.kotlin.KtSourceElement import org.jetbrains.kotlin.diagnostics.DiagnosticReporter import org.jetbrains.kotlin.diagnostics.reportOn @@ -76,7 +77,7 @@ class FirCheckedAnnotationTypeParameterChecker( ctx = ctx, origin = bound, originMapper = { it.coneType }, - symbolProvider = { it.toClassSymbol(context.session) }, + symbolProvider = { vsApi { it.toClassSymbolVS(context.session) } }, typeParameterSymbolsProvider = { it.typeParameterSymbols }, typeArgumentsProvider = { it.typeArguments.toList() }, typeArgumentsMapper = { it }, @@ -102,7 +103,7 @@ class FirCheckedAnnotationFirClassChecker( ctx = ctx, origin = superType, originMapper = { it.coneType }, - symbolProvider = { it.toClassSymbol(context.session) }, + symbolProvider = { vsApi { it.toClassSymbolVS(context.session) } }, typeParameterSymbolsProvider = { it.typeParameterSymbols }, typeArgumentsProvider = { it.typeArguments.toList() }, typeArgumentsMapper = { it }, @@ -127,7 +128,7 @@ class FirCheckedAnnotationFirFunctionChecker( ctx = ctx, origin = valueParameter.returnTypeRef, originMapper = { it.coneType }, - symbolProvider = { it.toClassSymbol(context.session) }, + symbolProvider = { vsApi { it.toClassSymbolVS(context.session) } }, typeParameterSymbolsProvider = { it.typeParameterSymbols }, typeArgumentsProvider = { it.typeArguments.toList() }, typeArgumentsMapper = { it }, @@ -172,7 +173,7 @@ object FirCheckedAnnotationHelper { parametersWithAnnotations.forEach { (i, annotations) -> val typeArgument = typeArguments[i] val type = typeArgumentsMapper(typeArgument)?.type - val classSymbol = type?.toClassSymbol(context.session) + val classSymbol = vsApi { type?.toClassSymbolVS(context.session) } val symbol = when { classSymbol != null -> { @@ -247,7 +248,7 @@ object FirCheckedAnnotationHelper { }?.type }, originTypeRefSource = extractedOriginSources.getOrNull(i) ?: nextOriginSource ?: originTypeRefSource, - symbolProvider = { it.toClassSymbol(context.session) }, + symbolProvider = { vsApi { it.toClassSymbolVS(context.session) } }, typeParameterSymbolsProvider = { it.typeParameterSymbols }, typeArgumentsProvider = { it.typeArguments.toList() }, typeArgumentsMapper = { it }, @@ -283,7 +284,7 @@ object FirCheckedAnnotationHelper { visited: Set> = emptySet(), ): List> { return symbol.annotations.mapNotNull { - it.resolvedType.toClassSymbol(session) + vsApi { it.resolvedType.toClassSymbolVS(session) } }.filter { annotation -> when { annotation in visited -> false @@ -328,7 +329,7 @@ object FirCheckedAnnotationHelper { symbol in visited -> false symbol.hasAnnotation(annotationId, session) -> true else -> symbol.annotations.any { annotation -> - annotation.resolvedType.toClassSymbol(session)?.let { + vsApi { annotation.resolvedType.toClassSymbolVS(session) }?.let { hasCheckedAnnotation(session, it, annotationId, visited + symbol) } == true } diff --git a/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt b/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt index c0f6af8e..9b619df7 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/latest/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt @@ -5,11 +5,14 @@ package kotlinx.rpc.codegen import org.jetbrains.kotlin.KtSourceElement +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.toFirResolvedTypeRef import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.builder.FirResolvedTypeRefBuilder +import org.jetbrains.kotlin.fir.types.toClassSymbol @Suppress("unused") object FirVersionSpecificApiImpl : FirVersionSpecificApi { @@ -20,6 +23,10 @@ object FirVersionSpecificApiImpl : FirVersionSpecificApi { return toFirResolvedTypeRef(source, delegatedTypeRef) } + override fun ConeKotlinType.toClassSymbolVS(session: FirSession): FirClassSymbol<*>? { + return toClassSymbol(session) + } + override var FirResolvedTypeRefBuilder.coneTypeVS: ConeKotlinType get() = coneType set(value) { diff --git a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt index 1603e8f8..c45c4219 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_10/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt @@ -5,11 +5,15 @@ package kotlinx.rpc.codegen import org.jetbrains.kotlin.KtSourceElement +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol +import org.jetbrains.kotlin.fir.types.ConeClassLikeType import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.toFirResolvedTypeRef import org.jetbrains.kotlin.fir.types.builder.FirResolvedTypeRefBuilder +import org.jetbrains.kotlin.fir.types.toClassSymbol @Suppress("unused") object FirVersionSpecificApiImpl : FirVersionSpecificApi { @@ -20,6 +24,10 @@ object FirVersionSpecificApiImpl : FirVersionSpecificApi { return toFirResolvedTypeRef(source, delegatedTypeRef) } + override fun ConeKotlinType.toClassSymbolVS(session: FirSession): FirClassSymbol<*>? { + return (this as? ConeClassLikeType)?.toClassSymbol(session) + } + override var FirResolvedTypeRefBuilder.coneTypeVS: ConeKotlinType get() = type set(value) { diff --git a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt index 989bd77e..b92a1be5 100644 --- a/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt +++ b/compiler-plugin/compiler-plugin-k2/src/main/pre_2_0_21/kotlinx/rpc/codegen/FirVersionSpecificApiImpl.kt @@ -5,11 +5,14 @@ package kotlinx.rpc.codegen import org.jetbrains.kotlin.KtSourceElement +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.toFirResolvedTypeRef import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.fir.types.builder.FirResolvedTypeRefBuilder +import org.jetbrains.kotlin.fir.types.toClassSymbol @Suppress("unused") object FirVersionSpecificApiImpl : FirVersionSpecificApi { @@ -20,6 +23,10 @@ object FirVersionSpecificApiImpl : FirVersionSpecificApi { return toFirResolvedTypeRef(source, delegatedTypeRef) } + override fun ConeKotlinType.toClassSymbolVS(session: FirSession): FirClassSymbol<*>? { + return toClassSymbol(session) + } + override var FirResolvedTypeRefBuilder.coneTypeVS: ConeKotlinType get() = type set(value) {