Lombok用法简介

2020-02-19  本文已影响0人  夜余玩家

Lombok 简介

Lombok项目是一个Java库,它会自动插入您的编辑器和构建工具中,从而为您的Java增光添彩。
永远不要再编写另一个getter或equals方法,一个带有注释的类将具有功能全面的生成器,自动执行日志记录变量等等。

Lombok官方原版介绍如下:

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

相关网址

Lombok Idea插件安装

下载Intellij Idea Lombok插件

https://plugins.jetbrains.com/plugin/6317-lombok/versions

Lombok依赖配置

https://projectlombok.org/setup/maven

Adding lombok to your pom file
To include lombok as a 'provided' dependency, add it to your <dependencies> block like so:

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

JDK9+ with module-info.java
The configuration of the compiler plug-in should contain the following:

<annotationProcessorPaths>
    <path>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.12</version>
    </path>
</annotationProcessorPaths>

常用注解

Lombok features

val

Finally! Hassle-free final local variables.

var

Mutably! Hassle-free local variables.

@NonNull

or: How I learned to stop worrying and love the NullPointerException.

@Cleanup

Automatic resource management: Call your close() methods safely with no hassle.

@Getter/@Setter

Never write public int getFoo() {return foo;} again.

@ToString

No need to start a debugger to see your fields: Just let lombok generate a toString for you!

@EqualsAndHashCode

Equality made easy: Generates hashCode and equals implementations from the fields of your object..

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

Constructors made to order: Generates constructors that take no arguments, one argument per final / non-nullfield, or one argument for every field.

@Data

All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!

@Value

Immutable classes made very easy.

@Builder

... and Bob's your uncle: No-hassle fancy-pants APIs for object creation!

@SneakyThrows

To boldly throw checked exceptions where no one has thrown them before!

@Synchronized

synchronized done right: Don't expose your locks.

@With

Immutable 'setters' - methods that create a clone but with one changed field.

@Getter(lazy=true)

Laziness is a virtue!

@Log

Captain's Log, stardate 24435.7: "What was that line again?"

experimental

Head to the lab: The new stuff we're working on.

原理

三者原理都是基于“Annotation Processing“,每个工具都在他们的JAR文件的“META-INF/services”中定义了一个javax.annotation.processing.Processor,最终完成了“从一个简洁的模板类生成包含更多方法(Getter,toString,hasCode,equals等)的生成类”的目的。

尽管Lombok、AutoValue和Immutables都使用了javac的annotation processing,但是Lombok使用注释处理的方式与AutoValue和Immutables使用注释处理的方式不同。

AutoValue和Immutables在更传统的意义上使用注释处理,并从源代码生成源代码。由AutoValue和Immutables生成的类源代码的名称与模板类不同,实际上扩展了模板类,它有自己的名称,包含所有生成的方法和字段,这避免了与模板类的任何名称冲突,并且使在同一个IDE项目中混合模板类源代码和生成的类源代码变得相当容易,因为它们实际上是不同的类。

其他

https://github.com/rzwitserloot/lombok/blob/master/doc/git-workflow.txt

参考

上一篇下一篇

猜你喜欢

热点阅读