隔离注解的实例化

我有一个巨大的@OpenApi注释(基本上它是Javalin /Kotlin 端点的文档),它占据了很多行:

public class ModuleGrader {

    final int examID = 123;

    //String excellent=null;

    //String good=null;

    //String satisfactory=null;

    //String compensatableFail=null;

    //String outrightFail=null;

    int grade;


    public String gradeModule(int mark) {

        String result = null;

        if (mark>=70 && mark<=100) 

        {

            result = "excellent";

            System.out.println(" ");

            }

        else if (mark>=60 && mark<=69)

        {

            result = "good";

        }

        else if (mark>=50 && mark<=59)

        {

            result = "satisfactory";

        }

        else if (mark>=40 && mark<=49)

        {

            result = "compensatableFail";

        }

        else if (mark>=0 && mark<=39) {

            result = "outrightFail";

        }

        else {

            System.out.println("Invalid entery, please insert an number between 100-0");

                    }


        return result;

    }


尚方宝剑之说
浏览 125回答 2
2回答

拉莫斯之舞

您可以定义单独的注释:annotation class MyOwnApi(&nbsp; &nbsp; val openApi: OpenApi = OpenApi(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; summary = "",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description = "Lists all customers",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path = "customers",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; queryParams =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...........&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...........&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // etc&nbsp; &nbsp; &nbsp; &nbsp; ))annotation class UserOpenApi(&nbsp; &nbsp; &nbsp; &nbsp; val openApi: OpenApi = OpenApi(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; summary = "Something",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description = "Lists all users",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...........&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...........&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // etc&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; )优点:代码分离可重用的注释类您甚至可以创建一个构建器类并测试它缺点:令人困惑注解不能继承、扩展类或实现接口如果直接检查类/对象,可能无法@OpenApi实现或需要复杂的代码更改。在这种情况下,您将需要另一个反射搜索来从字段中提取注释!

繁星淼淼

好的,您想要的是@OpenApi将文档与 REST 处理程序代码分开。您可以通过删除实现而不是删除注释来做到这一点。因此,在所有注释与 REST 处理程序代码混合的当前文件中@OpenApi,您可以调用辅助函数,如下所示:@OpenApi(&nbsp; &nbsp;summary = "",&nbsp; &nbsp;description = "Lists all customers",&nbsp; &nbsp;path = "customers",&nbsp; &nbsp;queryParams =&nbsp; &nbsp;// ...........&nbsp; &nbsp;// ...........&nbsp; &nbsp;// etc)override fun handle(context: Context) {&nbsp; &nbsp;handleGetCustomers(context)}然后您将所有 REST 处理程序放入该文件的顶部或另一个文件中以进行更多隔离,这样您就可以在它们之间滚动,而不会受到注释的干扰@OpenApi:// Collected at the top of the file, or in a separate filefun handleGetCustomers(context: Context) {&nbsp; &nbsp; // body of the REST handler}然后,您可以轻松地在 REST 处理程序代码之间滚动,而不会受到@OpenApi噪音的困扰。请注意,您应该使用Android Studio 的“转到” -> “定义”功能,以避免滚动到handleGetCustomers().
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java