io.p8e.util.MethodUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of p8e-sdk Show documentation
Show all versions of p8e-sdk Show documentation
A collection of services and libraries that iteract and run Provenance Java based contracts.
package io.p8e.util
import com.google.protobuf.Message
import io.p8e.proxy.Function
import io.p8e.proxy.Function1
import io.p8e.proxy.Function2
import io.p8e.proxy.Function3
import io.p8e.proxy.Function4
import io.p8e.proxy.Function5
import io.p8e.proxy.Function6
import io.p8e.proxy.Function7
import io.p8e.spec.P8eContract
import javassist.util.proxy.ProxyFactory
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentHashMap
object MethodUtil {
private val cache = ConcurrentHashMap, ProxyFactory>()
fun getMethodFact(
clazz: Class,
function: Function
): String {
val factFuture = CompletableFuture()
val proxy = getProxy(clazz, factFuture)
try {
function.apply(proxy)
} catch (ignored: Throwable) { }
return factFuture.get()
}
fun getMethodFact(
clazz: Class,
function: Function1
): String {
val factFuture = CompletableFuture()
val proxy = getProxy(clazz, factFuture)
try {
function.apply(proxy, null)
} catch (ignored: Throwable) { }
return factFuture.get()
}
fun getMethodFact(
clazz: Class,
function: Function2
): String {
val factFuture = CompletableFuture()
val proxy = getProxy(clazz, factFuture)
try {
function.apply(proxy, null, null)
} catch (ignored: Throwable) { }
return factFuture.get()
}
fun getMethodFact(
clazz: Class,
function: Function3
): String {
val factFuture = CompletableFuture()
val proxy = getProxy(clazz, factFuture)
try {
function.apply(proxy, null, null, null)
} catch (ignored: Throwable) { }
return factFuture.get()
}
fun getMethodFact(
clazz: Class,
function: Function4
): String {
val factFuture = CompletableFuture()
val proxy = getProxy(clazz, factFuture)
try {
function.apply(proxy, null, null, null, null)
} catch (ignored: Throwable) { }
return factFuture.get()
}
fun getMethodFact(
clazz: Class,
function: Function5
): String {
val factFuture = CompletableFuture()
val proxy = getProxy(clazz, factFuture)
try {
function.apply(proxy, null, null, null, null, null)
} catch (ignored: Throwable) { }
return factFuture.get()
}
fun getMethodFact(
clazz: Class,
function: Function6
): String {
val factFuture = CompletableFuture()
val proxy = getProxy(clazz, factFuture)
try {
function.apply(proxy, null, null, null, null, null, null)
} catch (ignored: Throwable) { }
return factFuture.get()
}
fun getMethodFact(
clazz: Class,
function: Function7
): String {
val factFuture = CompletableFuture()
val proxy = getProxy(clazz, factFuture)
try {
function.apply(proxy, null, null, null, null, null, null, null)
} catch (ignored: Throwable) { }
return factFuture.get()
}
private fun getProxy(
clazz: Class,
factFuture: CompletableFuture
): T {
return cache.computeIfAbsent(clazz) {
ProxyFactory()
.apply {
superclass = clazz
interfaces = arrayOf(P8eContract::class.java)
}
}.let { proxyFactory ->
proxyFactory.create(
arrayOf(),
arrayOf(),
FactMethodHandler(factFuture)
).let(clazz::cast)
}
}
}