lombok让你提高代码整洁度的神器附教程及原理分析

时间:2022-05-07
本文章向大家介绍lombok让你提高代码整洁度的神器附教程及原理分析,主要内容包括使用方法、Function and principle analysis 功能和原理分析、source code源码地址、Function intro功能介绍和使用教程、project home page 项目主页、讨论组:、Common Functions常用方法、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

在Java编程的过程中,我们在Code Entity的时候通常使用 IDE的generator来生成 get set toSting equals hashcode Constructor 等方法,有了lombok以后就不会了,它会在编译的过程中,分析AST抽象语法树的方式,把这些方法插入到编译以后的代码当中,这样做的好处可以降低代码量,让代码变得更容易读

程序源码

@Data
public class User {
    int id;
    String name;
}

编译后的代码

public class User {
    int id;
    String name;

    public User() {
    }

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

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

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

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

    public boolean equals(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.getId() != other.getId()) {
                return false;
            } else {
                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                return true;
            }
        }
    }

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

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

    public String toString() {
        return "User(id=" + this.getId() + ", name=" + this.getName() + ")";
    }
}

使用方法

用法也非常简单 Maven 项目加上项目依赖就可以使用了,最新地址:https://projectlombok.org/setup/maven

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.18</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

加载依赖之后IDE需要对应的plugin Eclipse 下载 lombok.jar 运行java -jar lombok.jar 即可启动图形界面安装 https://projectlombok.org/setup/eclipse

Intellij就直接在plugins里面搜索资源安装重启即可 https://projectlombok.org/setup/intellij

其他更多IDE请浏览官方教程 https://projectlombok.org/setup/overview

Function and principle analysis 功能和原理分析

https://www.ibm.com/developerworks/cn/java/j-lombok/ http://notatube.blogspot.fr/2010/11/project-lombok-trick-explained.html

source code源码地址

https://github.com/rzwitserloot/lombok

Function intro功能介绍和使用教程

http://codepub.cn/2015/07/30/Lombok-development-guidelines/ http://i.woblog.cn/2016/06/19/use-lombok/ http://blog.csdn.net/ghsau/article/details/52334762 https://dzone.com/articles/lombok-reduces-your

project home page 项目主页

https://projectlombok.org/

讨论组:

https://groups.google.com/forum/#!topic/project-lombok

Common Functions常用方法

@Data

@Data
All together now: A shortcut for 
@ToString, 
@EqualsAndHashCode,
@Getter on all fields, and 
@Setter on all non-final fields, and 
@RequiredArgsConstructor!

@Log 帮你注入Loger 支持多种 日志组件

@CommonsLog
Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@JBossLog
Creates private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);

Constructor

@NoArgsConstructor @RequiredArgsConstructor @AllArgsConstructor

@NonNull

@Synchronized