lombok在IntelliJ IDEA下的使用

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里可以直接看到生成的方法:(下图中打红圈的都是自动生成的)

原文:https://www.cnblogs.com/yjmyzz/p/lombok-with-intellij-idea.html

IntelliJ IDEA中配置Lombok主要是为了在编译时能够正确处理Lombok生成的代码。以下是详细步骤: 1. **检查是否有Lombok插件**: - 打开IntelliJ IDEA,点击菜单栏的 "File" > "Settings" (Windows/Linux) 或 "Preferences" (Mac)。 2. **搜索并安装Lombok插件**: - 在搜索框里输入 "Lombok",从插件市场中找到 "Intellij IDEA Lombok Plugin",点击 "Install" 安装。 3. **启用Lombok插件**: - 插件安装完成后,回到插件管理界面,确认Lombok插件已启用。如果有提示需要重启IDE,可以选择重启。 4. **配置编译器**: - 进入 "Build, Execution, Deployment" 菜单下的 "Compiler" 配置。 - 在 "Annotation Processors" 标签下,勾选 "Enable annotation processing",然后在下面的 "Annotation Processor Path" 中指定Lombok的jar路径。如果你之前通过Maven或Gradle添加了Lombok依赖,这个路径通常是对应的maven或gradle目录。 5. **生成源码**: - 有些IDEA版本默认会将Lombok代码生成为临时源文件,这可能会导致混淆。你可以关闭这个选项,在 "Advanced Settings" 里的 "Code style" -> "Java" -> "Other" -> "Generated code" 选择 "Generate code as source". 6. **重启IDEA**: - 最后,重启IntelliJ IDEA,让其加载新配置。 配置完成后,你应该就能在IntelliJ IDEA中正常使用Lombok注解了,无需手动编译也能享受到Lombok带来的便捷。如果还有疑问,可以直接在IDE的内置帮助文档中查找相关信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值