Lombok
作用:帮使用者提高编码效率,减少重复与冗余的代码
原理:ASM 动态修改class文件
配置
maven
依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
</dependency>
编译
<build>
<plugins>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.6.1</version>
</plugin>
</plugins>
</build>
idea
- 安装插件
常用注解:
java bean相关
@Setter
-
功能
生成setter方法
-
源码
@Setter public class LombokDemo { private Integer id; private String name; }
-
编译后
package xyz.mrwood.study.lombok;
public class LombokDemo { private Integer id; private String name;
public LombokDemo() { } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; }
}
@Getter
-
功能
生成getter方法
-
源码
@Getter public class LombokDemo {
private Integer id; private String name; }
-
编译后
package xyz.mrwood.study.lombok;
public class LombokDemo { private Integer id; private String name;
public LombokDemo() { } public Integer getId() { return this.id; } public String getName() { return this.name; }
}
@ToString
-
功能
生成toString方法
-
源码
@ToString public class LombokDemo { private Integer id; private String name; }
-
编译后
package xyz.mrwood.study.lombok; public class LombokDemo { private Integer id; private String name; public LombokDemo() { } public String toString() { return "LombokDemo(id=" + this.id + ", name=" + this.name + ")"; } }
@Getter(lazy = true)
-
功能
懒加载属性
-
注意:
这个与上面@Getter不同,那个是修饰在类上的,也可以修饰在属性上。如果有lazy=true只能修饰在属性,并且还要是private final修饰,限制很大
-
编码
public class LombokDemo { @Getter(lazy = true) private final List<Integer> ids = Arrays.asList(1, 2, 3, 4); private String name; }
-
编译后
package xyz.mrwood.study.lombok; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicReference; public class LombokDemo { private final AtomicReference<Object> ids = new AtomicReference(); private String name; public LombokDemo() { } public List<Integer> getIds() { Object value = this.ids.get(); if(value == null) { AtomicReference var2 = this.ids; synchronized(this.ids) { value = this.ids.get(); if(value == null) { List actualValue = Arrays.asList(new Integer[]{Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3), Integer.valueOf(4)}); value = actualValue == null?this.ids:actualValue; this.ids.set(value); } } } return (List)((List)(value == this.ids?null:value)); } }
@EqualsAndHashCode
-
功能
生成equals方法与hashCode方法
-
源码
@EqualsAndHashCode public class LombokDemo {
private Integer id; private String name;
}
-
编译后
package xyz.mrwood.study.lombok;
public class LombokDemo { private Integer id; private String name;
public LombokDemo() { } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof LombokDemo)) { return false; } else { LombokDemo other = (LombokDemo)o; if(!other.canEqual(this)) { return false; } else { Integer this$id = this.id; Integer other$id = other.id; if(this$id == null) { if(other$id != null) { return false; } } else if(!this$id.equals(other$id)) { return false; } String this$name = this.name; String other$name = other.name; if(this$name == null) { if(other$name != null) { return false; } } else if(!this$name.equals(other$name)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof LombokDemo; } public int hashCode() { boolean PRIME = true; byte result = 1; Integer $id = this.id; int result1 = result * 59 + ($id == null?43:$id.hashCode()); String $name = this.name; result1 = result1 * 59 + ($name == null?43:$name.hashCode()); return result1; }
}
@NoAragsConstructor
-
功能
添加一个无参构造函数
-
注意
这个注解在没有其它有参构造函数的情况下使用意义不大,因为在这种情况下java默认会添加一个无参构造函数
-
源码
@NoArgsConstructor public class LombokDemo { private Integer id; private String name; }
-
编译后
package xyz.mrwood.study.lombok; public class LombokDemo { private Integer id; private String name; public LombokDemo() { } }
@AllArgsConstructor
-
功能
添加一个所有参数的构造函数
-
源码
@AllArgsConstructor public class LombokDemo { private Integer id; private String name; }
-
编译后
package xyz.mrwood.study.lombok; import java.beans.ConstructorProperties; public class LombokDemo { private Integer id; private String name; @ConstructorProperties({"id", "name"}) public LombokDemo(Integer id, String name) { this.id = id; this.name = name; } }
@RequiredArgsConstructor
-
功能
生成一个包含必填参数的构造函数
-
注意
要与@NonNull 搭配使用,该注解修饰的属性就是必填参数
-
源码
@RequiredArgsConstructor public class LombokDemo { @NonNull private Integer id; private String name; }
-
编译后
package xyz.mrwood.study.lombok; import java.beans.ConstructorProperties; import lombok.NonNull; public class LombokDemo { @NonNull private Integer id; private String name; @ConstructorProperties({"id"}) public LombokDemo(@NonNull Integer id) { if(id == null) { throw new NullPointerException("id"); } else { this.id = id; } } }
参考 https://blog.csdn.net/u011719271/article/details/53842420#sneakythrows
本文由 SAn 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2018/09/02 23:26