在写使用Java时,难免会有一些模板代码要写,不然get/set,toString, hashCode, close 资源,定义构造函数等等。代码会显得很冗余,很长。Lombok项目可以是我们摆脱这些东西,通过一系列的注解,Lombok可以帮我们自动生成这些函数。

Lombok 官网地址:https://projectlombok.org/

参考文档:https://projectlombok.org/features/index.html

1. 安装

到官网下载 lombok.jar,直接双击,按照提示进行操作,就可以在eclipse中安装成功。

如果使用maven时,则需要引入依赖:

    <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.4</version>
<scope>provided</scope>
</dependency>

如果需要用javac或者其他命令工具编译java类,则需要将 lombok.jar放入classpath.

2. 使用方法 (文档:https://projectlombok.org/features/index.html)

1> @Getter/@Setter, 注解在一个pojo类上,会在编译时,帮我们自动生成get/set函数。

2> @ToString 注解在类上,编译时,帮我们生成包括所有field的toString函数;

3> @EqualsAndHashCode,  编译时,帮我们生成equlas 和hashCode函数;

4> @Cleanup, 注解在一些资源对象的定义上,可以帮我们自动调用它们的close()函数;这个很有帮助;

5> @NoArgsContructor,@RequireArgsContructor, @AllArgsContructor,分别帮我们生成无参数构造函数,每一个非Null的field的构造函数,所有field参数的构造函数;

6> @Data,All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor! (等价于:@ToString, @EqualsAndHashCode, @Getter, @Setter, @RequiredArgsConstructor)

更多的注解,参见https://projectlombok.org/features/index.html

3. 例子

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Test {
private int id;
private String name;
private String password; public static void main(String[] args) {
Test test = new Test(1, "test", "password");
System.out.println(test);
System.out.println(test.getName());
}
}

结果:

Test(id=1, name=test, password=password)
test

通过@Data, @AllArgsConstructor,@NoArgsConstructor 三个注解自动 生成了 Test 的全field参数的构造函数,自动生成了 toString(), get/set函数等等。

再看一例:

    public static void main(String[] args) throws IOException{
@Cleanup
InputStream in = new FileInputStream("/home/a.txt");
@Cleanup
OutputStream out = new FileOutputStream("/home/b.txt");
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1)
break;
out.write(b, 0, r);
}
}

@Cleanup自动帮我们调用 close() 方法进行关闭资源。

You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. You do this by annotating any local variable declaration with the @Cleanup annotation like so:
@Cleanup InputStream in = new FileInputStream("some/file");
As a result, at the end of the scope you're in, in.close() is called. This call is guaranteed to run by way of a
try/finally construct
.

If the type of object you'd like to cleanup does not have a close() method, but some other no-argument method, you can
specify the name of this method like so:
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);
By default, the cleanup method is presumed to be close(). A cleanup method that takes 1 or more arguments cannot be called via
@Cleanup.

@Cleanup是通过 try/finally 实现的,如果资源的关闭方法不是默认的close(),那么也可以指定关闭方法的名称@Cleanup("closeMethod"), 但是关闭方法不能有参数,不然就无法使用 @Cleanup了。

更多的 参考 https://projectlombok.org/features/index.html

通过使用 Lombok,可以减少很多的 Java 代码的,减轻了心理负担。

使用 Lombok 简化项目中无谓的Java代码的更多相关文章

  1. VS Code项目中共享自定义的代码片段方案

    VS Code项目中共享自定义的代码片段方案 一.问题背景 项目中注释风格不统一,如何统一注释风格 一些第三方组件库名称太长,每次使用都需要找文档,然后复制粘贴 部分组件库有自己的Snippets插件 ...

  2. Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南

    Vue2/3 项目中的 ESLint + Prettier 代码检测格式化风格指南 因为平时都是使用 VSCode ESLint + Prettier 检测格式化不规范代码,但是随着接手的项目越来越多 ...

  3. gradle项目中如何支持java与scala混合使用?

    之前写过一篇maven项目中java与scala如何混用,今天来看看gradle项目中如何达到同样的效果: 一.目录结构 基本上跟maven一样,tips:这一堆目录结构不用死记,后面会讲如何用gra ...

  4. 在PHP项目中使用Standford Moss代码查重系统

    Standford Moss 系统是斯坦福大学大名鼎鼎的代码查重系统,它可以查出哪些同学提交的代码是抄袭别人的,从而将提交结果拒之门外.它对一切希望使用该系统的人都是开放的,那么在PHP的项目中如何使 ...

  5. 03_Android项目中读写文本文件的代码

    编写一下Android界面的项目 使用默认的Android清单文件 <?xml version="1.0" encoding="utf-8"?> & ...

  6. 如何在Eclipse中Debug调试Java代码

    背景 有的时候你想debug调试Java的源代码,就想试图在Java源代码中设置断点,在Eclipse中常常会出现Unable to insert breakpoint Absent Line Num ...

  7. JDK中ThreadDump诊断Java代码中的线程死锁问题

    多线程的死锁..死锁不是死了而是线程互相等待... 在项目中可能就是在几十万行的代码中存在一个死锁的问题,如何发现这个问题并且解决这个问题. JavaJDK为我们提供了一个诊断工具叫做ThreadDu ...

  8. unity3D项目中如何避免硬代码(C#)

    平时做项目,代码中是不允许出现硬代码的,一般我们是怎么处理的呢? 那么硬代码又是什么呢? 我们俗称的硬代码:eg:   label.text = "欢迎来到梦幻岛";  这样我们俗 ...

  9. [置顶] oracle 数据库表中转换成java代码

    --数据库中字段java代码 select col.TABLE_NAME,replace(initcap(col.TABLE_NAME),'_', '')   , 'private '||decode ...

随机推荐

  1. How does flyway sort version numbers?

    https://stackoverflow.com/questions/19984397/how-does-flyway-sort-version-numbers In one word: numer ...

  2. C++实现可变参数列表

    // 接收数量不定的实参.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #includ ...

  3. 一个故意消耗内存的java程序MemoryEater

    公司提供的测试服务器是vmware的,号称给我6G, 物理内存事实上是按需分配的. 等到真正拿到6G物理内存,黄花菜都凉了. 看到下面的文章,觉得故意用java程序侵占6G内存, 然后把侵占到内存的释 ...

  4. OOP的感悟

    不要认为你关心的东西就是对象的全部或对象的核心,相对于对象的成员家族而言,它仅仅是其中的一个‘很小的成员而已’

  5. PHP中exit()与die()的区别

    PHP手册:die()Equivalent to exit(). 说明:die()和exit()都是中止脚本执行函数:其实exit和die这两个名字指向的是同一个函数,die()是exit()函数的别 ...

  6. jquery.validate验证表单

    添加引用 <script src="/${appName}/commons/js/validate/jquery.validate.min.js"></scrip ...

  7. UVA-10305 Ordering Tasks (拓扑排序)

    题目大意:给出n个点,m条关系,按关系的从小到大排序. 题目分析:拓扑排序的模板题,套模板. kahn算法: 伪代码: Kahn算法: 摘一段维基百科上关于Kahn算法的伪码描述: L← Empty ...

  8. js搜索算法——二分搜索

    二分搜索算法就是折半查找,是一种效率较高的查找方法.前提条件是要查找的数组是有序的.算法的实现还是相对简单的: function binarySearch(arr,item){ var min = 0 ...

  9. 网络编程I/O函数介绍

    read和write #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); ssize_t write(in ...

  10. 2018.11.23 Cypress BLE module test

    CYx63BPA BLE module IQC test guide Test Jig setting:1.  Connect  USB1 and USB2 with computer serial ...