-
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.
- Loading branch information
Showing
11 changed files
with
174 additions
and
72 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
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
47 changes: 47 additions & 0 deletions
47
core/src/main/kotlin/cmu/pasta/fray/core/command/Executor.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,47 @@ | ||
package cmu.pasta.fray.core.command | ||
|
||
import java.net.URI | ||
import java.net.URLClassLoader | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
sealed interface Executor { | ||
fun execute() | ||
} | ||
|
||
@Serializable | ||
@SerialName("executor") | ||
data class MethodExecutor( | ||
val clazz: String, | ||
val method: String, | ||
val args: List<String>, | ||
val classpaths: List<String> | ||
) : Executor { | ||
override fun execute() { | ||
val classLoader = | ||
URLClassLoader( | ||
classpaths.map { it -> URI("file://$it").toURL() }.toTypedArray(), | ||
Thread.currentThread().contextClassLoader) | ||
Thread.currentThread().contextClassLoader = classLoader | ||
val clazz = Class.forName(clazz, true, classLoader) | ||
if (args.isEmpty() && method != "main") { | ||
val m = clazz.getMethod(method) | ||
if (m.modifiers and java.lang.reflect.Modifier.STATIC == 0) { | ||
val obj = clazz.getConstructor().newInstance() | ||
m.invoke(obj) | ||
} else { | ||
m.invoke(null) | ||
} | ||
} else { | ||
val m = clazz.getMethod(method, Array<String>::class.java) | ||
m.invoke(null, args.toTypedArray()) | ||
} | ||
} | ||
} | ||
|
||
class LambdaExecutor(val lambda: () -> Unit) : Executor { | ||
override fun execute() { | ||
lambda() | ||
} | ||
} |
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
Oops, something went wrong.