码迷,mamicode.com
首页 > 其他好文 > 详细

Intellij IDEA 自定义 getter and setter

时间:2018-05-29 18:50:36      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:ase   mod   new   str   OLE   param   模板   div   uri   

选择某个类,Alt+Insert,选择getter and setter

编辑getter template,输入以下,将boolean类型生成getXx()和setXx(),而不是isXx()方法:

#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get${name}() {
return this.$field.name;
}

编辑setter template,输入以下,会生成如public className setXx()的方法。

#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
$classname 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;
return this;
}

生成示例代码如下:

public class User {
    private boolean flag;

    public boolean getFlag() {
        return flag;
    }

    public User setFlag(boolean flag) {
        this.flag = flag;
        return this;
    }
}

2,生成lombok风格的getter和setter

getter模板

#if($field.modifierStatic)  
static ##  
#end  
$field.type ##  
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))  
#set($name = $name.substring(0, 1).toLowerCase() + $name.substring(1))  
${name}() {  
return $field.name;  
}  

setter模板

#set($paramName = $helper.getParamName($field, $project))  
#if($field.modifierStatic)  
static ##  
#end  
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))  
#set($name = $name.substring(0, 1).toLowerCase() + $name.substring(1))  
$classname ${name}($field.type $paramName) {  
#if ($field.name == $paramName)  
    #if (!$field.modifierStatic)  
    this.##  
    #else  
        $classname.##  
    #end  
#end  
$field.name = $paramName;  
return this;  
}  

Intellij IDEA 自定义 getter and setter

标签:ase   mod   new   str   OLE   param   模板   div   uri   

原文地址:https://www.cnblogs.com/winner-0715/p/9106295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!