Skip to content

Commit 8cddf86

Browse files
authored
GH-48 Support inheritance (Resolve #47)
1 parent f3b94a5 commit 8cddf86

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

routing-annotations/routing-annotated/src/main/kotlin/io/javalin/community/routing/annotations/ReflectiveEndpointLoader.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ internal class ReflectiveEndpointLoader(
2323

2424
private val repeatedPathSeparatorRegex = Regex("/+")
2525

26+
private fun getAllDeclaredMethods(clazz: Class<*>): Collection<Method> {
27+
val methods = mutableListOf<Method>()
28+
var currentClass: Class<*>? = clazz
29+
while (currentClass?.name != "java.lang.Object") {
30+
methods.addAll(currentClass?.declaredMethods ?: emptyArray())
31+
currentClass = currentClass?.superclass
32+
}
33+
return methods
34+
}
35+
2636
fun loadRoutesFromEndpoint(endpoint: Any): List<AnnotatedRoute> {
2737
val endpointClass = endpoint::class.java
2838

@@ -32,7 +42,7 @@ internal class ReflectiveEndpointLoader(
3242

3343
val endpointRoutes = mutableListOf<AnnotatedRoute>()
3444

35-
endpointClass.declaredMethods.forEach { method ->
45+
getAllDeclaredMethods(endpointClass).forEach { method ->
3646
val (httpMethod, path, async) = when {
3747
method.isAnnotationPresent<Before>() -> method.getAnnotation<Before>()!!.let { Triple(Route.BEFORE, it.value, it.async) }
3848
method.isAnnotationPresent<BeforeMatched>() -> method.getAnnotation<BeforeMatched>()!!.let { Triple(BEFORE_MATCHED, it.value, it.async) }
@@ -86,7 +96,7 @@ internal class ReflectiveEndpointLoader(
8696
val endpointClass = endpoint::class.java
8797
val dslExceptions = mutableListOf<AnnotatedException>()
8898

89-
endpointClass.declaredMethods.forEach { method ->
99+
getAllDeclaredMethods(endpointClass).forEach { method ->
90100
val exceptionHandlerAnnotation = method.getAnnotation<ExceptionHandler>() ?: return@forEach
91101

92102
require(method.trySetAccessible()) {
@@ -128,7 +138,7 @@ internal class ReflectiveEndpointLoader(
128138
val endpointClass = endpoint::class.java
129139
val dslEvents = mutableMapOf<JavalinLifecycleEvent, AnnotatedEvent>()
130140

131-
endpointClass.declaredMethods.forEach { method ->
141+
getAllDeclaredMethods(endpointClass).forEach { method ->
132142
val lifecycleEventHandler = method.getAnnotation<LifecycleEventHandler>() ?: return@forEach
133143

134144
require(method.trySetAccessible()) {

0 commit comments

Comments
 (0)