diff --git a/routing-annotations/routing-annotated/src/main/kotlin/io/javalin/community/routing/annotations/ReflectiveEndpointLoader.kt b/routing-annotations/routing-annotated/src/main/kotlin/io/javalin/community/routing/annotations/ReflectiveEndpointLoader.kt index e4d0e9a..074d739 100644 --- a/routing-annotations/routing-annotated/src/main/kotlin/io/javalin/community/routing/annotations/ReflectiveEndpointLoader.kt +++ b/routing-annotations/routing-annotated/src/main/kotlin/io/javalin/community/routing/annotations/ReflectiveEndpointLoader.kt @@ -23,6 +23,16 @@ internal class ReflectiveEndpointLoader( private val repeatedPathSeparatorRegex = Regex("/+") + private fun getAllDeclaredMethods(clazz: Class<*>): Collection { + val methods = mutableListOf() + var currentClass: Class<*>? = clazz + while (currentClass?.name != "java.lang.Object") { + methods.addAll(currentClass?.declaredMethods ?: emptyArray()) + currentClass = currentClass?.superclass + } + return methods + } + fun loadRoutesFromEndpoint(endpoint: Any): List { val endpointClass = endpoint::class.java @@ -32,7 +42,7 @@ internal class ReflectiveEndpointLoader( val endpointRoutes = mutableListOf() - endpointClass.declaredMethods.forEach { method -> + getAllDeclaredMethods(endpointClass).forEach { method -> val (httpMethod, path, async) = when { method.isAnnotationPresent() -> method.getAnnotation()!!.let { Triple(Route.BEFORE, it.value, it.async) } method.isAnnotationPresent() -> method.getAnnotation()!!.let { Triple(BEFORE_MATCHED, it.value, it.async) } @@ -86,7 +96,7 @@ internal class ReflectiveEndpointLoader( val endpointClass = endpoint::class.java val dslExceptions = mutableListOf() - endpointClass.declaredMethods.forEach { method -> + getAllDeclaredMethods(endpointClass).forEach { method -> val exceptionHandlerAnnotation = method.getAnnotation() ?: return@forEach require(method.trySetAccessible()) { @@ -128,7 +138,7 @@ internal class ReflectiveEndpointLoader( val endpointClass = endpoint::class.java val dslEvents = mutableMapOf() - endpointClass.declaredMethods.forEach { method -> + getAllDeclaredMethods(endpointClass).forEach { method -> val lifecycleEventHandler = method.getAnnotation() ?: return@forEach require(method.trySetAccessible()) {