Isolate the instantiation of an annotation

Luís Soares :

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
}

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
}

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

This would make the code cleaner and the docs segregated.

Enselic :

Ok so what you want is the @OpenApi docs to be separated from the REST handler code. You can do that by moving away the implementation rather than moving away the annotation.

So in your current file with all the @OpenApi annotations mixed with REST handler code, you call helper functions, like this:

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

and then you put - either in the top of that file or in another file for even more segregation - all REST handlers, which allows you to scroll among them without the noise of @OpenApi annotations:

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

Then you can easily scroll among your REST handler code without being bothered by @OpenApi noise.

Note that you should use the Go To -> Definition functionality of Android Studio to avoid having to scroll to handleGetCustomers().

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=163573&siteId=1