-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support lambdas that access variables outside the lambda
- Loading branch information
Showing
3 changed files
with
24 additions
and
9 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
15 changes: 8 additions & 7 deletions
15
build-tools-gradle/src/main/kotlin/org/modelix/gradle/mpsbuild/InvokeLambda.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 |
---|---|---|
@@ -1,16 +1,17 @@ | ||
package org.modelix.gradle.mpsbuild | ||
|
||
import java.io.FileInputStream | ||
import java.io.ObjectInputStream | ||
|
||
object InvokeLambda { | ||
const val PROPERTY_KEY = "runMPS.lambda.name" | ||
const val PROPERTY_KEY = "runMPS.lambda.file" | ||
|
||
@JvmStatic | ||
fun invoke() { | ||
// System.getProperty(PROPERTY_KEY) would get instrumented which introduces a Gradle dependency | ||
val lambdaName = System::class.java.getMethod("getProperty", String::class.java) | ||
.invoke(null, PROPERTY_KEY) as String | ||
val cls = Class.forName(lambdaName) | ||
val lambdaInstance = cls.getField("INSTANCE").also { it.trySetAccessible() } | ||
.get(null) as () -> Unit | ||
val lambdaFileName = System.getProperty(PROPERTY_KEY) | ||
val lambdaInstance = ObjectInputStream(FileInputStream(lambdaFileName)).use { | ||
it.readObject() as () -> Unit | ||
} | ||
lambdaInstance() | ||
} | ||
} |
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