lombok在IntelliJ IDEA下的使用

简介: lombok是一款可以精减java代码、提升开发人员生产效率的辅助工具,利用注解在编译期自动生成setter/getter/toString()/constructor之类的代码。代码越少,意味着出bug的可能性越低。

lombok是一款可以精减java代码、提升开发人员生产效率的辅助工具,利用注解在编译期自动生成setter/getter/toString()/constructor之类的代码。代码越少,意味着出bug的可能性越低。

官网地址:https://projectlombok.org/ 首页有一段几分钟的演示视频,看完就明白是怎么回事了。

先来二段对比代码:

这是用lombok后的java代码:

import lombok.*;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayInputStream;
import java.io.*;
import java.util.ArrayList;

@Data
@Slf4j
@AllArgsConstructor
public class Something {

    private String name;
    private final String country;
    private final Object lockObj = new Object();

    public void sayHello(@NonNull String target) {
        String content = String.format("hello,%s", target);
        System.out.println(content);
        log.info(content);
    }

    public void addBalabala() {
        val list = new ArrayList<String>();
        list.add("haha");
        System.out.println(list.size());
    }

    @SneakyThrows(IOException.class)
    public void closeBalabala() {
        @Cleanup InputStream is = new ByteArrayInputStream("hello world".getBytes());
        System.out.println(is.available());
    }

    @Synchronized("lockObj")
    public void lockMethod() {
        System.out.println("test lock method");
    }
}

等效于下面这段java代码:

import java.beans.ConstructorProperties;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Something {
    private static final Logger log = LoggerFactory.getLogger(Something.class);
    private String name;
    private final String country;
    private final Object lockObj = new Object();

    public void sayHello(@NonNull String target) {
        if(target == null) {
            throw new NullPointerException("target");
        } else {
            String content = String.format("hello,%s", new Object[]{target});
            System.out.println(content);
            log.info(content);
        }
    }

    public void addBalabala() {
        ArrayList list = new ArrayList();
        list.add("haha");
        System.out.println(list.size());
    }

    public void closeBalabala() {
        try {
            ByteArrayInputStream $ex = new ByteArrayInputStream("hello world".getBytes());

            try {
                System.out.println($ex.available());
            } finally {
                if(Collections.singletonList($ex).get(0) != null) {
                    $ex.close();
                }

            }

        } catch (IOException var6) {
            throw var6;
        }
    }

    public void lockMethod() {
        Object var1 = this.lockObj;
        synchronized(this.lockObj) {
            System.out.println("test lock method");
        }
    }

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

    public String getCountry() {
        return this.country;
    }

    public Object getLockObj() {
        return this.lockObj;
    }

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

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof Something)) {
            return false;
        } else {
            Something other = (Something)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                label47: {
                    String this$name = this.getName();
                    String other$name = other.getName();
                    if(this$name == null) {
                        if(other$name == null) {
                            break label47;
                        }
                    } else if(this$name.equals(other$name)) {
                        break label47;
                    }

                    return false;
                }

                String this$country = this.getCountry();
                String other$country = other.getCountry();
                if(this$country == null) {
                    if(other$country != null) {
                        return false;
                    }
                } else if(!this$country.equals(other$country)) {
                    return false;
                }

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

                return true;
            }
        }
    }

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

    public int hashCode() {
        boolean PRIME = true;
        byte result = 1;
        String $name = this.getName();
        int result1 = result * 59 + ($name == null?0:$name.hashCode());
        String $country = this.getCountry();
        result1 = result1 * 59 + ($country == null?0:$country.hashCode());
        Object $lockObj = this.getLockObj();
        result1 = result1 * 59 + ($lockObj == null?0:$lockObj.hashCode());
        return result1;
    }

    public String toString() {
        return "Something(name=" + this.getName() + ", country=" + this.getCountry() + ", lockObj=" + this.getLockObj() + ")";
    }

    @ConstructorProperties({"name", "country"})
    public Something(String name, String country) {
        this.name = name;
        this.country = country;
    }
}

大概减少了2/3的代码,各种注解的详细用法,请参考:https://projectlombok.org/features/index.html

IDEA下使用时,可以通过插件的形式安装,插件下载地址:https://github.com/mplushnikov/lombok-intellij-plugin/releases 

然后

Plugins -> Install plugin from disk... 选择下载的zip包安装,重启idea即可。

另外,还有一个关键设置:

为了让设置生效,建议再重启一次idea,然后就可以开心的编码了,可以ide里可以直接看到生成的方法:(下图中打红圈的都是自动生成的)

  

目录
相关文章
|
6月前
|
Java 应用服务中间件 Maven
在IntelliJ IDEA中如何配置使用Maven以创建Tomcat环境
所以,别担心这些工具看起来有些吓人,实际上这些都是为了帮助你更好的完成工作的工具,就像超市里的各种烹饪工具一样,尽管它们看起来可能很复杂,但只要你学会用,它们会为你烹饪出一道道美妙的食物。这就是学习新技能的乐趣,让我们一起享受这个过程,攀登知识的高峰!
427 27
|
8月前
|
IDE 程序员 开发工具
只用正版!教你5个方法,白嫖JetBrains家族的所有产品,包含:IntelliJ IDEA、PyCharm、WebStorm、CLion、Rider
程序员晚枫分享了5种官方认证的免费使用JetBrains家族产品的方法,包括内容创作者计划、开源项目支持、教育许可证、用户组支持和开发者认可计划。这些方法帮助个人开发者与小型团队合法获取强大开发工具,如IntelliJ IDEA、PyCharm等,降低开发成本,提升效率。同时提醒大家遵守使用规范,尊重知识产权。
1495 13
|
Java 编译器 Maven
使用intellij idea搭建SSM架构的maven项目 详细
使用intellij idea搭建SSM架构的maven项目 详细
251 4
|
IDE Oracle Java
day4:JDK、IntelliJ IDEA的安装和环境变量配置
【7月更文挑战第4天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
589 0
|
Java Windows
IDEA不使用lombok,如何快速生成get和set方法
【11月更文挑战第10天】在 IntelliJ IDEA 中生成 `get` 和 `set` 方法有多种方式:通过菜单操作、使用快捷键或自定义模板。菜单操作包括选择“Code”菜单中的“Generate...”,快捷键为“Alt + Insert”。自定义模板可在“File”-&gt;“Settings”-&gt;“Editor”-&gt;“Code Style”-&gt;“Java”中设置。批量生成时,可多选变量一次性生成。
2672 2
|
网络协议 安全 Linux
在IntelliJ IDEA中使用固定公网地址远程SSH连接服务器环境进行开发
在IntelliJ IDEA中使用固定公网地址远程SSH连接服务器环境进行开发
521 2
|
Linux 开发工具 Windows
在WSL2中安装IntelliJ IDEA开发工具
在WSL2中安装IntelliJ IDEA开发工具
1389 2
|
IDE Java Scala
IntelliJ IDEA 2023.3 最新变化2
IntelliJ IDEA 2023.3 最新变化
264 1
IntelliJ IDEA lombok log 报红
IntelliJ IDEA lombok log 报红
171 0
|
IDE Java 项目管理
Java入门——Intellij IDEA简介、使用IDEA开发程序、IDEA常用快捷键、IDEA其他操作
Java入门——Intellij IDEA简介、使用IDEA开发程序、IDEA常用快捷键、IDEA其他操作
389 3
下一篇
oss云网关配置