This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
13 changed files
with
109 additions
and
64 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
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 |
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,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" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
generate-sources/new-plugin-runtime/src/main/java/io/arrowkt/example/MetaLog.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,6 @@ | ||
package io.arrowkt.example | ||
|
||
/** | ||
* Use this annotation for [io.arrowkt.example.MetaPlugin] plugin to inject logic to function | ||
*/ | ||
annotation class MetaLog |
13 changes: 13 additions & 0 deletions
13
generate-sources/new-plugin-runtime/src/main/java/io/arrowkt/example/MetaLogger.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,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") | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
generate-sources/new-plugin/src/main/kotlin/io/arrowkt/example/AddAnnotatedLogging.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,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 | ||
} | ||
} |
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
41 changes: 0 additions & 41 deletions
41
generate-sources/new-plugin/src/main/kotlin/io/arrowkt/example/TransformNewSources.kt
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
include ':new-plugin' | ||
include ':use-plugin' | ||
include 'new-plugin-runtime' | ||
|
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
16 changes: 16 additions & 0 deletions
16
generate-sources/use-plugin/src/main/kotlin/io/arrowkt/example/AnnotatedFunctionInjection.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,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") | ||
} |
3 changes: 0 additions & 3 deletions
3
generate-sources/use-plugin/src/main/kotlin/io/arrowkt/example/NewMultipleSource.kt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
generate-sources/use-plugin/src/test/kotlin/io/arrowkt/example/NewMultipleSourceTests.kt
This file was deleted.
Oops, something went wrong.