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. jvm系列(三):java GC算法 垃圾收集器

    GC算法 垃圾收集器 概述 垃圾收集 Garbage Collection 通常被称为“GC”,它诞生于1960年 MIT 的 Lisp 语言,经过半个多世纪,目前已经十分成熟了. jvm 中,程序计 ...

  2. 修改版: 小伙,多线程(GCD)看我就够了,骗你没好处!

    多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能.具有这种能力的系 ...

  3. 你真的会玩SQL吗?你所不知道的 数据聚合

    你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...

  4. OWIN与Katana详解

    前言 我胡汉三又回来了,!!!!, 最近忙成狗,实在没空写博文,实在对不起自己,博客园上逛了逛发现 我大微软还是很给力的 asp.net core 1.0 .net core 1.0 即将发布,虽然. ...

  5. [JavaEE笔记]Cookie

    引言 由于 Http 是一种无状态的协议,服务器单从网络连接上无从知道客户身份. 会话跟踪是 Web 程序中常用的技术,用来跟踪用户的整个会话.常用会话跟踪技术是 Cookie 与 Session. ...

  6. winform程序一启动抛出异常--调用目标发生异常

    在本机测试没有问题,可一到别的电脑上就抛出异常,这是最麻烦的事,一时间还找不出什么原因,本机上还无法重现. 现在好了,终于找到一个完美解决的办法,在Program.cs类中加入如下代码 static ...

  7. 分享api接口验证模块

    一.前言 权限验证在开发中是经常遇到的,通常也是封装好的模块,如果我们是使用者,通常指需要一个标记特性或者配置一下就可以完成,但实际里面还是有许多东西值得我们去探究.有时候我们也会用一些开源的权限验证 ...

  8. Angular通过XHR加载模板而限制使用file://(解决方案)

    编写angular项目时,遇到此困难: angular.js:12011 XMLHttpRequest cannot load file:///E:/angular/imooc/chapter2/bo ...

  9. 前端渲染利器——JsRender入门

    JsRender不少前端人员应该都用过,它是一个比较强大的模板,不牵涉太多技术依赖,使用起来非常舒服.我本人在前端开发中使用React之前,都是用的它了(实际上我感觉React没有JsViewes好用 ...

  10. java Io流更新文件内容

    package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...