aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/me/bush/illnamethislater/Util.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/me/bush/illnamethislater/Util.kt')
-rw-r--r--src/main/kotlin/me/bush/illnamethislater/Util.kt26
1 files changed, 12 insertions, 14 deletions
diff --git a/src/main/kotlin/me/bush/illnamethislater/Util.kt b/src/main/kotlin/me/bush/illnamethislater/Util.kt
index 6c0eb9a..5285718 100644
--- a/src/main/kotlin/me/bush/illnamethislater/Util.kt
+++ b/src/main/kotlin/me/bush/illnamethislater/Util.kt
@@ -12,10 +12,10 @@ import kotlin.reflect.full.starProjectedType
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.javaGetter
-// @author bush
-// @since 3/13/2022
+// author bush
+// since 1.0.0
-/**
+/*
* Using [KClass.members] only returns public members, and
* using [KClass.declaredMembers] doesn't return inherited
* members. This returns all members, private and inherited.
@@ -23,14 +23,14 @@ import kotlin.reflect.jvm.javaGetter
internal val <T : Any> KClass<T>.allMembers
get() = declaredMembers + allSuperclasses.flatMap { it.declaredMembers }
-/**
+/*
* Checks if a [KCallable] is static on the jvm, and handles invocation accordingly.
*
* I am not aware of a better alternative that works with `object` classes.
*/
internal fun <R> KCallable<R>.handleCall(receiver: Any) = if (static) call() else call(receiver)
-/**
+/*
* Checks if the calling [KCallable] is a static java field.
*
* Because kotlin likes to be funny, properties belonging to
@@ -45,13 +45,11 @@ internal val KCallable<*>.static
get() = if (this !is KProperty<*> || javaGetter != null) false
else javaField?.let { Modifier.isStatic(it.modifiers) } ?: false
-@Suppress("UNCHECKED_CAST") // This cannot fail
-internal inline fun <reified T : Any> List<KCallable<*>>.filterReturnType() =
- filter { it.returnType == T::class.starProjectedType } as List<KCallable<T>>
-
-/**
- * A simple class returned as a "key" for listeners that are not
- * members of a class, just to make its intentions clearer. This
- * can be used in [EventBus.unsubscribe] to remove the listener.
+/*
+ * Finds all listeners in a class. (properties and methods)
*/
-internal class ListenerKey \ No newline at end of file
+@Suppress("UNCHECKED_CAST") // This cannot fail
+internal inline val KClass<*>.listeners
+ get() = allMembers.asSequence().filter {
+ it.returnType == Listener::class.starProjectedType
+ } as Sequence<KCallable<Listener>>