RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1409089
Accepted
Zhenyria
Zhenyria
Asked:2022-07-12 20:33:47 +0000 UTC2022-07-12 20:33:47 +0000 UTC 2022-07-12 20:33:47 +0000 UTC

什么技术可以以这种方式转换文本?

  • 772

有大量的方法需要以某种方式重写。例如,这是原始方法:

/**
 * Finds all
 * @param id    the id
 * @param codes list of codes
 * @return entities
*/
@GET
@Path("/get")
@Produces(APPLICATION_JSON)
public List<Entity> get(@QueryParam("id") Integer id, @QueryParam("barcodes") List<Long> codes) {
  return entityBean.findByIdAndCodes(id, codes);
}

应该这样重写:

/**
 * Finds all
 * @param id    the id
 * @param codes list of codes
 * @return entities
*/
@RequestLine("GET " + PATH + "get")
List<Entity> get(@Param("id") Integer id, @Param("codes") List<Long> codes);

也就是说,重写代码应该按照以下场景进行:

  • 方法的主体由符号 代替;。
  • 该方法具有访问修饰符。
  • 注释掉了。@GET_@Path@Produces
  • 注释被传送而不作任何更改。
  • 在方法上方形成以下形式的字符串:@RequestLine("[аннотация @GET без символа @]" + PATH + "[содержимое аннотации @Path без первого символа /]").
  • 方法参数注释从@QueryParam变为@Param。

返回类型、参数个数和名称在不同的方法中可以不同。@GET此外,它可以是@POSTor ,而不是 annotation @DELETE。

什么技术可以让我最有效地执行这样的代码转换?

java текст
  • 1 1 个回答
  • 140 Views

1 个回答

  • Voted
  1. Best Answer
    Zhenyria
    2022-07-13T14:59:38Z2022-07-13T14:59:38Z

    为了解决这个问题,我使用Live template了 - 这是一个工具,IDEA可以让您创建方便的模板来转换/生成代码。

    模板本身看起来像这样:

    $METHOD$
    

    模板代码(为了转换我使用脚本编写的文本Groovy):

    groovyScript("D:\\method.groovy", clipboard())
    

    groovyScript()这里它用于指定将用于转换文本的脚本(第一个参数是脚本本身或带有脚本的文件的路径,第二个参数是传递给脚本的文本变量)。clipboard()是剪贴板的内容。

    脚本本身:

    def result = '';
    
    def commentMatcher = _1 =~ "(/\\*{2}[^/]*\\*/)";
    if (commentMatcher.find()) result = commentMatcher[0][0];
    
    result += '\r\n@RequestLine("';
    def httpMethodMatcher = _1 =~ "@(GET|POST|DELETE)";
    if (httpMethodMatcher.find()) {
        result += httpMethodMatcher[0][1];
        result += ' " + PATH + ';
    }
    
    def isSignatureMatcherFindAnything = false;
    def signatureMatcher = _1 =~ "(public |)([0-9a-zA-Z<>._,]*) ([a-zA-Z]*)(\\([^{]*\\))\\s*(throws [a-zA-Z0-9]+|)";
    if (signatureMatcher.find()) {
        isSignatureMatcherFindAnything = true;
        result += '"';
        result += signatureMatcher[0][3].replaceAll('([A-Z])', '-$1').toLowerCase().replaceAll('^_', '');
    }
    
    def paramsMatcher = signatureMatcher[0][4] =~ "(@QueryParam)(\\(\"([a-zA-Z0-9]+)\"\\))";
    if (paramsMatcher.find()) {
        result += '?'
        for (def i = 0; i < paramsMatcher.size(); i++) {
            if (i > 0) {
                result += '&';
            }
            def param = paramsMatcher[i][3];
            result += param;
            result += '={';
            result += param;
            result += '}';
        }
    }
    result += '")'
    
    def headersMatcher = _1 =~ "(\\@Consumes)(\\((\"|)([^\"\\)]+)(\"|)\\))";
    if (headersMatcher.find()) {
        result += '\r\n@Headers(';
        def content = headersMatcher[0][4];
        if (content.contains('8')) {
            result += 'CONTENT_TYPE_APPLICATION_JSON_UTF_8';
        } else {
            result += 'CONTENT_TYPE_APPLICATION_JSON';
        }
        result += ')';
    }
    
    if (isSignatureMatcherFindAnything) {
        result += '\r\n'
        result += signatureMatcher[0][2];
        result += ' ';
        result += signatureMatcher[0][3];
        result += signatureMatcher[0][4].replaceAll("@QueryParam", "@Param");
        
        if (signatureMatcher[0].size() == 6) {
            result += ' ';
            result += signatureMatcher[0][5];
        }
        
        result += ';';
    }
    
    return result;
    

    结果Live template是这样的:

    1. 复制方法。
    2. 我们启动Live template (使用之前指定的快捷方式)。

    因此这段代码:

    /**
     * Finds all
     * @param id    the id
     * @param codes list of codes
     * @return entities
    */
    @GET
    @Path("/get")
    @Produces(APPLICATION_JSON)
    public List<Entity> get(@QueryParam("id") Integer id, @QueryParam("barcodes") List<Long> codes) {
      return entityBean.findByIdAndCodes(id, codes);
    }
    

    变成以下内容:

    /**
     * Finds all
     * @param id    the id
     * @param codes list of codes
     * @return entities
    */
    @RequestLine("GET " + PATH + "get")
    List<Entity> get(@Param("id") Integer id, @Param("codes") List<Long> codes);
    
    • 6

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5