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

  

lombok在IntelliJ IDEA下的使用的更多相关文章

  1. 40.lombok在IntelliJ IDEA下的使用

    转自:https://www.cnblogs.com/yjmyzz/p/lombok-with-intellij-idea.html lombok是一款可以精减java代码.提升开发人员生产效率的辅助 ...

  2. IntelliJ IDEA 下的版本控制介绍

    不管是个人开发或是团队开发,版本控制都是可以很好地被使用的,目前我找不到任何开发者不使用版本控制的理由.而且对于 IDE 来讲,集成版本控制的本身就是它最大的亮点之一,很多开发者也是为此而使用它. 在 ...

  3. Intellij IDEA下导出Java工程的可运行JAR包

    Intellij IDEA下导出Java工程的可运行JAR包 昨天一直向导出一个Java工程的可运行JAR包,然后查阅网上的资料以及自己一遍一遍的尝试,均以失败告终.可以导出JAR包,但是导出的JAR ...

  4. IntelliJ IDEA下Git的配置与使用(命令行下)

    1. 安装Git并配置好Git 安装与配置参见Git与码云(Git@OSC)入门-如何在实验室和宿舍同步你的代码(1)中的2.在本机安装Git与3.1 配置git. 2. 创建远程仓库 在gitee. ...

  5. IntelliJ IDEA下的使用git

    1.git简介 git是目前流行的分布式版本管理系统.它拥有两套版本库,本地库和远程库,在不进行合并和删除之类的操作时这两套版本库互不影响.也因此其近乎所有的操作都是本地执行,所以在断网的情况下任然可 ...

  6. webstorm和intellij idea下如何自动编译sass和scss文件

    webstorm和intellij idea下如何自动编译sass和scss文件 https://segmentfault.com/a/1190000008996504 https://www.jia ...

  7. (转)IntelliJ IDEA下的使用git

    1.git简介 Git是目前流行的分布式版本管理系统.它拥有两套版本库,本地库和远程库,在不进行合并和删除之类的操作时这两套版本库互不影响.也因此其近乎所有的操作都是本地执行,所以在断网的情况下任然可 ...

  8. IntelliJ IDEA下SVN配置及使用

    一.在IDEA中使用SVN,首先需要下载安装 TortoiseSVN 插件. 打开 TortoiseSVN 下载地址,选择适合自己的系统类型下载. 接下来,进行安装即可.选择Modify,默认 com ...

  9. 解决Intellij Idea下修改jsp页面不自动更新

    解决Intellij Idea下修改jsp页面不自动更新 On frame deactivation:被设置成了Do nothing 解决办法:改为Update resources(更新资源)或者Up ...

随机推荐

  1. IDEA+weblogic部署运行项目

    前段时间再服务器上为了部署一个系统,一直存在各种问题,不过过程中倒是把weblogic的部署掌握地特别熟练,下面就一些主要步骤做个记录:1.自己用的是IDEA,所以打开项目之后需要设置src/main ...

  2. Spring MVC学习

    SpringMVC框架 转载请注明出处 目录 一:配置springMVC开发环境 1.1.配置文件的helloworld 1.2.基于注解的helloworld 二:@RequestMapping详解 ...

  3. 通过pycharm使用git[图文详解]

    前言 使用git+pycharm有一段时间了,算是稍有点心得,这边整理一下,可能有的方法不是最优,欢迎交流,可能还是习惯敲命令去使用git,不过其实pycharm已经帮忙做了很多了,我们可以不用记住那 ...

  4. 11 个很少人知道但很有用的 Linux 命令

    Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.Linux命令和它们的转换对于Linux用户.Shell脚本程序员和管理员来说是最有 ...

  5. c# 九九乘法表

    static void Main(string[] args) { ; i < ; i++) { ; s <= i; s++) { Console.Write(s + "*&qu ...

  6. c#使用Split分割换行符 \r\n

    c# 使用Split分割 换行符,方法如下(其余方法有空再添加):   string str = "aa" + "\r\n" + "bb"; ...

  7. C# 高效编程笔记2

    C# 高效编程笔记2 1.理解GetHashCode()的陷阱 (1)作用:作为基于散列集合定义键的散列值,如:HashSet<T>,Dictionary<K,V>容器等 (2 ...

  8. Yii 2.x 日志记录器-类图

  9. lsof命令

    学习资源https://linux.die.net/man/8/lsof lsof mean list open files 如果说linux中一切皆文件的话,那么lsof就是一盏照亮黑暗的文件系统的 ...

  10. 使用page object模式抓取几个主要城市的pm2.5并从小到大排序后写入txt文档

    #coding=utf-8from time import sleepimport unittestfrom selenium import webdriverfrom selenium.webdri ...