-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
209 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
compiler-plugin/src/main/java/de/jensklingenberg/DebugLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package de.jensklingenberg | ||
|
||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity | ||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector | ||
|
||
internal data class DebugLogger(val debug: Boolean, val messageCollector: MessageCollector) { | ||
fun log(message: String) { | ||
if (debug) { | ||
messageCollector.report(CompilerMessageSeverity.INFO, message) | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
compiler-plugin/src/main/java/de/jensklingenberg/transform/CreateFuncTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package de.jensklingenberg.transform | ||
|
||
import de.jensklingenberg.DebugLogger | ||
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext | ||
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext | ||
import org.jetbrains.kotlin.ir.expressions.IrCall | ||
import org.jetbrains.kotlin.ir.expressions.IrExpression | ||
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl | ||
import org.jetbrains.kotlin.ir.types.classFqName | ||
import org.jetbrains.kotlin.ir.types.defaultType | ||
import org.jetbrains.kotlin.ir.types.impl.originalKotlinType | ||
import org.jetbrains.kotlin.ir.util.constructors | ||
import org.jetbrains.kotlin.name.ClassId | ||
import org.jetbrains.kotlin.name.FqName | ||
import org.jetbrains.kotlin.name.Name | ||
|
||
/** | ||
* Transform create<TestApi>() to create<TestApi>(_TestApiIProvider()) | ||
*/ | ||
internal class CreateFuncTransformer( | ||
private val pluginContext: IrPluginContext, | ||
private val debugLogger: DebugLogger | ||
) : IrElementTransformerVoidWithContext() { | ||
|
||
companion object { | ||
|
||
fun ERROR_IMPL_NOT_FOUND(implName: String) = | ||
"${implName} not found" | ||
|
||
private const val EXAMPLE_PACKAGE = "sample" | ||
private const val EXAMPLE_CREATE = "create" | ||
|
||
} | ||
|
||
override fun visitExpression(expression: IrExpression): IrExpression { | ||
|
||
//Find exampleKtorfit.create<TestApi>() | ||
(expression as? IrCall)?.let { irCall -> | ||
if (irCall.typeArgumentsCount > 0) { | ||
|
||
if (!expression.symbol.owner.symbol.toString().contains(EXAMPLE_PACKAGE)) { | ||
return expression | ||
} | ||
if (expression.symbol.owner.name.asString() != EXAMPLE_CREATE) { | ||
return expression | ||
} | ||
|
||
if (expression.getValueArgument(0) != null) { | ||
return expression | ||
} | ||
|
||
//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())) | ||
} | ||
|
||
val packageName = classFqName.packageName | ||
val className = classFqName.shortName().toString() | ||
val providerClassName = "_$className" + "Provider" | ||
|
||
//Find the class _TestApiProvider | ||
val implClassSymbol = pluginContext.referenceClass( | ||
ClassId( | ||
FqName(packageName), | ||
Name.identifier(providerClassName) | ||
) | ||
) ?: throw IllegalStateException(ERROR_IMPL_NOT_FOUND(providerClassName)) | ||
|
||
val newConstructor = implClassSymbol.constructors.first() | ||
|
||
//Create the constructor call for _ExampleApiImpl() | ||
val newCall = IrConstructorCallImpl( | ||
0, | ||
0, | ||
type = implClassSymbol.defaultType, | ||
symbol = newConstructor, | ||
0, | ||
0, | ||
0, | ||
null | ||
) | ||
|
||
//Set _ExampleApiImpl() as argument for create<ExampleApi>() | ||
irCall.putValueArgument(0, newCall) | ||
debugLogger.log( | ||
"Transformed " + argumentType.originalKotlinType.toString() + " to _$className" + "Impl" | ||
) | ||
return super.visitExpression(irCall) | ||
} | ||
} | ||
return super.visitExpression(expression) | ||
} | ||
|
||
} | ||
|
||
|
||
private val FqName?.packageName: String | ||
get() { | ||
return this.toString().substringBeforeLast(".") | ||
} |
42 changes: 42 additions & 0 deletions
42
compiler-plugin/src/main/java/de/jensklingenberg/transform/ElementTransformer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package de.jensklingenberg.transform | ||
|
||
import de.jensklingenberg.DebugLogger | ||
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext | ||
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext | ||
import org.jetbrains.kotlin.ir.IrStatement | ||
import org.jetbrains.kotlin.ir.declarations.* | ||
import org.jetbrains.kotlin.ir.expressions.IrCall | ||
import org.jetbrains.kotlin.ir.expressions.IrExpression | ||
import org.jetbrains.kotlin.ir.expressions.IrFunctionExpression | ||
|
||
internal class ElementTransformer( | ||
private val pluginContext: IrPluginContext, | ||
private val debugLogger: DebugLogger | ||
) : IrElementTransformerVoidWithContext() { | ||
|
||
override fun visitValueParameterNew(declaration: IrValueParameter): IrStatement { | ||
declaration.transform(CreateFuncTransformer(pluginContext,debugLogger), null) | ||
return super.visitValueParameterNew(declaration) | ||
} | ||
|
||
override fun visitPropertyNew(declaration: IrProperty): IrStatement { | ||
declaration.transform(CreateFuncTransformer(pluginContext, debugLogger), null) | ||
return super.visitPropertyNew(declaration) | ||
} | ||
|
||
override fun visitCall(expression: IrCall): IrExpression { | ||
expression.transform(CreateFuncTransformer(pluginContext, debugLogger), null) | ||
return super.visitCall(expression) | ||
} | ||
|
||
override fun visitVariable(declaration: IrVariable): IrStatement { | ||
declaration.transform(CreateFuncTransformer(pluginContext, debugLogger), null) | ||
return super.visitVariable(declaration) | ||
} | ||
|
||
|
||
override fun visitFunctionExpression(expression: IrFunctionExpression): IrExpression { | ||
expression.transform(CreateFuncTransformer(pluginContext, debugLogger), null) | ||
return super.visitFunctionExpression(expression) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
compiler-plugin/src/main/java/de/jensklingenberg/transform/ExampleIrGenerationExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package de.jensklingenberg.transform | ||
|
||
import de.jensklingenberg.DebugLogger | ||
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension | ||
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext | ||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment | ||
|
||
internal class ExampleIrGenerationExtension(private val debugLogger: DebugLogger) : IrGenerationExtension { | ||
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) { | ||
moduleFragment.transform(ElementTransformer(pluginContext,debugLogger), null) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[versions] | ||
kotlin = "1.9.10" | ||
kotlin = "2.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters