近日在研究重構代碼的時候有用到idea的不少插件,比如CheckStyle,同時下載了阿里的開發規約,受到不少啟發。
規約中會要求所有的方法都有Javadoc,但是通常我們用idea默認生成的getter和setter方法是不帶注釋的,當然,我們同樣可以設置idea像MyEclipse一樣生成帶有Javadoc的模板,具體解決方案如下:
比如我們有這么一個pojo類:
/** * 人類. * @author eric */public final class People { /** * 姓名. */ private String name; /** * 年齡. */ private Integer age;}
這個時候我們按下 Alt + Insert (Windows),或者 Command + N (MacOS)來打開生成窗口:
這里我們選擇Getter and Setter。
打開如下窗口:
我們點擊如圖箭頭所示的地方,打開如下視圖:
然后我們新建一個Template,點擊圖中的 "+" 號,然后輸入名稱(當然你可以起個自己喜歡的名稱)
點擊OK。之后在編輯區域復制粘貼如下代碼:
/** * Gets the value of $field.name * @return the value of $field.name */public ###if($field.modifierStatic) static ###end$field.type ###set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))#if ($field.boolean && $field.primitive) #if ($StringUtil.startsWithIgnoreCase($name, 'is')) #set($name = $StringUtil.decapitalize($name)) #else is## #end#else get###end${name}() { return $field.name;}
完畢后點擊OK。
Setter的配置同上一致,我們只需將編輯區內的代碼改下(當然Template Name也可以改下:D):
/** * Sets the $field.name * <p>You can use get$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))() to get the value of $field.name</p> * @param $field.name $field.name */#set($paramName = $helper.getParamName($field, $project))public ###if($field.modifierStatic) static ###endvoid set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) {#if ($field.name == $paramName) #if (!$field.modifierStatic) this.## #else $classname.## #end#end$field.name = $paramName;}
下面就讓我們來看下成果:
在這里注意要選擇我們剛配置好的Template,而不是Default。
點擊OK。
到這里算是大功告成了。
總結
以上所述是小編給大家介紹的IDEA設置生成帶注釋的getter和setter的圖文教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答
圖片精選