一款好用的Java插件 - Lombok

青儿
• 阅读 1540

作者:烧鸡太子爷

来源:恒生LIGHT云社区

前面介绍了一款java的maven插件叫maven helper,很多同学还挺感兴趣的,今天再接再厉,给大家再推荐一款还用的java插件叫lombok。先给大家讲讲Lombok的主要功能和如何使用,后面有时间再讲讲Lombok的原理

Lombok是什么

先看段官网个的介绍说明

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.

Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码的工具,简单来说,比如我们新建了一个类,然后在其中写了几个字段,然后通常情况下我们需要手动去建立getter和setter方法啊,构造函数啊之类的,lombok的作用就是为了省去我们手动创建这些代码的麻烦,它能够在我们编译源码的时候自动帮我们生成这些方法

Lombok的主要功能

Lombok主要功能是通过注解的方式,用到的核心的功能我整理画了一个图

一款好用的Java插件 - Lombok

Lombok的使用

官网说明

官网有介绍很多引入的方式,可以参考官网install介绍

一款好用的Java插件 - Lombok

maven引入

本文只介绍maven引入,pom文件中直接引入lombok,目前官网最新的版本是1.18.22

<dependencies>

    <dependency>

        <groupId>org.projectlombok</groupId>

        <artifactId>lombok</artifactId>

        <version>1.18.22</version>

        <scope>provided</scope>

    </dependency>

</dependencies>

使用示例

lombok使用过程中主要是靠注解起作用的,官网上的文档里面有所有的注解,这里不一一罗列,只拿@Data做举例,代码也是来自官网。

@Data

@Data注解在类上,会为类的所有属性自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。

import lombok.AccessLevel;

import lombok.Setter;

import lombok.Data;

import lombok.ToString;



@Data public class DataExample {

  private final String name;

  @Setter(AccessLevel.PACKAGE) private int age;

  private double score;

  private String[] tags;

  

  @ToString(includeFieldNames=true)

  @Data(staticConstructor="of")

  public static class Exercise<T> {

private final String name;

private final T value;

  }

}

如不使用Lombok,则实现如下:

import java.util.Arrays;



public class DataExample {

  private final String name;

  private int age;

  private double score;

  private String[] tags;

  

  public DataExample(String name) {

this.name = name;

  }

  

  public String getName() {

return this.name;

  }

  

  void setAge(int age) {

this.age = age;

  }

  

  public int getAge() {

return this.age;

  }

  

  public void setScore(double score) {

this.score = score;

  }

  

  public double getScore() {

return this.score;

  }

  

  public String[] getTags() {

return this.tags;

  }

  

  public void setTags(String[] tags) {

this.tags = tags;

  }

  

  @Override public String toString() {

return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";

  }

  

  protected boolean canEqual(Object other) {

return other instanceof DataExample;

  }

  

  @Override public boolean equals(Object o) {

if (o == this) return true;

if (!(o instanceof DataExample)) return false;

DataExample other = (DataExample) o;

if (!other.canEqual((Object)this)) return false;

if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;

if (this.getAge() != other.getAge()) return false;

if (Double.compare(this.getScore(), other.getScore()) != 0) return false;

if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;

return true;

  }

  

  @Override public int hashCode() {

final int PRIME = 59;

int result = 1;

final long temp1 = 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());

return result;

  }

  

  public static class Exercise<T> {

private final String name;

private final T value;



private Exercise(String name, T value) {

  this.name = name;

  this.value = value;

}



public static <T> Exercise<T> of(String name, T value) {

  return new Exercise<T>(name, value);

}



public String getName() {

  return this.name;

}



public T getValue() {

  return this.value;

}



@Override public String toString() {

  return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";

}



protected boolean canEqual(Object other) {

  return other instanceof Exercise;

}



@Override public boolean equals(Object o) {

  if (o == this) return true;

  if (!(o instanceof Exercise)) return false;

  Exercise<?> other = (Exercise<?>) o;

  if (!other.canEqual((Object)this)) return false;

  if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;

  if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;

  return true;

}



@Override public int hashCode() {

  final int PRIME = 59;

  int result = 1;

  result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());

  result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());

  return result;

}

  }

}

其他的可以参考官网的地址 https://projectlombok.org/fea...,点击进去就可以看到相关的说明和使用样例

一款好用的Java插件 - Lombok

参考

官网https://projectlombok.org/

文档https://projectlombok.org/fea...

github项目https://github.com/projectlom...

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Wesley13 Wesley13
4年前
Activiti 工作流入门指南
<divclass"htmledit\_views"id"content\_views"<h1<aname"t0"</a概览</h1<p如我们的介绍部分所述,Activiti目前分为两大类:</p<ul<li<p<ahref"https://activiti.gitbook.io/activiti7deve
Wesley13 Wesley13
4年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
4年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
4年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Stella981 Stella981
4年前
Dubbo爆出严重漏洞!可导致网站被控制、数据泄露!附解决方案
http://dy.163.com/v2/article/detail/F5FPIFRU0511Q1AF.html  !(http://dingyue.ws.126.net/2020/0216/125ec4c4p00q5rcrs0019d200ig009qg00ig009q.png)  来源:华为云  原文地址:https://w
Stella981 Stella981
4年前
Neo4j删除节点和关系、彻底删除节点标签名
<divclass"htmledit\_views"id"content\_views"<p<ahref"https://www.jianshu.com/p/59bd829de0de"rel"nofollow"datatoken"720f42e8792665773f66044d30a60222"https://www.jians