main.name.remal.gradle_plugins.dsl.extensions.java.lang.reflect.Method.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-plugins-kotlin-dsl Show documentation
Show all versions of gradle-plugins-kotlin-dsl Show documentation
Remal Gradle plugins: gradle-plugins-kotlin-dsl
The newest version!
package name.remal.gradle_plugins.dsl.extensions
import name.remal.isPublic
import name.remal.isStatic
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
@Suppress("DEPRECATION")
fun Method.makeAccessible() = apply {
if (isAccessible) return@apply
if (!isPublic || !declaringClass.isPublic) {
isAccessible = true
}
}
fun Method.invokeForInstance(instance: Any, vararg args: Any?): T {
if (isStatic) throw IllegalStateException("Should be invoked statically: $this")
makeAccessible()
try {
@Suppress("UNCHECKED_CAST")
return invoke(instance, *args) as T
} catch (e: InvocationTargetException) {
throw e.cause ?: e
}
}
fun Method.invokeStatically(vararg args: Any?): T {
if (!isStatic) throw IllegalStateException("Should NOT be invoked statically: $this")
makeAccessible()
try {
@Suppress("UNCHECKED_CAST")
return invoke(null, *args) as T
} catch (e: InvocationTargetException) {
throw e.cause ?: e
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy