Isolate the instantiation of an annotation

Luí*_*res 6 java annotations kotlin openapi javalin

I have a huge @OpenApi annotation (basically it's documentation of a Javalin/Kotlin endpoint) which occupies a lot of lines:

@OpenApi(
   summary = "",
   description = "Lists all customers",
   path = "customers",
   queryParams =
   // ...........
   // ...........
   // etc
)
override fun handle(context: Context) {
   // body of the REST handler
}
Run Code Online (Sandbox Code Playgroud)

I have to scroll a lot to see the actual handler. Hence, I'd like to isolate it somehow like:

@GetCustomersDoc
override fun handle(context: Context) {
   // body of the REST handler
}
Run Code Online (Sandbox Code Playgroud)

I'm OK with other solutions that make the documentation go elsewhere.

This would make the code cleaner and the docs segregated.

Ens*_*lic 0

好的,您想要的是@OpenApi将文档与 REST 处理程序代码分开。您可以通过删除实现而不是删除注释来做到这一点。

因此,在所有注释与 REST 处理程序代码混合的当前文件中@OpenApi,您可以调用辅助函数,如下所示:

@OpenApi(
   summary = "",
   description = "Lists all customers",
   path = "customers",
   queryParams =
   // ...........
   // ...........
   // etc
)
override fun handle(context: Context) {
   handleGetCustomers(context)
}
Run Code Online (Sandbox Code Playgroud)

然后您将所有 REST 处理程序放入该文件的顶部或另一个文件中以进行更多隔离,这样您就可以在它们之间滚动,而不会受到@OpenApi注释的干扰:

// Collected at the top of the file, or in a separate file
fun handleGetCustomers(context: Context) {
    // body of the REST handler
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以轻松地在 REST 处理程序代码之间滚动,而不会受到@OpenApi噪音的困扰。

请注意,您应该使用Android Studio 的“转到” -> “定义”功能,以避免滚动到handleGetCustomers().