Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Memexurer committed Jul 22, 2024
1 parent 17b06fc commit 7fe461a
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ internal class ReflectiveEndpointLoader(

private val repeatedPathSeparatorRegex = Regex("/+")

private fun getAllDeclaredMethods(clazz: Class<*>): Collection<Method> {
val methods = mutableListOf<Method>()
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<AnnotatedRoute> {
val endpointClass = endpoint::class.java

Expand All @@ -32,7 +42,7 @@ internal class ReflectiveEndpointLoader(

val endpointRoutes = mutableListOf<AnnotatedRoute>()

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

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

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

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

require(method.trySetAccessible()) {
Expand Down

0 comments on commit 7fe461a

Please sign in to comment.