Jason5lee
01/14/2023, 1:38 AMIllegalAccessError
when using ktor. I defined a Controller
interface. A controller can be added to a router. I made a helper method to create a controller that wraps the handler with my exception handling and other logics. A simplified version is
interface Controller {
fun addToRouter(routing: Routing)
companion object {
fun handleException(e: Throwable) {
throw e
}
inline fun create(method: HttpMethod, path: String, crossinline handler: suspend () -> Unit): Controller = object : Controller {
override fun addToRouter(routing: Routing) {
routing.route(path, method) {
handle {
try {
handler()
} catch (e: Throwable) {
handleException(e)
}
}
}
}
}
}
}
In the controllers
package, there is a controller with a body that may throw an exception.
val errorController = Controller.create(HttpMethod.Get, "/error") {
throw Exception("This is an error")
}
In the Routing.kt
, all controllers are added to the routing.
routing {
controller.errorController.addToRouter(this)
// other controllers
}
Now when I tried to call the controller, if an exception is thrown, the IllegalAccessError
is caused.
java.lang.IllegalAccessError: class controller.ErrorControllerKt$special$$inlined$create$1$1$1 tried to access field routing.Controller$Companion.$$INSTANCE (controller.ErrorControllerKt$special$$inlined$create$1$1$1 and routing.Controller$Companion are in unnamed module of loader 'app')
Why will it happen?Jason5lee
01/14/2023, 1:42 AMinline
, but I'm curious about the reason.Jason5lee
01/14/2023, 6:52 AMJason5lee
01/14/2023, 7:15 AM