关于java:一款好用的Java插件-Lombok

40次阅读

共计 4154 个字符,预计需要花费 11 分钟才能阅读完成。

作者:烧鸡太子爷

起源:恒生 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 次要性能是通过注解的形式,用到的外围的性能我整顿画了一个图

Lombok 的应用

官网阐明

官网有介绍很多引入的形式,能够参考官网 install 介绍

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…,点击进去就能够看到相干的阐明和应用样例

参考

官网 :https://projectlombok.org/

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

github 我的项目 :https://github.com/projectlom…

正文完
 0