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里可以直接看到生成的方法:(下图中打红圈的都是自动生成的)
lombok在IntelliJ IDEA下的使用的更多相关文章
- 40.lombok在IntelliJ IDEA下的使用
转自:https://www.cnblogs.com/yjmyzz/p/lombok-with-intellij-idea.html lombok是一款可以精减java代码.提升开发人员生产效率的辅助 ...
- IntelliJ IDEA 下的版本控制介绍
不管是个人开发或是团队开发,版本控制都是可以很好地被使用的,目前我找不到任何开发者不使用版本控制的理由.而且对于 IDE 来讲,集成版本控制的本身就是它最大的亮点之一,很多开发者也是为此而使用它. 在 ...
- Intellij IDEA下导出Java工程的可运行JAR包
Intellij IDEA下导出Java工程的可运行JAR包 昨天一直向导出一个Java工程的可运行JAR包,然后查阅网上的资料以及自己一遍一遍的尝试,均以失败告终.可以导出JAR包,但是导出的JAR ...
- IntelliJ IDEA下Git的配置与使用(命令行下)
1. 安装Git并配置好Git 安装与配置参见Git与码云(Git@OSC)入门-如何在实验室和宿舍同步你的代码(1)中的2.在本机安装Git与3.1 配置git. 2. 创建远程仓库 在gitee. ...
- IntelliJ IDEA下的使用git
1.git简介 git是目前流行的分布式版本管理系统.它拥有两套版本库,本地库和远程库,在不进行合并和删除之类的操作时这两套版本库互不影响.也因此其近乎所有的操作都是本地执行,所以在断网的情况下任然可 ...
- webstorm和intellij idea下如何自动编译sass和scss文件
webstorm和intellij idea下如何自动编译sass和scss文件 https://segmentfault.com/a/1190000008996504 https://www.jia ...
- (转)IntelliJ IDEA下的使用git
1.git简介 Git是目前流行的分布式版本管理系统.它拥有两套版本库,本地库和远程库,在不进行合并和删除之类的操作时这两套版本库互不影响.也因此其近乎所有的操作都是本地执行,所以在断网的情况下任然可 ...
- IntelliJ IDEA下SVN配置及使用
一.在IDEA中使用SVN,首先需要下载安装 TortoiseSVN 插件. 打开 TortoiseSVN 下载地址,选择适合自己的系统类型下载. 接下来,进行安装即可.选择Modify,默认 com ...
- 解决Intellij Idea下修改jsp页面不自动更新
解决Intellij Idea下修改jsp页面不自动更新 On frame deactivation:被设置成了Do nothing 解决办法:改为Update resources(更新资源)或者Up ...
随机推荐
- Anliven - To-Do List
2016 - December Name Type Start Deadline Status Output Comments Last Review SQL必知必会(第4版) Book 2016-1 ...
- Windows下构建ASP.NET Core+Code First+Docker
背景介绍 本文将会示范如何在Windows系统下基于ASP.NET Core构建跨平台服务,并通过Docker容器运行发布. 首先说一下为什么选择这一套组合: 我本人和我们Code4Thought团队 ...
- 『.NET Core CLI工具文档』(十)dotnet-build
说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-build 翻译:dotnet-build 名称 dotnet-build -- 生成项目和所有的依赖 概 ...
- Java Swing interview
http://www.careerride.com/Swing-AWT-Interview-Questions.aspx Swing interview questions and answers ...
- jQuery中iframe的操作
今天遇到一个问题:怎样实现点击一个按钮,在当前的页面上新增加一个小窗口,展示一个图片信息? 如图: 点击之前: 单击之后: 分析:要使新增的小窗口不影响父页面,我们这里采用iframe的框架的技术. ...
- php静态缓存简单制作
制作缓存的目的是为了让我们的页面运行更加快速,减少读取数据库内容的次数,给用户更好的体验,为此我们可以使自己的程序做一下缓存,并且设置一个缓存过期的时间,来保证与数据库的一致,当然并不是所有的程序都适 ...
- Android 内存泄漏的一些情况。
最近在维护代码,发现一个自定义View(这个View是在一个AsyncTask的工作线程doInBackground中新建的,在UI线程onPostExecute中添加进window中的)经常会泄漏内 ...
- Eos开发——构造查询条件
1.ajax 方式 var data = { orgid :orgid,year:year ,month: month,type:type,sortField:'sellEmpname' ,sortO ...
- 分享一下Java写的坦克大战游戏,相信很多人小时候玩过
写在前面 程序是五六年前读书的时候写的,里面会有一些不规范的,我已经将代码传到github上了,有时间会优化修改. 程序运行机制 定义了一个JPanel,然后每隔一小段时间重绘一遍. 重绘的内容如下: ...
- Spring 4 异常处理
异常与HTTP状态码的映射(@ResponseStatus) Spring默认会将自身抛出的异常自动映射到合适的状态码,如下是一些示例: 举个例子,当后端抛出如下异常(TypeMismatchExce ...