1. 首页
  2. IT资讯

效率提高 10 倍!一份不可多得的 Lombok 学习指南

“u003Cimg src=”http:u002Fu002Fp9.pstatp.comu002Flargeu002Fpgc-imageu002FRh1NRi0C5wEPsR” img_width=”800″ img_height=”340″ alt=”效率提高 10 倍!一份不可多得的 Lombok 学习指南” inline=”0″u003Eu003Cpu003E作者 | semlinkeru003Cu002Fpu003Eu003Cpu003E链接 | segmentfaultu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E.comu003Cu002Fiu003Eu002Fau002F11900000208u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003E64u003Cu002Fiu003E572u003Cu002Fpu003Eu003Cpu003Eu003Cstrong toutiao-origin=”span” class=”highlight-text”u003E一、Lombok 简介u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003ELombok 是一款 Java 开发插件,使得 Java 开发者可以通过其定义的一些注解来消除业务工程中冗长和繁琐的代码,尤其对于简单的 Java 模型对象(POJO)。在开发环境中使用 Lombok 插件后,Java 开发人员可以节省出重复构建,诸如 hashCode 和 equals 这样的方法以及各种业务对象模型的 accessor 和 toString 等方法的大量时间。对于这些方法,Lombok 能够在编译源代码期间自动帮我们生成这些方法,但并不会像反射那样降低程序的性能。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h3″u003E二、Lombok 安装u003Cu002Fh1u003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E2.1 构建工具u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrongu003EGradleu003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E在 u003Ccodeu003Ebuild.gradleu003Cu002Fcodeu003E文件中添加 lombok 依赖:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Edependencies {u003Cbru003EcompileOnly ‘org.projectlombok:lombok:1.18.10’u003Cbru003EannotationProcessor ‘org.projectlombok:lombok:1.18.10’u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003EMavenu003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E在 Maven 项目的 u003Ccodeu003Epom.xmlu003Cu002Fcodeu003E文件中添加 lombok 依赖:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E<dependency>u003Cbru003E<groupId>org.projectlombok<u002FgroupId>u003Cbru003E<artifactId>lombok<u002FartifactId>u003Cbru003E<version>1.18.10<u002Fversion>u003Cbru003E<scope>provided<u002Fscope>u003Cbru003E<u002Fdependency>u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrongu003EAntu003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E假设在 u003Ccodeu003Elibu003Cu002Fcodeu003E目录中已经存在u003Ccodeu003Elombok.jaru003Cu002Fcodeu003E,然后设置 javac 任务:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E<javac srcdir=”src” destdir=”build” source=”1.8″>u003Cbru003Eu003Cstrong toutiao-origin=”span” class=”highlight-text”u003E<classpath location=”libu002Flombok.jar” u002F>u003Cu002Fstrongu003Eu003Cbru003Eu003Cstrong toutiao-origin=”span” class=”highlight-text”u003E<u002Fjavac>u003Cu002Fstrongu003Eu003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E2.2 IDEu003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E由于 Lombok 仅在编译阶段生成代码,所以使用 Lombok 注解的源代码,在 IDE 中会被高亮显示错误,针对这个问题可以通过安装 IDE 对应的插件来解决。这里不详细展开,具体的安装方式可以参考 Setting up Lombok with Eclipse and IntelliJ 这篇文章。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h3″u003E三、Lombok 详解u003Cu002Fh1u003Eu003Cpu003Eu003Cstrongu003E注意:以下示例所使用的 Lombok 版本是 1.18.10u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003E3.1 @Getter and @Setter 注解u003Cu002Fh2u003Eu003Cpu003E你可以使用 u003Ccodeu003E@Getteru003Cu002Fcodeu003E或u003Ccodeu003E@Setteru003Cu002Fcodeu003E注释任何类或字段,Lombok 会自动生成默认的 getteru002Fsetter 方法。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrongu003E@Getter 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003E@Target({ElementType.FIELD, ElementType.TYPE})u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface Getter {u003Cbru003Eu003Cbru003Elombok.AccessLevel value default lombok.AccessLevel.PUBLIC;u003Cbru003EAnyAnnotation onMethod default {};u003Cbru003Eu003Cbru003Eboolean lazy default false;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrongu003E@Setter 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003E@Target({ElementType.FIELD, ElementType.TYPE})u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface Setter {u003Cbru003Eu003Cbru003Elombok.AccessLevel value default lombok.AccessLevel.PUBLIC;u003Cbru003EAnyAnnotation onMethod default {};u003Cbru003EAnyAnnotation onParam default {};u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E使用示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003E@Getteru003Cbru003E@Setteru003Cbru003Epublic class GetterAndSetterDemo {u003Cbru003EString firstName;u003Cbru003EString lastName;u003Cbru003ELocalDate dateOfBirth;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class GetterAndSetterDemo {u003Cbru003EString firstName;u003Cbru003EString lastName;u003Cbru003ELocalDate dateOfBirth;u003Cbru003Eu003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EGetterAndSetterDemou003Cu002Fstrongu003E {u003Cbru003E}u003Cbru003Eu003Cbru003Eu003Cbru003Epublic String getFirstName {u003Cbru003Ereturn this.firstName;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EsetFirstNameu003Cu002Fstrongu003E(String firstName) {u003Cbru003Ethis.firstName = firstName;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003ELazy Getteru003Cu002Fh2u003Eu003Cpu003Eu003Ccodeu003E@Getteru003Cu002Fcodeu003E注解支持一个 lazy 属性,该属性默认为 false。当设置为 true 时,会启用延迟初始化,即当首次调用 getter 方法时才进行初始化。u003Cu002Fpu003Eu003Cpu003ETips:u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E关注u003Cu002Fiu003Eu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E微信u003Cu002Fiu003E公众号:Java后端,每日获取博文的推送。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class LazyGetterDemo {u003Cbru003Epublic static voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003Emainu003Cu002Fstrongu003E(String[] args) {u003Cbru003ELazyGetterDemo m = new LazyGetterDemo;u003Cbru003ESystem.out.println(“Main instance is created”);u003Cbru003Em.getLazy;u003Cbru003E}u003Cbru003Eu003Cbru003E@Getteru003Cbru003Eprivate final String notLazy = createValue(“not lazy”);u003Cbru003Eu003Cbru003E@Getter(lazy = true)u003Cbru003Eprivate final String lazy = createValue(“lazy”);u003Cbru003Eu003Cbru003Eprivate String createValue(String name) {u003Cbru003ESystem.out.println(“createValue(” + name + “)”);u003Cbru003Ereturn ;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class LazyGetterDemo {u003Cbru003Eprivate final String notLazy = this.createValue(“not lazy”);u003Cbru003Eprivate final AtomicReference<Object> lazy = new AtomicReference;u003Cbru003Eu003Cbru003Eu003Cbru003Epublic String getNotLazy {u003Cbru003Ereturn this.notLazy;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic String getLazy {u003Cbru003EObject value = this.lazy.get;u003Cbru003Eif (value == ) {u003Cbru003Esynchronized(this.lazy) {u003Cbru003Evalue = this.lazy.get;u003Cbru003Eif (value == ) {u003Cbru003EString actualValue = this.createValue(“lazy”);u003Cbru003Evalue = actualValue == ? this.lazy : actualValue;u003Cbru003Ethis.lazy.set(value);u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cbru003Eu003Cbru003Ereturn (String)((String)(value == this.lazy ? : value));u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E通过以上代码可知,调用 getLazy 方法时,若发现 value 为 ,则会在同步代码块中执行初始化操作。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003E3.2 Constructor Annotationsu003Cu002Fh2u003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrongu003E@NoArgsConstructor 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E使用 u003Ccodeu003E@NoArgsConstructoru003Cu002Fcodeu003E注解可以为指定类,生成默认的构造函数,u003Ccodeu003E@NoArgsConstructoru003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target(ElementType.TYPE)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface NoArgsConstructor {u003Cbru003Eu003Cbru003EString staticName default “”;u003Cbru003EAnyAnnotation onConstructor default {};u003Cbru003Eu003Cbru003EAccessLevel access default lombok.AccessLevel.PUBLIC;u003Cbru003Eu003Cbru003Ebooleanu003Cstrong toutiao-origin=”span” class=”highlight-text”u003Eforceu003Cu002Fstrongu003E default false;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003E@NoArgsConstructor(staticName = “getInstance”)u003Cbru003Epublic class NoArgsConstructorDemo {u003Cbru003Eprivate long id;u003Cbru003Eprivate String name;u003Cbru003Eprivate int age;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class NoArgsConstructorDemo {u003Cbru003Eprivate long id;u003Cbru003Eprivate String name;u003Cbru003Eprivate int age;u003Cbru003Eu003Cbru003Eprivateu003Cstrong toutiao-origin=”span” class=”highlight-text”u003ENoArgsConstructorDemou003Cu002Fstrongu003E {u003Cbru003E}u003Cbru003Eu003Cbru003Epublic static NoArgsConstructorDemo getInstance {u003Cbru003Ereturn new NoArgsConstructorDemo;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrongu003E@AllArgsConstructor 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E使用 u003Ccodeu003E@AllArgsConstructoru003Cu002Fcodeu003E注解可以为指定类,生成包含所有成员的构造函数,u003Ccodeu003E@AllArgsConstructoru003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target(ElementType.TYPE)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface AllArgsConstructor {u003Cbru003Eu003Cbru003EString staticName default “”;u003Cbru003EAnyAnnotation onConstructor default {};u003Cbru003Eu003Cbru003EAccessLevel access default lombok.AccessLevel.PUBLIC;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003E@AllArgsConstructoru003Cbru003Epublic class AllArgsConstructorDemo {u003Cbru003Eprivate long id;u003Cbru003Eprivate String name;u003Cbru003Eprivate int age;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class AllArgsConstructorDemo {u003Cbru003Eprivate long id;u003Cbru003Eprivate String name;u003Cbru003Eprivate int age;u003Cbru003Eu003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EAllArgsConstructorDemou003Cu002Fstrongu003E(long id, String name, int age) {u003Cbru003Ethis.id = id;u003Cbru003Ethis.name = name;u003Cbru003Ethis.age = age;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrongu003E@RequiredArgsConstructorDemo 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E使用 u003Ccodeu003E@RequiredArgsConstructoru003Cu002Fcodeu003E注解可以为指定类必需初始化的成员变量,如 final 成员变量,生成对应的构造函数,u003Ccodeu003E@RequiredArgsConstructoru003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target(ElementType.TYPE)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface RequiredArgsConstructor {u003Cbru003Eu003Cbru003EString staticName default “”;u003Cbru003EAnyAnnotation onConstructor default {};u003Cbru003Eu003Cbru003EAccessLevel access default lombok.AccessLevel.PUBLIC;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003E@RequiredArgsConstructoru003Cbru003Epublic class RequiredArgsConstructorDemo {u003Cbru003Eprivate final long id;u003Cbru003Eprivate String name;u003Cbru003Eprivate int age;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class RequiredArgsConstructorDemo {u003Cbru003Eprivate final long id;u003Cbru003Eprivate String name;u003Cbru003Eprivate int age;u003Cbru003Eu003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003ERequiredArgsConstructorDemou003Cu002Fstrongu003E(long id) {u003Cbru003Ethis.id = id;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E3.3 @EqualsAndHashCode 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E使用 u003Ccodeu003E@EqualsAndHashCodeu003Cu002Fcodeu003E注解可以为指定类生成 equals 和 hashCode 方法,u003Ccodeu003E@EqualsAndHashCodeu003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target(ElementType.TYPE)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface EqualsAndHashCode {u003Cbru003Eu003Cbru003EString exclude default {};u003Cbru003Eu003Cbru003EString of default {};u003Cbru003Eu003Cbru003Eboolean callSuper default false;u003Cbru003Eu003Cbru003Eboolean doNotUseGetters default false;u003Cbru003Eu003Cbru003EAnyAnnotation onParam default {};u003Cbru003Eu003Cbru003E@Deprecatedu003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003E@Target({})u003Cbru003E@interface AnyAnnotation {}u003Cbru003Eu003Cbru003E@Target(ElementType.FIELD)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface Exclude {}u003Cbru003Eu003Cbru003E@Target({ElementType.FIELD, ElementType.METHOD})u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface Include {u003Cbru003EString replaces default “”;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003E@EqualsAndHashCodeu003Cbru003Epublic class EqualsAndHashCodeDemo {u003Cbru003EString firstName;u003Cbru003EString lastName;u003Cbru003ELocalDate dateOfBirth;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class EqualsAndHashCodeDemo {u003Cbru003EString firstName;u003Cbru003EString lastName;u003Cbru003ELocalDate dateOfBirth;u003Cbru003Eu003Cbru003Epublic EqualsAndHashCodeDemo {u003Cbru003E}u003Cbru003Eu003Cbru003Epublic boolean equals(Object o) {u003Cbru003Eif (o == this) {u003Cbru003Ereturn true;u003Cbru003E} else if (!(o instanceof EqualsAndHashCodeDemo)) {u003Cbru003Ereturn false;u003Cbru003E} else {u003Cbru003EEqualsAndHashCodeDemo other = (EqualsAndHashCodeDemo)o;u003Cbru003Eif (!other.canEqual(this)) {u003Cbru003Ereturn false;u003Cbru003E} else {u003Cbru003Eu003Cbru003E}u003Cbru003E}u003Cbru003Eu003Cbru003Epublic int hashCode {u003Cbru003Eint PRIME = true;u003Cbru003Eint result = 1;u003Cbru003EObject $firstName = this.firstName;u003Cbru003Eint result = result * 59 + ($firstName == ? 43 : $firstName.hashCode);u003Cbru003EObject $lastName = this.lastName;u003Cbru003Eresult = result * 59 + ($lastName == ? 43 : $lastName.hashCode);u003Cbru003EObject $dateOfBirth = this.dateOfBirth;u003Cbru003Eresult = result * 59 + ($dateOfBirth == ? 43 : $dateOfBirth.hashCode);u003Cbru003Ereturn result;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E3.4 @ToString 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E使用 u003Ccodeu003E@ToStringu003Cu002Fcodeu003E注解可以为指定类生成 toString 方法,u003Ccodeu003E@ToStringu003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target(ElementType.TYPE)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface ToString {u003Cbru003Eu003Cbru003Ebooleanu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EincludeFieldNamesu003Cu002Fstrongu003E default true;u003Cbru003Eu003Cbru003EString exclude default {};u003Cbru003Eu003Cbru003EString of default {};u003Cbru003Eu003Cbru003Eu003Cbru003Ebooleanu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EcallSuperu003Cu002Fstrongu003E default false;u003Cbru003Eu003Cbru003Ebooleanu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EdoNotUseGettersu003Cu002Fstrongu003E default false;u003Cbru003Eu003Cbru003Ebooleanu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EonlyExplicitlyIncludedu003Cu002Fstrongu003E default false;u003Cbru003Eu003Cbru003E@Target(ElementType.FIELD)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface Exclude {}u003Cbru003Eu003Cbru003E@Target({ElementType.FIELD, ElementType.METHOD})u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface Include {u003Cbru003Eint rank default 0;u003Cbru003EString name default “”;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003E@ToString(exclude = {“dateOfBirth”})u003Cbru003Epublic class ToStringDemo {u003Cbru003EString firstName;u003Cbru003EString lastName;u003Cbru003ELocalDate dateOfBirth;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class ToStringDemo {u003Cbru003EString firstName;u003Cbru003EString lastName;u003Cbru003ELocalDate dateOfBirth;u003Cbru003Eu003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EToStringDemou003Cu002Fstrongu003E {u003Cbru003E}u003Cbru003Eu003Cbru003Epublic String toString {u003Cbru003Ereturn “ToStringDemo(firstName=” + this.firstName + “, lastName=” +u003Cbru003Ethis.lastName + “)”;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E3.5 @Data 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003Eu003Ccodeu003E@Datau003Cu002Fcodeu003E注解与同时使用以下的注解的效果是一样的:u003Cu002Fpu003Eu003Culu003Eu003Cliu003Eu003Cpu003Eu003Cstrongu003E@ToStringu003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cu002Fliu003Eu003Cliu003Eu003Cpu003Eu003Cstrongu003E@Getteru003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cu002Fliu003Eu003Cliu003Eu003Cpu003Eu003Cstrongu003E@Setteru003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cu002Fliu003Eu003Cliu003Eu003Cpu003Eu003Cstrongu003E@RequiredArgsConstructoru003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cu002Fliu003Eu003Cliu003Eu003Cpu003Eu003Cstrongu003E@EqualsAndHashCodeu003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cu002Fliu003Eu003Cu002Fulu003Eu003Cpu003Eu003Ccodeu003E@Datau003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target(ElementType.TYPE)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface Data {u003Cbru003EString staticConstructor default “”;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003E@Datau003Cbru003Epublic class DataDemo {u003Cbru003Eprivate Long id;u003Cbru003Eprivate String summary;u003Cbru003Eprivate String description;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class DataDemo {u003Cbru003Eprivate Long id;u003Cbru003Eprivate String summary;u003Cbru003Eprivate String description;u003Cbru003Eu003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EDataDemou003Cu002Fstrongu003E {u003Cbru003E}u003Cbru003Eu003Cbru003Eu003Cbru003Epublic Long getId {u003Cbru003Ereturn this.id;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EsetIdu003Cu002Fstrongu003E(Long id) {u003Cbru003Ethis.id = id;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic booleanu003Cstrong toutiao-origin=”span” class=”highlight-text”u003Eequalsu003Cu002Fstrongu003E(Object o) {u003Cbru003Eif (o == this) {u003Cbru003Ereturn true;u003Cbru003E} else if (!(o instanceof DataDemo)) {u003Cbru003Ereturn false;u003Cbru003E} else {u003Cbru003EDataDemo other = (DataDemo)o;u003Cbru003Eif (!other.canEqual(this)) {u003Cbru003Ereturn false;u003Cbru003E} else {u003Cbru003Eu003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cbru003Eu003Cbru003Eprotected booleanu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EcanEqualu003Cu002Fstrongu003E(Object other) {u003Cbru003Ereturn other instanceof DataDemo;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic intu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EhashCodeu003Cu002Fstrongu003E {u003Cbru003Eint PRIME = true;u003Cbru003Eint result = 1;u003Cbru003EObject $id = this.getId;u003Cbru003Eint result = result * 59 + ($id == ? 43 : $id.hashCode);u003Cbru003EObject $summary = this.getSummary;u003Cbru003Eresult = result * 59 + ($summary == ? 43 : $summary.hashCode);u003Cbru003EObject $description = this.getDescription;u003Cbru003Eresult = result * 59 + ($description == ? 43 : $description.hashCode);u003Cbru003Ereturn result;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic String toString {u003Cbru003Ereturn “DataDemo(id=” + this.getId + “, summary=” + this.getSummary + “, description=” + this.getDescription + “)”;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E3.6 @Log 注解u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003E若你将 u003Ccodeu003E@Logu003Cu002Fcodeu003E的变体放在类上(适用于你所使用的日志记录系统的任何一种);之后,你将拥有一个静态的 final log 字段,然后你就可以使用该字段来输出日志。u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Logu003Cbru003Eprivate static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName);u003Cbru003Eu003Cbru003E@Log4ju003Cbru003Eprivate static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);u003Cbru003Eu003Cbru003E@Log4j2u003Cbru003Eprivate static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);u003Cbru003Eu003Cbru003E@Slf4ju003Cbru003Eprivate static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);u003Cbru003Eu003Cbru003E@XSlf4ju003Cbru003Eprivate static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);u003Cbru003Eu003Cbru003E@CommonsLogu003Cbru003Eprivate static final org.apacheu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E.comu003Cu002Fiu003Emons.logging.Log log = org.apacheu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E.comu003Cu002Fiu003Emons.logging.LogFactory.getLog(LogExample.class);u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E3.7 @Synchronized 注解u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@Synchronizedu003Cu002Fcodeu003E是同步方法修饰符的更安全的变体。与u003Ccodeu003Esynchronizedu003Cu002Fcodeu003E一样,该注解只能应用在静态和实例方法上。它的操作类似于u003Ccodeu003Esynchronizedu003Cu002Fcodeu003E关键字,但是它锁定在不同的对象上。u003Ccodeu003Esynchronizedu003Cu002Fcodeu003E关键字应用在实例方法时,锁定的是 this 对象,而应用在静态方法上锁定的是类对象。对于 @Synchronized 注解声明的方法来说,它锁定的是u003Ccodeu003E$LOCKu003Cu002Fcodeu003E或u003Ccodeu003E$locku003Cu002Fcodeu003E。u003Ccodeu003E@Synchronizedu003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target(ElementType.METHOD)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface Synchronized {u003Cbru003Eu003Cbru003EString value default “”;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class SynchronizedDemo {u003Cbru003Eprivate final Object readLock = new Object;u003Cbru003Eu003Cbru003E@Synchronizedu003Cbru003Epublic static voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003Ehellou003Cu002Fstrongu003E {u003Cbru003ESystem.out.println(“world”);u003Cbru003E}u003Cbru003Eu003Cbru003E@Synchronizedu003Cbru003Epublic intu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EanswerToLifeu003Cu002Fstrongu003E {u003Cbru003Ereturn 42;u003Cbru003E}u003Cbru003Eu003Cbru003E@Synchronized(“readLock”)u003Cbru003Epublic voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003Efoou003Cu002Fstrongu003E {u003Cbru003ESystem.out.println(“bar”);u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class SynchronizedDemo {u003Cbru003Eprivate static final Object $LOCK = new Object[0];u003Cbru003Eprivate final Object $lock = new Object[0];u003Cbru003Eprivate final Object readLock = new Object;u003Cbru003Eu003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003ESynchronizedDemou003Cu002Fstrongu003E {u003Cbru003E}u003Cbru003Eu003Cbru003Epublic static voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003Ehellou003Cu002Fstrongu003E {u003Cbru003Esynchronized($LOCK) {u003Cbru003ESystem.out.println(“world”);u003Cbru003E}u003Cbru003E}u003Cbru003Eu003Cbru003Epublic intu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EanswerToLifeu003Cu002Fstrongu003E {u003Cbru003Esynchronized(this.$lock) {u003Cbru003Ereturn 42;u003Cbru003E}u003Cbru003E}u003Cbru003Eu003Cbru003Epublic voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003Efoou003Cu002Fstrongu003E {u003Cbru003Esynchronized(this.readLock) {u003Cbru003ESystem.out.println(“bar”);u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E3.8 @Builder 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E使用 u003Ccodeu003E@Builderu003Cu002Fcodeu003E注解可以为指定类实现建造者模式,该注解可以放在类、构造函数或方法上。u003Ccodeu003E@Builderu003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target({TYPE, METHOD, CONSTRUCTOR})u003Cbru003E@Retention(SOURCE)u003Cbru003Epublic @interface Builder {u003Cbru003E@Target(FIELD)u003Cbru003E@Retention(SOURCE)u003Cbru003Epublic @interface Default {}u003Cbru003Eu003Cbru003Eu003Cbru003EString builderMethodName default “builder”;u003Cbru003Eu003Cbru003EString buildMethodName default “build”;u003Cbru003Eu003Cbru003EString builderClassName default “”;u003Cbru003Eu003Cbru003Eboolean toBuilder default false;u003Cbru003Eu003Cbru003EAccessLevel access default lombok.AccessLevel.PUBLIC;u003Cbru003Eu003Cbru003E@Target({FIELD, PARAMETER})u003Cbru003E@Retention(SOURCE)u003Cbru003Epublic @interface ObtainVia {u003Cbru003EString field default “”;u003Cbru003EString method default “”;u003Cbru003Eboolean isStatic default false;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003E@Builderu003Cbru003Epublic class BuilderDemo {u003Cbru003Eprivate final String firstname;u003Cbru003Eprivate final String lastname;u003Cbru003Eprivate final String email;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class BuilderDemo {u003Cbru003Eprivate final String firstname;u003Cbru003Eprivate final String lastname;u003Cbru003Eprivate final String email;u003Cbru003Eu003Cbru003EBuilderDemo(String firstname, String lastname, String email) {u003Cbru003Ethis.firstname = firstname;u003Cbru003Ethis.lastname = lastname;u003Cbru003Ethis.email = email;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic static BuilderDemo.BuilderDemoBuilder builder {u003Cbru003Ereturn new BuilderDemo.BuilderDemoBuilder;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic static class BuilderDemoBuilder {u003Cbru003Eprivate String firstname;u003Cbru003Eprivate String lastname;u003Cbru003Eprivate String email;u003Cbru003Eu003Cbru003EBuilderDemoBuilder {u003Cbru003E}u003Cbru003Eu003Cbru003Epublic BuilderDemo.BuilderDemoBuilder firstname(String firstname) {u003Cbru003Ethis.firstname = firstname;u003Cbru003Ereturn this;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic BuilderDemo.BuilderDemoBuilder lastname(String lastname) {u003Cbru003Ethis.lastname = lastname;u003Cbru003Ereturn this;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic BuilderDemo.BuilderDemoBuilder email(String email) {u003Cbru003Ethis.email = email;u003Cbru003Ereturn this;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic BuilderDemo build {u003Cbru003Ereturn new BuilderDemo(this.firstname, this.lastname, this.email);u003Cbru003E}u003Cbru003Eu003Cbru003Epublic String toString {u003Cbru003Ereturn “BuilderDemo.BuilderDemoBuilder(firstname=” + this.firstname + “, lastname=” + this.lastname + “, email=” + this.email + “)”;u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E3.9 @SneakyThrows 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003Eu003Ccodeu003E@SneakyThrowsu003Cu002Fcodeu003E注解用于自动抛出已检查的异常,而无需在方法中使用 throw 语句显式抛出。u003Ccodeu003E@SneakyThrowsu003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface SneakyThrows {u003Cbru003Eu003Cbru003EClass<? extends Throwable> value default java.lang.Throwable.class;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class SneakyThrowsDemo {u003Cbru003E@SneakyThrowsu003Cbru003E@Overrideu003Cbru003Eprotected Object clone {u003Cbru003Ereturn super.clone;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class SneakyThrowsDemo {u003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003ESneakyThrowsDemou003Cu002Fstrongu003E {u003Cbru003E}u003Cbru003Eu003Cbru003Eprotected Object clone {u003Cbru003Etry {u003Cbru003Ereturn super.clone;u003Cbru003E} catch (Throwable var2) {u003Cbru003Ethrow var2;u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E3.10 @Non 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E你可以在方法或构造函数的参数上使用 u003Ccodeu003E@Nonu003Cu002Fcodeu003E注解,它将会为你自动生成非空校验语句。u003Ccodeu003E@Nonu003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE})u003Cbru003E@Retention(RetentionPolicy.CLASS)u003Cbru003E@Documentedu003Cbru003Epublic @interface Non {u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class NonDemo {u003Cbru003E@Getteru003Cbru003E@Setteru003Cbru003E@Nonu003Cbru003Eprivate String name;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class NonDemo {u003Cbru003E@Nonu003Cbru003Eprivate String name;u003Cbru003Eu003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003ENonDemou003Cu002Fstrongu003E {u003Cbru003E}u003Cbru003Eu003Cbru003E@Nonu003Cbru003Epublic String getName {u003Cbru003Ereturn this.name;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EsetNameu003Cu002Fstrongu003E(@Non String name) {u003Cbru003Eif (name == ) {u003Cbru003Ethrow new PointerException(“name is marked non- but is “);u003Cbru003E} else {u003Cbru003Ethis.name = name;u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cstrongu003E3.11 @Clean 注解u003Cu002Fstrongu003Eu003Cu002Fpu003Eu003Cpu003Eu003Ccodeu003E@Cleanu003Cu002Fcodeu003E注解用于自动管理资源,用在局部变量之前,在当前变量范围内即将执行完毕退出之前会自动清理资源,自动生成u003Ccodeu003Etry-finallyu003Cu002Fcodeu003E这样的代码来关闭流。u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target(ElementType.LOCAL_VARIABLE)u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface Cleanup {u003Cbru003Eu003Cbru003EString value default “close”;u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E示例u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class CleanupDemo {u003Cbru003Epublic static void main(String[] args) throws IOException {u003Cbru003E@Cleanup InputStream in = new FileInputStream(args[0]);u003Cbru003E@Cleanup OutputStream out = new FileOutputStream(args[1]);u003Cbru003Ebyte b = new byte[10000];u003Cbru003Ewhile (true) {u003Cbru003Eint r = in.read(b);u003Cbru003Eif (r == -1) break;u003Cbru003Eout.write(b, 0, r);u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epackage com.semlinker.lombok;u003Cbru003Eu003Cbru003Epublic class CleanupDemo {u003Cbru003Epublic CleanupDemo {u003Cbru003E}u003Cbru003Eu003Cbru003Epublic static void main(String[] args) throws IOException {u003Cbru003EFileInputStream in = new FileInputStream(args[0]);u003Cbru003Eu003Cbru003Etry {u003Cbru003EFileOutputStream out = new FileOutputStream(args[1]);u003Cbru003Eu003Cbru003Etry {u003Cbru003Ebyte b = new byte[10000];u003Cbru003Eu003Cbru003Ewhile(true) {u003Cbru003Eint r = in.read(b);u003Cbru003Eif (r == -1) {u003Cbru003Ereturn;u003Cbru003E}u003Cbru003Eu003Cbru003Eout.write(b, 0, r);u003Cbru003E}u003Cbru003E} finally {u003Cbru003Eif (Collections.singletonList(out).get(0) != ) {u003Cbru003Eout.close;u003Cbru003E}u003Cbru003Eu003Cbru003E}u003Cbru003E} finally {u003Cbru003Eif (Collections.singletonList(in).get(0) != ) {u003Cbru003Ein.close;u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E3.11 @With 注解u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003E在类的字段上应用 u003Ccodeu003E@Withu003Cu002Fcodeu003E注解之后,将会自动生成一个u003Ccodeu003EwithFieldName(newValue)u003Cu002Fcodeu003E的方法,该方法会基于 newValue 调用相应构造函数,创建一个当前类对应的实例。u003Ccodeu003E@Withu003Cu002Fcodeu003E注解的定义如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E@Target({ElementType.FIELD, ElementType.TYPE})u003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003Epublic @interface With {u003Cbru003EAccessLevel value default AccessLevel.PUBLIC;u003Cbru003Eu003Cbru003EWith.AnyAnnotation onMethod default {};u003Cbru003Eu003Cbru003EWith.AnyAnnotation onParam default {};u003Cbru003Eu003Cbru003E@Deprecatedu003Cbru003E@Retention(RetentionPolicy.SOURCE)u003Cbru003E@Target({})u003Cbru003Epublic @interface AnyAnnotation {u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h5″u003E示例u003Cu002Fh2u003Eu003Cpreu003Eu003Ccodeu003Epublic class WithDemo {u003Cbru003E@With(AccessLevel.PROTECTED)u003Cbru003E@Nonu003Cbru003Eprivate final String name;u003Cbru003E@Withu003Cbru003Eprivate final int age;u003Cbru003Eu003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EWithDemou003Cu002Fstrongu003E(String name, int age) {u003Cbru003Eif (name == ) throw new PointerException;u003Cbru003Ethis.name = name;u003Cbru003Ethis.age = age;u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码经过 Lombok 编译后,会生成如下代码:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epublic class WithDemo {u003Cbru003E@Nonu003Cbru003Eprivate final String name;u003Cbru003Eprivate final int age;u003Cbru003Eu003Cbru003Epublicu003Cstrong toutiao-origin=”span” class=”highlight-text”u003EWithDemou003Cu002Fstrongu003E(String name, int age) {u003Cbru003Eif (name == ) {u003Cbru003Ethrow new PointerException;u003Cbru003E} else {u003Cbru003Ethis.name = name;u003Cbru003Ethis.age = age;u003Cbru003E}u003Cbru003E}u003Cbru003Eu003Cbru003Eprotected WithDemo withName(@Non String name) {u003Cbru003Eif (name == ) {u003Cbru003Ethrow new PointerException(“name is marked non- but is “);u003Cbru003E} else {u003Cbru003Ereturn this.name == name ? this : new WithDemo(name, this.age);u003Cbru003E}u003Cbru003E}u003Cbru003Eu003Cbru003Epublic WithDemo withAge(int age) {u003Cbru003Ereturn this.age == age ? this : new WithDemo(this.name, age);u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch2 toutiao-origin=”h4″u003Eu003Cstrongu003E3.12 其它特性u003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Ch2 toutiao-origin=”h5″u003Eu003Cstrongu003Evalu003Cu002Fstrongu003Eu003Cu002Fh2u003Eu003Cpu003Eval 用在局部变量前面,相当于将变量声明为 final,此外 Lombok 在编译时还会自动进行类型推断。val 的使用示例:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epublic class ValExample {u003Cbru003Epublic String example {u003Cbru003Eval example = new ArrayList<String>;u003Cbru003Eexample.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(“Hello, World!”);u003Cbru003Eval foo = example.get(0);u003Cbru003Ereturn foo.toLowerCase;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003Eexample2u003Cu002Fstrongu003E {u003Cbru003Eval map = new HashMap<Integer, String>;u003Cbru003Emap.put(0, “zero”);u003Cbru003Emap.put(5, “five”);u003Cbru003Efor (val entry : map.entrySet) {u003Cbru003ESystem.out.printf(“%d: %s\n”, entry.getKey, entry.getValue);u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E以上代码等价于:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003Epublic class ValExample {u003Cbru003Epublic String example {u003Cbru003Efinal ArrayList<String> example = new ArrayList<String>;u003Cbru003Eexample.au003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003Eddu003Cu002Fiu003E(“Hello, World!”);u003Cbru003Efinal String foo = example.get(0);u003Cbru003Ereturn foo.toLowerCase;u003Cbru003E}u003Cbru003Eu003Cbru003Epublic voidu003Cstrong toutiao-origin=”span” class=”highlight-text”u003Eexample2u003Cu002Fstrongu003E {u003Cbru003Efinal HashMap<Integer, String> map = new HashMap<Integer, String>;u003Cbru003Emap.put(0, “zero”);u003Cbru003Emap.put(5, “five”);u003Cbru003Efor (final Map.Entry<Integer, String> entry : map.entrySet) {u003Cbru003ESystem.out.printf(“%d: %s\n”, entry.getKey, entry.getValue);u003Cbru003E}u003Cbru003E}u003Cbru003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E至此功能强大的 Lombok 工具就介绍完了。若你对于它的实现原理感兴趣的话,建议阅读猿码道大佬 十分钟搞懂Lombok使用与原理 这篇文章:u003Cu002Fpu003Eu003Cpu003Ehttps:u002Fu002Fjuejin.imu002Fpostu002F5a6eceb8f265da3e467555feu003Cu002Fpu003Eu003Cpu003E作者 :semlinkeru003Cu002Fpu003Eu003Cpu003E链接:https:u002Fu002Fsegmentfaultu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E.comu003Cu002Fiu003Eu002Fau002F11900000208u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-2″u003E64u003Cu002Fiu003E572u003Cu002Fpu003Eu003Cpu003E项目地址:Github – springboot2-lomboku003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h3″u003E四、参考资源u003Cu002Fh1u003Eu003Culu003Eu003Cliu003Eu003Cpu003ELombok 官网u003Cu002Fpu003Eu003Cu002Fliu003Eu003Cliu003Eu003Cpu003Elombok-cheatsheetu003Cu002Fpu003Eu003Cu002Fliu003Eu003Cliu003Eu003Cpu003EJava开发神器Lombok的使用与原理u003Cu002Fpu003Eu003Cu002Fliu003Eu003Cu002Fulu003Eu003Cpu003E-END-u003Cu002Fpu003Eu003Cpu003E如果看到这里,说明你喜欢这篇文章,请u003Cstrong toutiao-origin=”span” class=”highlight-text”u003E转发u003Cu002Fstrongu003Eu003Cstrong toutiao-origin=”span” class=”highlight-text”u003E、点赞u003Cu002Fstrongu003E。u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E微信u003Cu002Fiu003E搜索「web_resource」,u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-1″u003E关注u003Cu002Fiu003E后回复「进群」或者扫描下方二维码即可进入无广告交流群。u003Cu002Fpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FRclVELq4D7W5d4″ img_width=”499″ img_height=”642″ alt=”效率提高 10 倍!一份不可多得的 Lombok 学习指南” inline=”0″u003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002Fpgc-imageu002FReU3KFq9FXVax2″ img_width=”1071″ img_height=”742″ alt=”效率提高 10 倍!一份不可多得的 Lombok 学习指南” inline=”0″u003E”

原文始发于:效率提高 10 倍!一份不可多得的 Lombok 学习指南

主题测试文章,只做测试使用。发布者:逗乐男神i,转转请注明出处:http://www.cxybcw.com/17794.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code