前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >lombok让你提高代码整洁度的神器附教程及原理分析

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

作者头像
Albert陈凯
发布2018-04-03 16:24:03
6880
发布2018-04-03 16:24:03
举报
文章被收录于专栏:Albert陈凯Albert陈凯

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

程序源码

代码语言:javascript
复制
@Data
public class User {
    int id;
    String name;
}

编译后的代码

代码语言:javascript
复制
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

代码语言:javascript
复制
<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
代码语言:javascript
复制
@Data
All together now: A shortcut for 
@ToString, 
@EqualsAndHashCode,
@Getter on all fields, and 
@Setter on all non-final fields, and 
@RequiredArgsConstructor!
@Log 帮你注入Loger 支持多种 日志组件
代码语言:javascript
复制
@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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.12.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用方法
  • Function and principle analysis 功能和原理分析
  • source code源码地址
  • Function intro功能介绍和使用教程
  • project home page 项目主页
  • 讨论组:
  • Common Functions常用方法
    • @Data
      • @Log 帮你注入Loger 支持多种 日志组件
        • Constructor
          • @NonNull
          • @Synchronized
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档