Skip to content

Commit

Permalink
Improve compiler error handling (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso authored May 27, 2024
1 parent dbe7a60 commit 8a78128
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ import org.jetbrains.kotlin.name.Name
/**
* Transform exampleKtorfit.create<TestApi>() to exampleKtorfit.create<TestApi>(_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"
Expand All @@ -56,22 +58,24 @@ class CreateFuncTransformer(
//Get T from create<T>()
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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 8a78128

Please sign in to comment.