lombok插件浅析

lombok插件浅析,第1张

在java开发中,定义实体类之后,需要写很多的get/set/toString/hashCode/equals等方法,麻烦的很,尤其是定义很多的实体类且每个实体类的变量又高达几十个时,写完get/set方法之后,简直要心潮澎湃,鲜血直喷。

为了简化代码,避免重复劳动的工作,下面谈谈lombok插件工具。

1、lombok使用
Lombok 是一款非常流行的代码简洁工具,利用它的注解特性,直接就可以省去高达几百行的get、set方法, *** 作非常方便。
@Data注解实例

@Data
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private String id;
    private String name;
    private String sex;
    private int age;
    private String password;
    // 不需要显式写get、set方法
}

对这个实体类进行编译之后,打开class文件,发现注解帮我们实现get/set等方法了。

public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;
    private String sex;
    private int age;
    private String password;

    public User() {
    }

    public String getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public String getSex() {
        return this.sex;
    }

    public int getAge() {
        return this.age;
    }

    public String getPassword() {
        return this.password;
    }

    public void setId(final String id) {
        this.id = id;
    }

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

    public void setSex(final String sex) {
        this.sex = sex;
    }

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

    public void setPassword(final String password) {
        this.password = password;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof User)) {
            return false;
        } else {
            User other = (User)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getAge() != other.getAge()) {
                return false;
            } else {
                label61: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id == null) {
                            break label61;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label61;
                    }

                    return false;
                }

                label54: {
                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name == null) {
                            break label54;
                        }
                    } else if (this$name.equals(other$name)) {
                        break label54;
                    }

                    return false;
                }

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

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

                return true;
            }
        }
    }

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

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        int result = result * 59 + this.getAge();
        Object $id = this.getId();
        result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $sex = this.getSex();
        result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
        Object $password = this.getPassword();
        result = result * 59 + ($password == null ? 43 : $password.hashCode());
        return result;
    }

    public String toString() {
        return "User(id=" + this.getId() + ", name=" + this.getName() + ", sex=" + this.getSex() + ", age=" + this.getAge() + ", password=" + this.getPassword() + ")";
    }
}

可见,使用Data注解可用大大减少代码量,使代码非常简洁,这也是很多开发者喜欢使用lombok的主要原因。

2、lombok插件安装
在idea开发工具中,可用直接在priferences/plugins里面搜索lombok,然后点击安装即可,如下:

然后在工程中引入lombok依赖

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>

最后在实体类中加上@Data等注解就可以了。

3、lombok原理
由于Java的官方版本没有提供这种快速生成方法的注解工具,类似Lombok这样的工具,其实都是使用了从Java 6和JSR 269的Annotation Processing技术中实现方法的注入。
简单的说,就是使用了 Java 非公开的 API,在 javac 编译代码时,通过强类型转换获取JavacAnnotationProcessor对象,再从JavacAnnotationProcessor的方法里面拿到抽象语法树(AST)做强制修改,注入get、set等方法。

4、lombok特点

lombok的优点如下:代码简洁、节省大量重复工作;
lombok的缺点如下:

  • 需要安装lombok插件:使用lombok插件来快速开发时,一起协同开发的同事也不得不安装lombok插件,不然代码编译报错;
  • 代码可调式性降低:节省了get/set等方法,但在调试时不知道该属性被那些类的那些方法调用了,若是原生方式,可用很方便的知道‘谁’调用了;
  • lombok注解踩坑点:使用@Data会重写hashCode()和equals()方法,如果是单个实体类,没有继承的话,你使用@Data不会产生问题。若继承了父类,@Data只会重写子类的hashCode()和equals()方法,不会把父类的属性加进去,这样就会导致,例如当你在使用HashMap的时候,用当前这个实体类作为key,可能会得到意想不到的结果。这时需要加上这个注解@EqualsAndHashCode(callSuper=true),子类的hashCode()和equals()方法会加入父类的属性。
  • 影响jdk升级:lombok的原理是使用了非官方支持的 API 接口,通过程序强制植入方式来修改类,实现get、set等方法的注入。随着jdk的升级,若未来jdk把这种后门堵死了,那lombok基本上就不能用了;

Lombok 作为一款非常流行的工具插件,肯定有它自身的优势所在,也是当前的使用趋势。个人建议,根据业务和工程场景考量是否引用,且在使用lombok插件之前,了解其特点和原理,避免踩坑的同时又最大化的简化代码及开发。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/2889341.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-14
下一篇 2022-09-14

发表评论

登录后才能评论

评论列表(0条)

保存