当前位置: 首页 > 后端技术 > Java

一个简单易用的Java插件——Lombok

时间:2023-04-01 23:25:59 Java

作者:烤鸡太子大师来源:恒生LIGHT云社区前面介绍过一个javamaven插件mavenhelper。很多同学都很感兴趣。今天再接再厉,分享给大家然后推荐一个还在用的java插件,叫lombok。先给大家说说Lombok的主要功能和使用方法,以后有时间再说说Lombok的原理。什么是Lombok先看看官网的介绍。ProjectLombok是一个java库,它会自动插入您的编辑器和构建工具,为您的java增添趣味。再也不用编写另一个getter或equals方法,只需一个注释,您的类就有一个功能齐全的构建器、自动化您的日志变量等等。lombok是一种可以帮助我们简化和淘汰一些必须但臃肿的Java代码工具的形式。简单的说,比如我们新建一个类,在里面写几个字段。通常,我们需要手动创建getter和setter方法和构造函数。类,lombok的作用就是省去我们手动创建这些代码的麻烦。它可以在我们编译源代码的时候自动为我们生成这些方法。Lombok的主要功能Lombok的主要功能是注解使用的核心功能我画了一张图来说明Lombok的使用。官网介绍了很多导入方式。可以参考官网install介绍mavenimport。本文只介绍mavenimport。pom文件直接导入lombok。官网最新版本是1.18.22org.projectlomboklombok1.18.22provideduse实例lombok的使用主要依赖注解,官网官网上的文档里面都有注解,这里就不一一列举了,就以@Data为例,代码也是来自官网@Data类上的@Data注解,会自动生成setter/getter,equals,canEqual对于class的所有属性,hashCode,toString方法,如果是final属性,则不会为属性生成setter方法。导入lombok.AccessLevel;导入lombok.Setter;导入lombok.Data;导入lombok.ToString;@DatapublicclassDataExample{privatefinalStringname;@Setter(AccessLevel.PACKAGE)privateintage;私人双倍分数;私有String[]标签;@ToString(includeFieldNames=true)@Data(staticConstructor="of")publicstaticclassExercise{privatefinalStringname;privatefinalTvalue;}}如果不使用Lombok,则实际如下:importjava.util.Arrays;publicclassDataExample{privatefinalStringname;私人年龄;私人双倍分数;私有String[]标签;publicDataExample(Stringname){this.name=name;}publicStringgetName(){returnthis.name;}voidsetAge(intage){this.age=age;}publicintgetAge(){returnthis.age;}publicvoidsetScore(doublescore){this.score=score;}publicdoublegetScore(){returnthis.score;}publicString[]getTags(){returnthis.tags;}publicvoidsetTags(字符串[]标签){this.tags=标签;}@OverridepublicStringtoString(){return"DataExample("+this.getName()+","+this.getAge()+","+this.getScore()+","+Arrays.deepToString(this.getTags())+")";}protectedbooleancanEqual(Objectother){returnotherinstanceofDataExample;}@Overridepublicbooleanequals(Objecto){if(o==this)returntrue;if(!(oinstanceofDataExample))returnfalse;DataExampleother=(DataExample)o;if(!other.canEqual((Object)this))returnfalse;if(this.getName()==null?other.getName()!=null:!this.getName().equals(other.getName()))returnfalse;if(this.getAge()!=other.getAge())返回false;if(Double.compare(this.getScore(),other.getScore())!=0)returnfalse;if(!Arrays.deepEquals(this.getTags(),other.getTags()))返回假;返回真;}@OverridepublicinthashCode(){finalintPRIME=59;intresult=1;finallongtemp1=Double.doubleToLongBits(this.getScore());result=(result*PRIME)+(this.getName()==null?43:this.getName().hashCode());result=(result*PRIME)+this.getAge();result=(result*PRIME)+(int)(temp1^(temp1>>>32));result=(result*PRIME)+Arrays.deepHashCode(this.getTags());返回结果;}publicstaticclassExercise{privatefinalStringname;privatefinalTvalue;privateExercise(Stringname,Tvalue){this.name=name;this.value=value;}publicstaticExerciseof(Stringname,Tvalue){returnnewExercise(name,value);}publicStringgetName(){returnthis.name;}publicTgetValue(){returnthis.value;}@OverridepublicStringtoString(){return"Exercise(name="+this.getName()+",value="+this.getValue()+")";}protectedbooleancanEqual(Objectother){returnotherinstanceofExercise;}@Overridepublicbooleanequals(Objecto){if(o==this)returntrue;如果(!(oinstanceofExercise))返回false;练习其他=(练习)o;如果(!other.canEqual((Object)this))返回false;如果(this.getName()==null?other.getValue()!=null:!this.getName().equals(other.getName()))返回false;如果(this.getValue()==null?other.getValue()!=null:!this.getValue().equals(other.getValue()))返回false;returntrue;}@OverridepublicinthashCode(){finalintPRIME=59;整数结果=1;结果=(result*PRIME)+(this.getName()==null?43:this.getName().hashCode());结果=(result*PRIME)+(this.getValue()==null?43:this.getValue().hashCode());returnresult;}}}其他可以参考官网地址为https://projectlombok.org/fea...,点击查看相关说明和使用示例。参考官网??:https://projectlombok.org/文档:https://projectlombok.org/fea。..github项目:https://github.com/projectlom...