idea live template高級知識, 進階(給方法,類,js方法添加註釋)

来源:http://www.cnblogs.com/xzjxylophone/archive/2017/06/12/6994488.html
-Advertisement-
Play Games

為瞭解決用一個命令(巨集)給方法,類,js方法添加註釋,經過幾天的研究.終於得到結果了. 實現的效果如下: 給Java中的method添加方法: 給 Java class 添加註釋: 給js 的方法添加註釋 現在把答案公佈如下 Abbreviation: Template text(註1): Edit ...


為瞭解決用一個命令(巨集)給方法,類,js方法添加註釋,經過幾天的研究.終於得到結果了.

實現的效果如下:

給Java中的method添加方法:

    /**
     *
     * @Method : addMenu
     * @Description :
     * @param menu :
     * @return : cn.yiyizuche.common.base.ResultMsg
     * @author : Rush.D.Xzj
     * @CreateDate : 2017-06-12 星期一 18:17:42
     *
     */public ResultMsg addMenu(Menu menu){
        ResultMsg result = null;
        return result;
    }

給 Java class 添加註釋:

/**
 *
 * @Project : 壹壹OA
 * @Package : cn.yiyizuche.common.ou.menu.controller
 * @Class : MenuController
 * @Description :
 * @author : Rush.D.Xzj
 * @CreateDate : 2017-06-12 星期一 18:15:32
 * @version : V1.0.0
 * @Copyright : 2017 yizukeji Inc. All rights reserved.
 * @Reviewed :
 * @UpateLog :    Name    Date    Reason/Contents
 *             ---------------------------------------
 *                 ***        ****    ****
 *
 */
public class MenuController {
}

給js 的方法添加註釋

/**
 *
 * @Method : standardShowBatchCheckBox
 * @Description :
 * @return :
 * @author : Rush.D.Xzj
 * @CreateDate : 2017-06-12 星期一 18:15:21
 *
 */
function standardShowBatchCheckBox(jspElement, dataList, max, valueIdFunction, textFunction, selectedFunction) {
}

 

 

現在把答案公佈如下

Abbreviation:

cmj

Template text(註1):

**
 *$context$ 
 */

Edit variables:

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

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}\"; def result = ' * @return : ' + returnType + '\\n'; return result;", 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中輸出(註2):

/cmj

後 按 tab鍵(此鍵是預設的,可以更改成其他的)

註1 和註2

也可以換成第二種方法(網上大部分的方法):

Template text

*
 *$context$ 
 */

跟註1比較第一行少了一個*,

因此註2就是變成了:

/*cmj

我感覺用我的方法比第二種方法好.

 

 

Edit Variable中代碼詳解


首先我分解瞭如下的 10幾個函數(是小函數):

    // 輸出方法名
    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}\"; def result = ' * @return : ' + returnType + '\\n'; return result;", 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"))

    // 輸出Project
    groovyScript("def result = ' * @Project : 壹壹OA' + '\\n'; return result;")
    
    // 輸出package
    groovyScript("def currentPackage = \"${_1}\"; def result = ' * @Package : ' + currentPackage + '\\n'; return result;", currentPackage())
    
    // 輸出class
    groovyScript("def className = \"${_1}\"; def result = ' * @Class : ' + className + '\\n'; return result;", className())
    
    // 輸出copyright/reviewd/updatelog
    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;")
    
    // 輸出version
    groovyScript("def result = ' * @version : V1.0.0' + '\\n'; return result;")

    // 輸出jsMethodName
    groovyScript("def jsMethodName = \"${_1}\"; def result = ' * @Method : ' + jsMethodName + '\\n'; return result;", jsMethodName())
    
    // 輸出 js 的return
    groovyScript("def result = ' * @return : ' + '\\n'; return result;")

 

然後在主要代碼中,需要把上述的10幾個函數當做參數供給主要代碼使用.

判斷是函數,類,js函數主要是通過如下的2個內置變數來實現的:

methodName()
jsMethodName()

當methodName()不為空的時候, 生成 方法的 註釋

當jsMethodName()不為空的時候, 生成 js方法的註釋

否則生成類的註釋

所以上述的代碼可以簡單的做如下的歸類了:

獲取相關參數:

    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 = '';

判斷是哪一種類型的註釋(代碼片段2):

   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;
    }

這下就可以了.就把整個這麼難看(groopscript搞的?)代碼整理的比較清晰了.

 

我還有如下的幾個疑惑/問題需要解決, 如果有朋友能給我答案那就好了.

問題1. 不知道可以通過什麼方法獲取js方法中的參數列表

問題2. 代碼片段2中如果我改成了:

    result += '\\n';
    if (methodName != 'null') {
        result += outputMethodName;
        result += outputDesc;
        result += outputParams;
        result += outputReturnType;
        result += outputAuthor;
        result += outputDateTime;
    } else if (jsMethodName != 'null') {
        result += '\\n';
        result += outputJsMethodName;
        result += outputDesc;
        result += outputJsReturnType;
        result += outputAuthor;
        result += outputDateTime;
    } else {
        result += '\\n';
        result += outputProject;
        result += outputPackage;
        result += outputClass;
        result += outputDesc;
        result += outputAuthor;
        result += outputDateTime;
        result += outputVersion;
        result += outputClassOtherInfo;
    }
    result += ' *';
    return result;

會出現如下的錯誤:

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

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

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 第一節 Python文件類型 源代碼 Python源代碼的文件以“py”為擴展名,由Python程式解釋,不需要編譯 位元組代碼 Python源文件經編譯後生成的擴展名為“pyc”的文件 編譯方法 優化代碼 經過優化的源文件,擴展名為“.pyo” 以上三種均可直接運行 第二節 Python變數 變數的 ...
  • 繼承定義一個類B時,發現已有類A與要定義的類B相似,並且類B屬於類A的一種時,可以將類B定義為類A的子類。 當多個類有著共性內容,可以將共性內容向上抽取,抽取到一個新的類中,這個新類和多個類形成一個關係叫做繼承。 子類繼承父類後,自動擁有父類所有可繼承的屬性和功能。 註意事項 java只支持單繼承, ...
  • 基本語法 shell的if語法和C語言等高級語言非常相似,唯一需要註意的地方就是shell的if語句對空格方面的要求比較嚴格(其實shell對所有語法的空格使用都比較嚴格),如果在需要空格的地方沒有打上空格,都會報錯。如if [ $1x == "ip"x ];then echo "abc";fi中少 ...
  • 今天上午找資料,發現了mongodb,閑來無事就研究了下,本人用的是phpstudy集成包添加mongodb擴展。 1.添加mongo、mongodb擴展 phpstudy集成環境一般用的是32位php,去官網下載32位的mongo、mongodb擴展 mongo:http://pecl.php.n ...
  • 期末複習比較忙過段時間來專門寫scrapy框架使用,今天介紹如何用python生成詞雲,雖然網上有很多詞雲生成工具,不過自己用python來寫是不是更有成就感。 今天要生成的是勵志歌曲的詞雲,百度文庫裡面找了20來首,如《倔強》,海闊天空是,什麼的大家熟悉的。 所要用到的python庫有 jieba ...
  • 鍵盤錄入(Java): 1.導包 格式 import java.util.Scanner; 位置 在class上面 2.創建鍵盤錄入對象 格式 Scanner sc = new Scanner(System.in); 3.通過對象獲取數據 格式 int x = sc.nextInt(); impor ...
  • 最近看了看JavaWeb的書籍,才感覺到大二時候學的JavaWeb才僅僅只是個入門。最尷尬的當初還沒咋學一直在看.NET,現在看起來JavaWeb,各種框架各種頭疼啊。看了幾個例子之後覺得,還是自己動手做個項目試試。邊做邊學才能學的快一點。做的時候用博客園記錄下過程,省得以後忘。之所以選圖書館管理系 ...
  • 本文是作者在學習了軟體工程(C編碼實踐篇)課程後的學習總結性質的文章 希望能通過自己的一些理解和經驗幫助大家更好地理解軟體工程的有關內容。。 學生 戢祥 課程地址《軟體工程(C編碼實踐篇)》MOOC課程http://mooc.study.163.com/course/USTC-1000002006 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...