简单使用LomBok

LomBok

LomBok 是什么

官网

Lombok 是一种 Java™ 实用工具,可用来帮助开发人员消除 Java 中的冗长代码,尤其是对于简单的 Java 对象 (POJO),它通过注解实现这一目的。

常用注解

1、@Getter/@Setter

放在字段上

为该字段生成 Getter/Setter 方法

AccessLevel.PUBLIC指定该 Getter/Setter 方法为 public(默认为 public)

public class User {
    @Getter(AccessLevel.PUBLIC)
    private Integer id;
    private String name;
}


放在类上

为该类所有字段生成 Getter/Setter 方法

@Getter
public class User {
    private Integer id;
    private String name;
}


放在类上时,对指定字段取消该字段的 Getter/Setter 方法

AccessLevel.NONE

@Getter
public class User {
    private Integer id;
    @Getter(AccessLevel.NONE) //单独指定该字段不生成 Getter 方法
    private String name;
}

注意

@Getter/@Setter 注解只对该类中的成员变量生效

static 不产生效果

final 仅生成 Getter 方法(final 类型字段不可更改

2、@ToString

该注解只能放在类上

为该类生成 toString 方法

@ToString
public class User {
    private Integer id;
    private String name;
}


排除对某些字段的输出

exclude = {"XX","XX"}

@ToString(exclude = {"name"})
public class User {
    private Integer id;
    private String name;
}

必须包含哪些字段(与 exclude 相反),只输出该字段

of = {"XX",XX}

@ToString(of = {"id"})
public class User {
    private Integer id;
    private String name;
}

3、@EqualsAndHashCode

用于对象之间的判等和比较

该注解会生成三个方法

3.1、equals()

该方法通过一系列判断逻辑来判断两个对象是否相等

public boolean equals(final Object o) {
    //判断两个对象是否为同一对象
    if (o == this) {
        return true;
    //判断两个对象是否为同一类的实例
    } else if (!(o instanceof User)) {
        return false;
    } else {
        User other = (User)o;
        //判断两个对象是否为同一类的实例(canEqual())
        if (!other.canEqual(this)) {
            return false;
        //判断各属性值是否相等
        } else {
            Object this$id = this.id;
            Object other$id = other.id;
            if (this$id == null) {
                if (other$id != null) {
                    return false;
                }
            } else if (!this$id.equals(other$id)) {
                return false;
            }

            Object this$name = this.name;
            Object other$name = other.name;
            if (this$name == null) {
                if (other$name != null) {
                    return false;
                }
            } else if (!this$name.equals(other$name)) {
                return false;
            }

            return true;
        }
    }
}
3.2、canEqual()

判断两个对象是否为同一类的实例

protected boolean canEqual(final Object other) {
    return other instanceof User;
}
3.3、hashCode()

通过各属性的值组成该对象的 hashCode

public int hashCode() {
    int PRIME = true;
    int result = 1;
    Object $id = this.id;
    int result = result * 59 + ($id == null ? 43 : $id.hashCode());
    Object $name = this.name;
    result = result * 59 + ($name == null ? 43 : $name.hashCode());
    return result;
}

默认情况下,equal() 方法的判等规则为判断两个对象的全部字段值是否相等

排除指定字段的判等:exclude = {"XX","XX"}

指定哪些字段进行判等:of = {"XX","XX"}

@EqualsAndHashCode(exclude = {"name"})
public class User {
    private Integer id;
    private String name;
}


@EqualsAndHashCode(of = {"name"})
public class User {
    private Integer id;
    private String name;
}

4、@NonNull

不为空 :使用在类中的方法/构造器中(参数判非空),或者使用在字段上(该字段值不为空)

public class User {
    @NonNul
    private Integer id;
    private String name;

    //s不为空
    public void test(@NonNull String s) {
        System.out.println(s);
    }
    public static void main(String[] args) {
        User user = new User();
        user.test(null);
    }
}

该注解生成了如下代码

public void test(@NonNull String s) {
    if (s == null) {
        throw new NullPointerException("s is marked non-null but is null");
    } else {
        System.out.println(s);
    }
}

5、构造函数

5.1、@NoArgsConstructor

为该类创建无参构造函数

5.2、@RequiredArgsConstructor

生成带有指定参数的构造函数

默认生成的构造函数的参数为 标识了 @NonNull 的字段以及 final 修饰的字段(final 修饰字段不能有值)

//该注解生成的有参构造函数
public User(final int id2, @NonNull final Integer id) {
    if (id == null) {
        throw new NullPointerException("id is marked non-null but is null");
    } else {
        this.id2 = id2;
        this.id = id;
    }
}

5.3、@AllArgsConstructor

生成带有全部参数的构造函数

6、@Data

该注解集成了

Getter/Setter

ToString

EqualsAndHashCode

RequiredArgsConstructor

一键式开启以上注解

7、@Builder

Builder 使用建造者模式。简单来说,就是一步步创建一个对象,它对用户屏蔽了里面构建的细节,但却可以精细地控制对象的构造过程。

@Builder
public class User {
    final int id2 = 10;
    private Integer id;
    private String name;
}

开启 Builder 后,可以像如下一样创建对象

User user = User.builder().id(1).name("builder").build();

8、@Log

在类上开启 Log 注解,即可进行日志相关操作

自动生成以下代码

private static final Logger log = Logger.getLogger(User.class.getName());

9、val

类似于 JavaScriptvar

val 在定义变量的时候不指定该变量的类型,在该变量赋完值后,该变量类型才确定下来

10、@Cleanup

自动资源管理:没有麻烦和安全地调用close()方法。

使用 Lombok
import lombok.Cleanup;
import java.io.*;

public class CleanupExample {
  public static void main(String[] args) throws IOException {
    @Cleanup InputStream in = new FileInputStream(args[0]);
    @Cleanup OutputStream out = new FileOutputStream(args[1]);
    byte[] b = new byte[10000];
    while (true) {
      int r = in.read(b);
      if (r == -1) break;
      out.write(b, 0, r);
    }
  }
}
不使用 Lombok
import java.io.*;

public class CleanupExample {
  public static void main(String[] args) throws IOException {
    InputStream in = new FileInputStream(args[0]);
    try {
      OutputStream out = new FileOutputStream(args[1]);
      try {
        byte[] b = new byte[10000];
        while (true) {
          int r = in.read(b);
          if (r == -1) break;
          out.write(b, 0, r);
        }
      } finally {
        if (out != null) {
          out.close();
        }
      }
    } finally {
      if (in != null) {
        in.close();
      }
    }
  }
}

注意:

如果要清理的对象类型没有close()方法,但是有其他一些无参数方法,则可以指定此方法的名称,如下所示:
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);

默认情况下,清除方法为close()

@Cleanup无法调用带有1个或多个参数的清理方法。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值