Skip to content

Commit 2c44946

Browse files
committed
update to Kotlin 2.0.0
1 parent 8b1a826 commit 2c44946

File tree

12 files changed

+209
-13
lines changed

12 files changed

+209
-13
lines changed

Readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![jCenter](https://img.shields.io/badge/Apache-2.0-green.svg
55
)](https://github.com/Foso/KotlinReactNativeMpp/blob/master/LICENSE)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
7-
[![jCenter](https://img.shields.io/badge/Kotlin-1.9.10-green.svg
7+
[![jCenter](https://img.shields.io/badge/Kotlin-2.0.0-green.svg
88
)](https://github.com/Foso/Sheasy/blob/master/LICENSE)
99

1010

compiler-plugin/build.gradle.kts

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
22
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
33

44
plugins {
5-
kotlin("jvm") version("1.9.23")
6-
kotlin("kapt") version("1.9.23")
5+
kotlin("jvm") version("2.0.0")
6+
kotlin("kapt") version("2.0.0")
77
id("com.vanniktech.maven.publish") version("0.23.1")
88
`maven-publish`
99
signing
@@ -31,10 +31,10 @@ val autoService = "1.1.1"
3131
dependencies {
3232
compileOnly("com.google.auto.service:auto-service:$autoService")
3333
kapt("com.google.auto.service:auto-service:$autoService")
34-
compileOnly("org.jetbrains.kotlin:kotlin-compiler-embeddable:1.9.23")
35-
testImplementation("dev.zacsweers.kctfork:core:0.2.1")
34+
compileOnly("org.jetbrains.kotlin:kotlin-compiler-embeddable:2.0.0")
35+
testImplementation("dev.zacsweers.kctfork:core:0.4.1")
3636
testImplementation("junit:junit:4.13.2")
37-
testImplementation("com.google.truth:truth:1.1.5")
37+
testImplementation("com.google.truth:truth:1.4.2")
3838
testImplementation(kotlin("reflect"))
3939

4040
}

compiler-plugin/src/main/java/de/jensklingenberg/CommonComponentRegistrar.kt

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package de.jensklingenberg
22

33
import com.google.auto.service.AutoService
4+
import de.jensklingenberg.transform.ExampleIrGenerationExtension
5+
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
46
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
57
import org.jetbrains.kotlin.cli.common.config.kotlinSourceRoots
68
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
@@ -26,5 +28,10 @@ class CommonComponentRegistrar : CompilerPluginRegistrar() {
2628
"*** Hello from ***" + it.path
2729
)
2830
}
31+
32+
val logging = true
33+
IrGenerationExtension.registerExtension(
34+
ExampleIrGenerationExtension(DebugLogger(logging, messageCollector))
35+
)
2936
}
3037
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.jensklingenberg
2+
3+
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
4+
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
5+
6+
internal data class DebugLogger(val debug: Boolean, val messageCollector: MessageCollector) {
7+
fun log(message: String) {
8+
if (debug) {
9+
messageCollector.report(CompilerMessageSeverity.INFO, message)
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.IrStatement
7+
import org.jetbrains.kotlin.ir.declarations.*
8+
import org.jetbrains.kotlin.ir.expressions.IrCall
9+
import org.jetbrains.kotlin.ir.expressions.IrExpression
10+
import org.jetbrains.kotlin.ir.expressions.IrFunctionExpression
11+
12+
internal class ElementTransformer(
13+
private val pluginContext: IrPluginContext,
14+
private val debugLogger: DebugLogger
15+
) : IrElementTransformerVoidWithContext() {
16+
17+
override fun visitValueParameterNew(declaration: IrValueParameter): IrStatement {
18+
declaration.transform(CreateFuncTransformer(pluginContext,debugLogger), null)
19+
return super.visitValueParameterNew(declaration)
20+
}
21+
22+
override fun visitPropertyNew(declaration: IrProperty): IrStatement {
23+
declaration.transform(CreateFuncTransformer(pluginContext, debugLogger), null)
24+
return super.visitPropertyNew(declaration)
25+
}
26+
27+
override fun visitCall(expression: IrCall): IrExpression {
28+
expression.transform(CreateFuncTransformer(pluginContext, debugLogger), null)
29+
return super.visitCall(expression)
30+
}
31+
32+
override fun visitVariable(declaration: IrVariable): IrStatement {
33+
declaration.transform(CreateFuncTransformer(pluginContext, debugLogger), null)
34+
return super.visitVariable(declaration)
35+
}
36+
37+
38+
override fun visitFunctionExpression(expression: IrFunctionExpression): IrExpression {
39+
expression.transform(CreateFuncTransformer(pluginContext, debugLogger), null)
40+
return super.visitFunctionExpression(expression)
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.jensklingenberg.transform
2+
3+
import de.jensklingenberg.DebugLogger
4+
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
5+
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
6+
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
7+
8+
internal class ExampleIrGenerationExtension(private val debugLogger: DebugLogger) : IrGenerationExtension {
9+
override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
10+
moduleFragment.transform(ElementTransformer(pluginContext,debugLogger), null)
11+
}
12+
}

gradle-plugin/build.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
2-
kotlin("jvm") version("1.9.10")
3-
kotlin("kapt") version("1.9.10")
2+
kotlin("jvm") version("2.0.0")
3+
kotlin("kapt") version("2.0.0")
44
id("java-gradle-plugin")
55
`maven-publish`
66
}
@@ -19,7 +19,7 @@ allprojects {
1919
}
2020
}
2121
dependencies {
22-
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin-api:1.9.21")
22+
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin-api:2.0.0")
2323
}
2424

2525
gradlePlugin {

gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[versions]
2-
kotlin = "1.9.10"
2+
kotlin = "2.0.0"

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip

lib/src/commonMain/kotlin/sample/Sample.kt

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,18 @@ expect object Platform {
88
val name: String
99
}
1010

11-
fun hello(): String = "Hello from ${Platform.name}"
11+
fun hello(): String = "Hello from ${Platform.name}"
12+
13+
class _MyTestProvider : MyTest{
14+
override fun print(){
15+
println("Hello from _MyTestProvider")
16+
}
17+
}
18+
19+
interface MyTest{
20+
fun print()
21+
}
22+
23+
fun <T> create(myTestProvider: MyTest? = null): MyTest {
24+
return myTestProvider!!
25+
}

lib/src/jvmMain/kotlin/sample/SampleJvm.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ actual object Platform {
99
}
1010

1111
fun main() {
12-
12+
/**
13+
* The compiler plugin will replace this with create<MyTest>(_MyTestProvider)
14+
*/
15+
val myTest = create<MyTest>()
16+
myTest.print()
1317
}

0 commit comments

Comments
 (0)