为了解决用一个命令(宏)给方法,类,js方法添加注释,经过几天的研究.终于得到结果了.

实现的效果如下:

给Java中的method添加方法:

  1. /**
  2. *
  3. * @Method : addMenu
  4. * @Description :
  5. * @param menu :
  6. * @return : cn.yiyizuche.common.base.ResultMsg
  7. * @author : Rush.D.Xzj
  8. * @CreateDate : 2017-06-12 星期一 18:17:42
  9. *
  10. */public ResultMsg addMenu(Menu menu){
  11. ResultMsg result = null;
  12. return result;
  13. }

给 Java class 添加注释:

  1. /**
  2. *
  3. * @Project : 壹壹OA
  4. * @Package : cn.yiyizuche.common.ou.menu.controller
  5. * @Class : MenuController
  6. * @Description :
  7. * @author : Rush.D.Xzj
  8. * @CreateDate : 2017-06-12 星期一 18:15:32
  9. * @version : V1.0.0
  10. * @Copyright : 2017 yizukeji Inc. All rights reserved.
  11. * @Reviewed :
  12. * @UpateLog : Name Date Reason/Contents
  13. * ---------------------------------------
  14. * *** **** ****
  15. *
  16. */
  17. public class MenuController {
  18. }

给js 的方法添加注释

  1. /**
  2. *
  3. * @Method : standardShowBatchCheckBox
  4. * @Description :
  5. * @return :
  6. * @author : Rush.D.Xzj
  7. * @CreateDate : 2017-06-12 星期一 18:15:21
  8. *
  9. */
  10. function standardShowBatchCheckBox(jspElement, dataList, max, valueIdFunction, textFunction, selectedFunction) {
  11. }

现在把答案公布如下

Abbreviation:

  1. *

Template text:

  1. **
  2. *$context$
  3. */

Edit variables:

$context$的代码(主要代码)如下:

  1. groovyScript("def methodName = \"${_1}\"; def jsMethodName = \"${_2}\"; def outputMethodName = \"${_3}\"; def outputDesc = \"${_4}\"; def outputParams = \"${_5}\"; def outputAuthor = \"${_6}\"; def outputReturnType = \"${_7}\"; def outputDateTime = \"${_8}\"; def outputPackage = \"${_9}\"; def outputClass = \"${_10}\"; def outputClassOtherInfo = \"${_11}\"; def outputProject = \"${_12}\"; def outputVersion = \"${_13}\"; def outputJsMethodName = \"${_14}\"; def outputJsReturnType = \"${_15}\"; def result = ''; if (methodName != 'null') { result += '\\n'; result += outputMethodName; result += outputDesc; result += outputParams; result += outputReturnType; result += outputAuthor; result += outputDateTime; result += ' *'; return result;} else if (jsMethodName != 'null') { result += '\\n'; result += outputJsMethodName; result += outputDesc; result += outputJsReturnType; result += outputAuthor; result += outputDateTime; result += ' *'; return result;} else { result += '\\n'; result += outputProject; result += outputPackage; result += outputClass; result += outputDesc; result += outputAuthor; result += outputDateTime; result += outputVersion; result += outputClassOtherInfo; result += ' *'; return result;} ", methodName(), jsMethodName(), groovyScript("def methodName = \"${_1}\"; def result = ' * @Method : ' + methodName + '\\n'; return result;", methodName()), groovyScript("def result = ' * @Description : ' + '\\n'; return result;"), groovyScript("if(\"${_1}\".length() == 2) {return '';} else {def result=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {result+=' * @param ' + params[i] + ' : ' + '\\n'}; return result;}", methodParameters()), groovyScript("def author = 'Rush.D.Xzj'; def result = ' * @author : ' + author + '\\n'; return result;"), groovyScript("def returnType = \"${_1}\"; if (returnType == 'void') { return ''; } else { return ' * @return : ' + returnType + '\\n'; }", methodReturnType()), groovyScript("def date = \"${_1}\"; def time = \"${_2}\"; def result = ' * @CreateDate : ' + date + ' ' + time + '\\n'; return result;", date("yyyy-MM-dd E"), time("HH:mm:ss")), groovyScript("def currentPackage = \"${_1}\"; def result = ' * @Package : ' + currentPackage + '\\n'; return result;", currentPackage()), groovyScript("def className = \"${_1}\"; def result = ' * @Class : ' + className + '\\n'; return result;", className()), groovyScript("def result = ' * @Copyright : 2017 yizukeji Inc. All rights reserved.' + '\\n'; result += ' * @Reviewed : ' + '\\n'; result += ' * @UpateLog : Name Date Reason/Contents' + '\\n'; result += ' * ---------------------------------------' + '\\n'; result += ' * *** **** **** ' + '\\n'; return result;"), groovyScript("def result = ' * @Project : 壹壹OA' + '\\n'; return result;"), groovyScript("def result = ' * @version : V1.0.0' + '\\n'; return result;"), groovyScript("def jsMethodName = \"${_1}\"; def result = ' * @Method : ' + jsMethodName + '\\n'; return result;", jsMethodName()), groovyScript("def result = ' * @return : ' + '\\n'; return result;"))

使用如下:

在xx.java或者 xx.js中输出:

  1. /*

后 按 tab键(tab键是默认的,可以更改成其他的)

Edit Variable中代码详解


首先我分解了如下的 10几个函数(是小函数):

  1. // 输出方法名
  2. groovyScript("def methodName = \"${_1}\"; def result = ' * @Method : ' + methodName + '\\n'; return result;", methodName())
  3.  
  4. // 输出描述
  5. groovyScript("def result = ' * @Description : ' + '\\n'; return result;")
  6.  
  7. // 输出参数列表的子函数
  8. groovyScript("if(\"${_1}\".length() == 2) {return '';} else {def result=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {result+=' * @param ' + params[i] + ' : ' + '\\n'}; return result;}", methodParameters())
  9.  
  10. // 输出作者的子函数 这里可以改作者
  11. groovyScript("def author = 'Rush.D.Xzj'; def result = ' * @author : ' + author + '\\n'; return result;")
  12.  
  13. // 输出返回值的子函数
  14. groovyScript("def returnType = \"${_1}\"; def result = ' * @return : ' + returnType + '\\n'; return result;", methodReturnType())
  15. // 如果是return 是 void 那就不显示这@return
  16. groovyScript("def returnType = \"${_1}\"; if (returnType == 'void') { return ''; } else { return ' * @return : ' + returnType + '\\n'; }", methodReturnType())
  17.  
  18. // 输出日期的字函数, 这个可以更改日期格式
  19. groovyScript("def date = \"${_1}\"; def time = \"${_2}\"; def result = ' * @CreateDate : ' + date + ' ' + time + '\\n'; return result;", date("yyyy-MM-dd E"), time("HH:mm:ss"))
  20.  
  21. // 输出Project
  22. groovyScript("def result = ' * @Project : 壹壹OA' + '\\n'; return result;")
  23.  
  24. // 输出package
  25. groovyScript("def currentPackage = \"${_1}\"; def result = ' * @Package : ' + currentPackage + '\\n'; return result;", currentPackage())
  26.  
  27. // 输出class
  28. groovyScript("def className = \"${_1}\"; def result = ' * @Class : ' + className + '\\n'; return result;", className())
  29.  
  30. // 输出copyright/reviewd/updatelog
  31. groovyScript("def result = ' * @Copyright : 2017 yizukeji Inc. All rights reserved.' + '\\n'; result += ' * @Reviewed : ' + '\\n'; result += ' * @UpateLog : Name Date Reason/Contents' + '\\n'; result += ' * ---------------------------------------' + '\\n'; result += ' * *** **** **** ' + '\\n'; return result;")
  32.  
  33. // 输出version
  34. groovyScript("def result = ' * @version : V1.0.0' + '\\n'; return result;")
  35.  
  36. // 输出jsMethodName
  37. groovyScript("def jsMethodName = \"${_1}\"; def result = ' * @Method : ' + jsMethodName + '\\n'; return result;", jsMethodName())
  38.  
  39. // 输出 js 的return
  40. groovyScript("def result = ' * @return : ' + '\\n'; return result;")

然后在主要代码中,需要把上述的10几个函数当做参数供给主要代码使用.

判断是函数,类,js函数主要是通过如下的2个内置变量来实现的:

  1. methodName()
  2. jsMethodName()

当methodName()不为空的时候, 生成 方法的 注释

当jsMethodName()不为空的时候, 生成 js方法的注释

否则生成类的注释

所以上述的代码可以简单的做如下的归类了:

获取相关参数:

  1. def methodName = \"${_1}\";
  2. def jsMethodName = \"${_2}\";
  3. def outputMethodName = \"${_3}\";
  4. def outputDesc = \"${_4}\";
  5. def outputParams = \"${_5}\";
  6. def outputAuthor = \"${_6}\";
  7. def outputReturnType = \"${_7}\";
  8. def outputDateTime = \"${_8}\";
  9. def outputPackage = \"${_9}\";
  10. def outputClass = \"${_10}\";
  11. def outputClassOtherInfo = \"${_11}\";
  12. def outputProject = \"${_12}\";
  13. def outputVersion = \"${_13}\";
  14. def outputJsMethodName = \"${_14}\";
  15. def outputJsReturnType = \"${_15}\";
  16. def result = '';

判断是哪一种类型的注释(代码片段2):

  1. if (methodName != 'null') {
  2. result += '\\n';
  3. result += outputMethodName;
  4. result += outputDesc;
  5. result += outputParams;
  6. result += outputReturnType;
  7. result += outputAuthor;
  8. result += outputDateTime;
  9. result += ' *';
  10. return result;
  11. } else if (jsMethodName != 'null') {
  12. result += '\\n';
  13. result += outputJsMethodName;
  14. result += outputDesc;
  15. result += outputJsReturnType;
  16. result += outputAuthor;
  17. result += outputDateTime;
  18. result += ' *';
  19. return result;
  20. } else {
  21. result += '\\n';
  22. result += outputProject;
  23. result += outputPackage;
  24. result += outputClass;
  25. result += outputDesc;
  26. result += outputAuthor;
  27. result += outputDateTime;
  28. result += outputVersion;
  29. result += outputClassOtherInfo;
  30. result += ' *';
  31. return result;
  32. }

这下就可以了.就把整个这么难看(groopscript搞的?)代码整理的比较清晰了.

我还有如下的几个疑惑/问题需要解决, 如果有朋友能给我答案那就好了.

问题1. 不知道可以通过什么方法获取js方法中的参数列表

问题2. 代码片段2中如果我改成了:

  1. result += '\\n';
  2. if (methodName != 'null') {
  3. result += outputMethodName;
  4. result += outputDesc;
  5. result += outputParams;
  6. result += outputReturnType;
  7. result += outputAuthor;
  8. result += outputDateTime;
  9. } else if (jsMethodName != 'null') {
  10. result += outputJsMethodName;
  11. result += outputDesc;
  12. result += outputJsReturnType;
  13. result += outputAuthor;
  14. result += outputDateTime;
  15. } else {
  16. result += outputProject;
  17. result += outputPackage;
  18. result += outputClass;
  19. result += outputDesc;
  20. result += outputAuthor;
  21. result += outputDateTime;
  22. result += outputVersion;
  23. result += outputClassOtherInfo;
  24. }
  25. result += ' *';
  26. return result;

会出现如下的错误:

  1. startup failed:
  2. Script1.groovy: 1: expecting EOF, found 'result' @ line 1, column 1036.
  3. lt += outputClassOtherInfo; } result +=
  4. ^
  5.  
  6. 1 error

问题3: 不知道怎么获取Override方法对应的接口方法(父类方法)

关于问题3: 实际上给这种方法加注释是没有意义的,具体可以参考: https://stackoverflow.com/questions/4310858/comments-on-overridden-method-in-java

但是如果非要加该怎么去判断.

参考资料:

http://blog.sina.com.cn/s/blog_4c4195e70102wh7e.html

https://stackoverflow.com/questions/4310858/comments-on-overridden-method-in-java

更高级的用法请看:

http://www.cnblogs.com/xzjxylophone/p/7060035.html


idea live template高级知识, 进阶(给方法,类,js方法添加注释)的更多相关文章

  1. idea live template高级知识, 进阶(给方法,类,js方法添加注释)(二)

    上一篇文章(http://www.cnblogs.com/xzjxylophone/p/6994488.html) 是在 groovyScript中直接添加的代码,这个看起来是简单,粗暴,麻烦和不美观 ...

  2. IDEA使用 live template添加groovy脚本给方法,类,js方法添加注释(转载)

    IDEA添加Live Template: File->Setting->Editor->Live Templates Abbreviation: * Template text: * ...

  3. IDEA 创建类是自动添加注释和创建方法时快速添加注释

    1.创建类是自动添加注释 /*** @Author: chiyl* @DateTime: ${DATE} ${TIME}* @Description: TODO*/2. 创建方法时快速添加注释2.1 ...

  4. day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)

    1. 装饰器 / 类中的方法 / 类的方法变属性 # ### 装饰器 """ 定义:装饰器用于拓展原来函数功能的一种语法,返回新函数替换旧函数 优点:在不更改原函数代码的 ...

  5. accessor mothod mutator mothod 更改器方法 访问器方法 类的方法可以访问类的任何一个对象的私有域!

    LocalDate.plusDate String.toUpperCase GregorianCalendar.add import java.time.*; public class Calenda ...

  6. thymeleaf onclick方法向js方法传递参数

    如下图 这个错误并不影响 请放心使用

  7. python高级(一)—— python数据模型(特殊方法)

    本文主要内容 collections.namedtuple __getitem__ 和 __len__ __repr__和__str__ __abs__.__add__和__mul__ __bool_ ...

  8. java类的方法

    我前面说的都是类的属性,现在说类的方法: 类{ 属性:“类有什么” 方法:“类能做什么” } 首先我们在之前创建的学生类中添加个打印1-10数字的方法,参数是name package cuteSnow ...

  9. iOS Swift结构体与类的方法调度

    前言 hello,小伙伴们:在忙碌中闲暇之余给大家聊聊swift的知识点,今天给大家带来的是swift中结构体与类的方法调度详细区别,希望对你有所帮助,好了废话不用多说,接下来步入主题! 1.普通方法 ...

随机推荐

  1. bzoj1798 [Ahoi2009]维护序列

    Description 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2 ...

  2. tomcat 下部署单框架cas时,报出org.apache.jasper.JasperException异常的解决办法

    在tomcat中部署好cas server(设置好https,将cas.war拷贝到了webapps下部署完成),启动tomcat后,访问http://localhost:8443/cas/login ...

  3. 蓝桥杯-无穷分数-java

    /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...

  4. 安装hexo报错(npm WARN deprecated swig@1.4.2: This package is no longer maintained),已解决

    问题:在使用npm安装hexo时报错 $ npm install -g hexo npm WARN deprecated swig@1.4.2: This package is no longer m ...

  5. 针对iPhone的pt、Android的dp、HTML的css像素与dpr、设计尺寸和物理像素的浅分析

    最近被一朋友问到:css中设置一DOM的height:65px,请问显示的高度是否和Android的65dp的元素等高?脑子里瞬间闪现了一堆的概念,如dpr,ppi,dp,pt等,然而想了一阵,浆糊了 ...

  6. sql注入基础(原理)

    一.注入的分类 按数据库分类:1.整形 2.字符型(需要考虑单引号闭合的问题,还有注释不必要的语句  #  ) eag:  id='admin'  id='admin and1=1'这样会报错  所以 ...

  7. MySQL ProxySQL读写分离使用初探

    目的 在美团点评DBProxy读写分离使用说明文章中已经说明了使用目的,本文介绍ProxySQL的使用方法以及和DBProxy的性能差异.具体的介绍可以看官网的相关说明,并且这个中间件也是percon ...

  8. vue视频学习笔记

    video 7 vue问题: 论坛 http://bbs.zhinengshe.com------------------------------------------------UI组件 别人提供 ...

  9. 【19道XSS题目】不服来战!

    记得第一次接触xss这个概念是在高中,那个时候和一个好基友通过黑客X档案和黑客手册.第一次接触到了除了游戏以外的电脑知识,然后知道了,原来电脑除了玩游戏还可以搞这些,从此两人一发不可收拾的爱上了玩黑这 ...

  10. RabbitMQ学习-1补充

    1.如果尝试声明一个已经存在的队列会发生什么? 只要参数完全匹配现存的队列的话,Rabbit什么也不做,并返回成功,就好像这个队列已经创建成功. 2.如何检测队列是否存在? 在创建队列的时候设置que ...