From 8a781281745a4167205ecade9278efed1bd17ce3 Mon Sep 17 00:00:00 2001 From: Jens Klingenberg Date: Mon, 27 May 2024 19:34:46 +0200 Subject: [PATCH] Improve compiler error handling (#565) --- .../ktorfit/CreateFuncTransformer.kt | 22 +++++++++++-------- .../de/jensklingenberg/ktorfit/DebugLogger.kt | 2 +- .../ktorfit/ElementTransformer.kt | 2 +- .../ktorfit/KtorfitIrGenerationExtension.kt | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/CreateFuncTransformer.kt b/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/CreateFuncTransformer.kt index 2a117ee0e..4457bab04 100644 --- a/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/CreateFuncTransformer.kt +++ b/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/CreateFuncTransformer.kt @@ -18,18 +18,20 @@ import org.jetbrains.kotlin.name.Name /** * Transform exampleKtorfit.create() to exampleKtorfit.create(_TestApiImpl()) */ -class CreateFuncTransformer( +internal class CreateFuncTransformer( private val pluginContext: IrPluginContext, private val debugLogger: DebugLogger -) : - IrElementTransformerVoidWithContext() { +) : IrElementTransformerVoidWithContext() { companion object { fun ERROR_TYPE_ARGUMENT_NOT_INTERFACE(implName: String)= "create<${implName}> argument is not supported. Type argument needs to be an interface" - fun ERROR_IMPL_NOT_FOUND(implName: String) = - "_${implName}Impl() not found, did you apply the Ksp Ktorfit plugin?" + fun ERROR_IMPL_NOT_FOUND(implName: String, className: String) = + "${implName} not found, did you apply the Ksp Ktorfit plugin? Use .create${className}() instead" + + fun ERROR_CLASS_NOT_FOUND(implName: String) = + "class ${implName} not found, did you apply the Ksp Ktorfit plugin?" private const val KTORFIT_PACKAGE = "de.jensklingenberg.ktorfit.Ktorfit" private const val KTORFIT_CREATE = "create" @@ -56,22 +58,24 @@ class CreateFuncTransformer( //Get T from create() val argumentType = irCall.getTypeArgument(0) ?: return expression val classFqName = argumentType.classFqName + if (!argumentType.isInterface()) throw IllegalStateException(ERROR_TYPE_ARGUMENT_NOT_INTERFACE(argumentType.originalKotlinType.toString())) if (classFqName == null) { - throw IllegalStateException(ERROR_IMPL_NOT_FOUND(argumentType.originalKotlinType.toString())) + throw IllegalStateException(ERROR_CLASS_NOT_FOUND(argumentType.originalKotlinType.toString())) } val packageName = classFqName.packageName val className = classFqName.shortName().toString() + val providerClassName = "_$className" + "Provider" - //Find the class _TestApiImpl + //Find the class _TestApiProvider val implClassSymbol = pluginContext.referenceClass( ClassId( FqName(packageName), - Name.identifier("_$className" + "Provider") + Name.identifier(providerClassName) ) - ) ?: throw IllegalStateException(ERROR_IMPL_NOT_FOUND(argumentType.originalKotlinType.toString())) + ) ?: throw IllegalStateException(ERROR_IMPL_NOT_FOUND(providerClassName, className)) val newConstructor = implClassSymbol.constructors.first() diff --git a/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/DebugLogger.kt b/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/DebugLogger.kt index 47da9e8f7..b77f89a8a 100644 --- a/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/DebugLogger.kt +++ b/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/DebugLogger.kt @@ -3,7 +3,7 @@ package de.jensklingenberg.ktorfit import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.MessageCollector -data class DebugLogger(val debug: Boolean, val messageCollector: MessageCollector) { +internal data class DebugLogger(val debug: Boolean, val messageCollector: MessageCollector) { fun log(message: String) { if (debug) { messageCollector.report(CompilerMessageSeverity.INFO, message) diff --git a/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/ElementTransformer.kt b/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/ElementTransformer.kt index feb70214d..0ba80bb03 100644 --- a/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/ElementTransformer.kt +++ b/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/ElementTransformer.kt @@ -8,7 +8,7 @@ import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrFunctionExpression -class ElementTransformer( +internal class ElementTransformer( private val pluginContext: IrPluginContext, private val debugLogger: DebugLogger ) : IrElementTransformerVoidWithContext() { diff --git a/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/KtorfitIrGenerationExtension.kt b/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/KtorfitIrGenerationExtension.kt index 0586f6ae7..15d08b0f2 100644 --- a/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/KtorfitIrGenerationExtension.kt +++ b/ktorfit-compiler-plugin/src/main/java/de/jensklingenberg/ktorfit/KtorfitIrGenerationExtension.kt @@ -4,7 +4,7 @@ import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext import org.jetbrains.kotlin.ir.declarations.IrModuleFragment -class KtorfitIrGenerationExtension(private val debugLogger: DebugLogger) : IrGenerationExtension { +internal class KtorfitIrGenerationExtension(private val debugLogger: DebugLogger) : IrGenerationExtension { override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) { moduleFragment.transform(ElementTransformer(pluginContext,debugLogger), null) }