自定义idea的set和get方法

idea自定义的set、get方法是这样子的

public class Person {
    private String name;
    private Integer age;
    private Boolean isStudent;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getStudent() {
        return isStudent;
    }

    public void setStudent(Boolean student) {
        isStudent = student;
    }
}

尤其是Boolean类型的修改,每次都要手动改回去,挺麻烦的,研究了一下idea的live template的语法,修改后效果为:

public class Person {
    private String name;
    private Integer age;
    private Boolean isStudent;

    public String getName() {
        return name;
    }

    public Person setName(String name) {
        this.name = name;
        return this;
    }

    public Integer getAge() {
        return age;
    }

    public Person setAge(Integer age) {
        this.age = age;
        return this;
    }

    public Boolean getIsStudent() {
        return isStudent;
    }

    public Person setIsStudent(Boolean isStudent) {
        this.isStudent = isStudent;
        return this;
    }
}

自定义脚本为:

set方法

#set($paramName =  $field.name)
public
#if($field.modifierStatic)
static ##
#end

$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($paramName)))
get##
${name}() {
return $field.name;
}

get方法

###set($paramName =   $helper.getParamName($field, $project))
#set($paramName =  $field.name)
public ##
#if($field.modifierStatic)
static ##
#end
$classname   set$StringUtil.capitalizeWithJavaBeanConvention($paramName)($field.type $paramName) {
#if ($field.name == $paramName)
    #if (!$field.modifierStatic)
    this.##
    #else
        $classname.##
    #end
#end
$field.name = $paramName;
return this;
}

至于在哪里添加呢?
1.command+N或者鼠标右键,genereate
这里写图片描述
2.Getter and Setter
这里写图片描述
3.点击+,新增一个
这里写图片描述

语法形式和velocity挺像的。其中最终要的是field变量,加入你选中的是name,那么:

field:
AbstractElement {
            name = 'name', 
            isPrimitiveArray = false, 
            isStringArray = false, 
            isCollection = false, 
            isMap = false, 
            isList = false, 
            isPrimitive = false, 
            isDate = false, 
            isCalendar = false,
            isLong = false, 
            isDouble = false, 
            isVoid = false, 
            isByte = false, 
            type = 'java.lang.String', 
            typeQualifiedName = 'java.lang.String',  
            isModifierStatic = false,
            isModifierProtected = false,
             isModifierPackageLocal = false, 
            isModifierFinal = false
        } :::FieldElement {
            isConstant = false, isEnum = false, isModifierTransient = false, isModifierVolatile = false
        }
阅读更多
文章标签: idea
个人分类: 开发工具
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭