|
| 1 | +package de.jensklingenberg.transform |
| 2 | + |
| 3 | +import de.jensklingenberg.DebugLogger |
| 4 | +import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext |
| 5 | +import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext |
| 6 | +import org.jetbrains.kotlin.ir.expressions.IrCall |
| 7 | +import org.jetbrains.kotlin.ir.expressions.IrExpression |
| 8 | +import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl |
| 9 | +import org.jetbrains.kotlin.ir.types.classFqName |
| 10 | +import org.jetbrains.kotlin.ir.types.defaultType |
| 11 | +import org.jetbrains.kotlin.ir.types.impl.originalKotlinType |
| 12 | +import org.jetbrains.kotlin.ir.util.constructors |
| 13 | +import org.jetbrains.kotlin.name.ClassId |
| 14 | +import org.jetbrains.kotlin.name.FqName |
| 15 | +import org.jetbrains.kotlin.name.Name |
| 16 | + |
| 17 | +/** |
| 18 | + * Transform create<TestApi>() to create<TestApi>(_TestApiIProvider()) |
| 19 | + */ |
| 20 | +internal class CreateFuncTransformer( |
| 21 | + private val pluginContext: IrPluginContext, |
| 22 | + private val debugLogger: DebugLogger |
| 23 | +) : IrElementTransformerVoidWithContext() { |
| 24 | + |
| 25 | + companion object { |
| 26 | + |
| 27 | + fun ERROR_IMPL_NOT_FOUND(implName: String) = |
| 28 | + "${implName} not found" |
| 29 | + |
| 30 | + private const val EXAMPLE_PACKAGE = "sample" |
| 31 | + private const val EXAMPLE_CREATE = "create" |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + override fun visitExpression(expression: IrExpression): IrExpression { |
| 36 | + |
| 37 | + //Find exampleKtorfit.create<TestApi>() |
| 38 | + (expression as? IrCall)?.let { irCall -> |
| 39 | + if (irCall.typeArgumentsCount > 0) { |
| 40 | + |
| 41 | + if (!expression.symbol.owner.symbol.toString().contains(EXAMPLE_PACKAGE)) { |
| 42 | + return expression |
| 43 | + } |
| 44 | + if (expression.symbol.owner.name.asString() != EXAMPLE_CREATE) { |
| 45 | + return expression |
| 46 | + } |
| 47 | + |
| 48 | + if (expression.getValueArgument(0) != null) { |
| 49 | + return expression |
| 50 | + } |
| 51 | + |
| 52 | + //Get T from create<T>() |
| 53 | + val argumentType = irCall.getTypeArgument(0) ?: return expression |
| 54 | + val classFqName = argumentType.classFqName |
| 55 | + |
| 56 | + //if (!argumentType.isInterface()) throw IllegalStateException(ERROR_TYPE_ARGUMENT_NOT_INTERFACE(argumentType.originalKotlinType.toString())) |
| 57 | + |
| 58 | + if (classFqName == null) { |
| 59 | + throw IllegalStateException(ERROR_IMPL_NOT_FOUND(argumentType.originalKotlinType.toString())) |
| 60 | + } |
| 61 | + |
| 62 | + val packageName = classFqName.packageName |
| 63 | + val className = classFqName.shortName().toString() |
| 64 | + val providerClassName = "_$className" + "Provider" |
| 65 | + |
| 66 | + //Find the class _TestApiProvider |
| 67 | + val implClassSymbol = pluginContext.referenceClass( |
| 68 | + ClassId( |
| 69 | + FqName(packageName), |
| 70 | + Name.identifier(providerClassName) |
| 71 | + ) |
| 72 | + ) ?: throw IllegalStateException(ERROR_IMPL_NOT_FOUND(providerClassName)) |
| 73 | + |
| 74 | + val newConstructor = implClassSymbol.constructors.first() |
| 75 | + |
| 76 | + //Create the constructor call for _ExampleApiImpl() |
| 77 | + val newCall = IrConstructorCallImpl( |
| 78 | + 0, |
| 79 | + 0, |
| 80 | + type = implClassSymbol.defaultType, |
| 81 | + symbol = newConstructor, |
| 82 | + 0, |
| 83 | + 0, |
| 84 | + 0, |
| 85 | + null |
| 86 | + ) |
| 87 | + |
| 88 | + //Set _ExampleApiImpl() as argument for create<ExampleApi>() |
| 89 | + irCall.putValueArgument(0, newCall) |
| 90 | + debugLogger.log( |
| 91 | + "Transformed " + argumentType.originalKotlinType.toString() + " to _$className" + "Impl" |
| 92 | + ) |
| 93 | + return super.visitExpression(irCall) |
| 94 | + } |
| 95 | + } |
| 96 | + return super.visitExpression(expression) |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +private val FqName?.packageName: String |
| 103 | + get() { |
| 104 | + return this.toString().substringBeforeLast(".") |
| 105 | + } |
0 commit comments