aboutsummaryrefslogtreecommitdiff
path: root/integration/src
diff options
context:
space:
mode:
authorSimon Ogorodnik <Simon.Ogorodnik@jetbrains.com>2017-06-16 22:38:01 +0300
committerSimon Ogorodnik <Simon.Ogorodnik@jetbrains.com>2017-06-20 16:15:48 +0300
commit558c6b67fafb73c97db9ce1eeadfd4fcbb911134 (patch)
tree0c80684834dac2d178700055f11acb47aca51ffb /integration/src
parent7c55d94eeb42f851e5ee0664bb38a873b6c06ee1 (diff)
downloaddokka-558c6b67fafb73c97db9ce1eeadfd4fcbb911134.tar.gz
dokka-558c6b67fafb73c97db9ce1eeadfd4fcbb911134.tar.bz2
dokka-558c6b67fafb73c97db9ce1eeadfd4fcbb911134.zip
Configure dokka based on Kotlin compile tasks
Diffstat (limited to 'integration/src')
-rw-r--r--integration/src/main/kotlin/org/jetbrains/dokka/ReflectDsl.kt70
1 files changed, 70 insertions, 0 deletions
diff --git a/integration/src/main/kotlin/org/jetbrains/dokka/ReflectDsl.kt b/integration/src/main/kotlin/org/jetbrains/dokka/ReflectDsl.kt
new file mode 100644
index 00000000..3c9bf156
--- /dev/null
+++ b/integration/src/main/kotlin/org/jetbrains/dokka/ReflectDsl.kt
@@ -0,0 +1,70 @@
+package org.jetbrains.dokka
+
+import kotlin.reflect.*
+import kotlin.reflect.jvm.isAccessible
+
+object ReflectDsl {
+
+ class CallOrPropAccess(private val receiver: Any?,
+ private val clz: KClass<*>,
+ private val selector: String) {
+
+ @Suppress("UNCHECKED_CAST")
+ operator fun <T : Any?> invoke(vararg a: Any?): T {
+ return func!!.call(receiver, *a) as T
+ }
+
+ operator fun get(s: String): CallOrPropAccess {
+ return v<Any?>()!![s]
+ }
+
+ val func: KFunction<*>? by lazy { clz.memberFunctions.find { it.name == selector } }
+ val prop: KProperty<*>? by lazy { clz.memberProperties.find { it.name == selector } }
+
+ fun takeIfIsFunc(): CallOrPropAccess? = if (func != null) this else null
+
+ fun takeIfIsProp(): CallOrPropAccess? = if (prop != null) this else null
+
+ @Suppress("UNCHECKED_CAST")
+ fun <T : Any?> v(): T {
+ val prop = prop!!
+ return try {
+ prop.getter.apply { isAccessible = true }.call(receiver) as T
+ } catch (e: KotlinNullPointerException) {
+ // Hack around kotlin-reflect bug KT-18480
+ val jclass = clz.java
+ val customGetterName = prop.getter.name
+ val getterName = if (customGetterName.startsWith("<")) "get" + prop.name.capitalize() else customGetterName
+ val getter = jclass.getDeclaredMethod(getterName)
+ getter.isAccessible = true
+
+ getter.invoke(receiver) as T
+
+ }
+ }
+
+ @Suppress("UNCHECKED_CAST")
+ fun v(x: Any?) {
+ (prop as KMutableProperty).setter.apply { isAccessible = true }.call(receiver, x)
+ }
+
+
+ }
+
+ operator fun Any.get(s: String): CallOrPropAccess {
+ val clz = this.javaClass.kotlin
+ return CallOrPropAccess(this, clz, s)
+ }
+
+ operator fun Any.get(s: String, clz: Class<*>): CallOrPropAccess {
+ val kclz = clz.kotlin
+ return CallOrPropAccess(this, kclz, s)
+ }
+
+ operator fun Any.get(s: String, clz: KClass<*>): CallOrPropAccess {
+ return CallOrPropAccess(this, clz, s)
+ }
+
+ inline infix fun Any.isInstance(clz: Class<*>?): Boolean = clz != null && clz.isAssignableFrom(this.javaClass)
+ inline infix fun Any.isNotInstance(clz: Class<*>?): Boolean = !(this isInstance clz)
+} \ No newline at end of file