Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
#58: [WIP] Update to new Meta API (#59)
Browse files Browse the repository at this point in the history
* #58: implement function injection

* #58: rename function

* Update generate-sources/new-plugin/src/main/kotlin/io/arrowkt/example/AddAnnotatedLogging.kt

Co-authored-by: Raúl Raja Martínez <[email protected]>

* #58: clean-up

* #58: change arrow-meta version

Co-authored-by: Raúl Raja Martínez <[email protected]>
  • Loading branch information
rcd27 and raulraja authored Jul 30, 2022
1 parent bd63dbb commit 5004b76
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 64 deletions.
4 changes: 2 additions & 2 deletions generate-sources/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
KOTLIN_VERSION=1.5.0
KOTLIN_VERSION=1.6.21
JVM_TARGET_VERSION=1.8
ARROW_META_VERSION=1.5.0-SNAPSHOT
ARROW_META_VERSION=1.6.0
JUNIT_VERSION=5.7.0
ASSERTJ_VERSION=3.17.2
15 changes: 15 additions & 0 deletions generate-sources/new-plugin-runtime/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id "org.jetbrains.kotlin.jvm"
}

group 'io.arrow.example'

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}

compileKotlin {
kotlinOptions {
jvmTarget = "$JVM_TARGET_VERSION"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.arrowkt.example

/**
* Use this annotation for [io.arrowkt.example.MetaPlugin] plugin to inject logic to function
*/
annotation class MetaLog
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.arrowkt.example

@Suppress("unused")
object MetaLogger {

/**
* Called by [io.arrowkt.example.MetaPlugin] during IR transformation
*/
@Suppress("unused")
fun log() {
println("MetaLogger.log() invoked")
}
}
2 changes: 2 additions & 0 deletions generate-sources/new-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ plugins {
}

dependencies {
implementation project(":new-plugin-runtime")

compileOnly "org.jetbrains.kotlin:kotlin-compiler-embeddable:$KOTLIN_VERSION"
compileOnly "io.arrow-kt:arrow-meta:$ARROW_META_VERSION"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.arrowkt.example

import arrow.meta.CliPlugin
import arrow.meta.Meta
import arrow.meta.invoke
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.ir.builders.irBlockBody
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irGetObject
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.util.getSimpleFunction
import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.ir.util.statements
import org.jetbrains.kotlin.name.FqName

val Meta.addLoggingToAnnotatedFunctions: CliPlugin
get() = "Add Logging To Annotated Functions" {
meta(
irFunction { declaration ->
return@irFunction if (declaration.hasAnnotation(FqName("io.arrowkt.example.MetaLog"))) {
declaration.body = prependLoggingToBody(pluginContext, declaration)

declaration
} else {
declaration
}
}
)
}

fun prependLoggingToBody(pluginContext: IrPluginContext, declaration: IrFunction): IrBody {
return DeclarationIrBuilder(pluginContext, declaration.symbol).irBlockBody {

val referenceClass = pluginContext.referenceClass(FqName("io.arrowkt.example.MetaLogger"))
?: throw NoClassDefFoundError("MetaLogger not found")

val interceptionCall = referenceClass.getSimpleFunction("log")
?: throw NoClassDefFoundError("MetaLogger.log() not found")

// Inject logging to function top statement(will be called first)
+irCall(interceptionCall).apply {
dispatchReceiver = irGetObject(referenceClass)
}

// Apply original statements
for (statement in declaration.body!!.statements) +statement
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class MetaPlugin : Meta {
@ExperimentalContracts
override fun intercept(ctx: CompilerContext): List<CliPlugin> =
listOf(
transformNewSources
addLoggingToAnnotatedFunctions
)
}

This file was deleted.

2 changes: 2 additions & 0 deletions generate-sources/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
include ':new-plugin'
include ':use-plugin'
include 'new-plugin-runtime'

2 changes: 2 additions & 0 deletions generate-sources/use-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ plugins {
}

dependencies {
implementation project(":new-plugin-runtime")

testImplementation "org.junit.jupiter:junit-jupiter:$JUNIT_VERSION"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.arrowkt.example

class AnnotatedFunctionInjection {

@MetaLog
fun doStuff(input: Int): Int {
return input * 2
}
}

fun main() {
// Assert logging is invoked
val testInstance = AnnotatedFunctionInjection()
val result = testInstance.doStuff(5)
println("result: $result")
}

This file was deleted.

This file was deleted.

0 comments on commit 5004b76

Please sign in to comment.