使用lombok提高编码效率
Lombok简介
lombok项目通过增加处理程序使我们的java语言更加刺激(简洁和快速)。(lombok其实就是在我们进行编译事加一层处理的机制,通过各种注释,大量简化类生成时的步骤)。
常用注解
@Data :注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
@Setter:注解在属性上;为属性提供 setting 方法
@Getter:注解在属性上;为属性提供 getting 方法
@Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@Slf4j : 注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法
@Accessors(chain = true) 支持链式调用的:
@Builder 建筑者模式
@NonNull自检是否为空值
--------------------- 本文来自 汪小哥 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u012881904/article/details/64438046?utm_source=copy
@Data
编译前:
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
/**
* Created by wangji on 2017/3/20.
*/
@Slf4j
@Data
public class Person {
private String name;
private String old;
public static void main(String[] args) {
Person px =new Person();
px.setName("xx");
px.setOld("xx");
log.info(px.getName());
}
}
编译后的结果,可以看到给予了许多的get,set and toString等等
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Person {
private static final Logger log = LoggerFactory.getLogger(Person.class);
private String name;
private String old;
public static void main(String[] args) {
Person px = new Person();
px.setName("xx");
px.setOld("xx");
log.info(px.getName());
}
public Person() {
}
public String getName() {
return this.name;
}
public String getOld() {
return this.old;
}
public void setName(String name) {
this.name = name;
}
public void setOld(String old) {
this.old = old;
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
if(!other.canEqual(this)) {
return false;
} else {
String this$name = this.getName();
String other$name = other.getName();
if(this$name == null) {
if(other$name != null) {
return false;
}
} else if(!this$name.equals(other$name)) {
return false;
}
String this$old = this.getOld();
String other$old = other.getOld();
if(this$old == null) {
if(other$old != null) {
return false;
}
} else if(!this$old.equals(other$old)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Person;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $name = this.getName();
int result1 = result * 59 + ($name == null?43:$name.hashCode());
String $old = this.getOld();
result1 = result1 * 59 + ($old == null?43:$old.hashCode());
return result1;
}
public String toString() {
return "Person(name=" + this.getName() + ", old=" + this.getOld() + ")";
}
}
支持链式调用 @Data @Accessors(chain = true) 配合使用
类似这种类型:
Student student = new Student()
.setAge(24)
.setName("zs");
编译前:
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
/**
* Created by wangji on 2017/3/20.
*/
@Slf4j
@Data
@Accessors(chain = true)
public class Person {
private String name;
private String old;
public static void main(String[] args) {
Person px =new Person().setOld("xxx").setName("xxx");
log.info(px.getName());
}
}
编译后的代码:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Person {
private static final Logger log = LoggerFactory.getLogger(Person.class);
private String name;
private String old;
public static void main(String[] args) {
Person px = (new Person()).setOld("xxx").setName("xxx");
log.info(px.getName());
}
public Person() {
}
public String getName() {
return this.name;
}
public String getOld() {
return this.old;
}
public Person setName(String name) {
this.name = name;
return this;
}
public Person setOld(String old) {
this.old = old;
return this;
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
if(!other.canEqual(this)) {
return false;
} else {
String this$name = this.getName();
String other$name = other.getName();
if(this$name == null) {
if(other$name != null) {
return false;
}
} else if(!this$name.equals(other$name)) {
return false;
}
String this$old = this.getOld();
String other$old = other.getOld();
if(this$old == null) {
if(other$old != null) {
return false;
}
} else if(!this$old.equals(other$old)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Person;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $name = this.getName();
int result1 = result * 59 + ($name == null?43:$name.hashCode());
String $old = this.getOld();
result1 = result1 * 59 + ($old == null?43:$old.hashCode());
return result1;
}
public String toString() {
return "Person(name=" + this.getName() + ", old=" + this.getOld() + ")";
}
}
@val @var
使用Lombok ,java也能够像javascript一样使用弱类型定义变量了
val注解变量申明是final类型 var注解变量是非final类型
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import lombok.val;
-
-
public class ValExample {
-
public String example() {
-
val example = new ArrayList<String>();
-
example.add("Hello, World!");
-
val foo = example.get(0);
-
return foo.toLowerCase();
-
}
-
-
public void example2() {
-
val map = new HashMap<Integer, String>();
-
map.put(0, "zero");
-
map.put(5, "five");
-
for (val entry : map.entrySet()) {
-
System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
-
}
-
}
-
}
翻译后
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.Map;
-
-
public class ValExample {
-
public String example() {
-
final ArrayList<String> example = new ArrayList<String>();
-
example.add("Hello, World!");
-
final String foo = example.get(0);
-
return foo.toLowerCase();
-
}
-
-
public void example2() {
-
final HashMap<Integer, String> map = new HashMap<Integer, String>();
-
map.put(0, "zero");
-
map.put(5, "five");
-
for (final Map.Entry<Integer, String> entry : map.entrySet()) {
-
System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
-
}
-
}
-
}
2.@NonNull
在方法或构造函数的参数上使用@NonNull,lombok将生成一个空值检查语句。
-
import lombok.NonNull;
-
-
public class NonNullExample extends Something {
-
private String name;
-
-
public NonNullExample(@NonNull Person person) {
-
super("Hello");
-
this.name = person.getName();
-
}
-
}
翻译后
-
import lombok.NonNull;
-
-
public class NonNullExample extends Something {
-
private String name;
-
-
public NonNullExample(@NonNull Person person) {
-
super("Hello");
-
if (person == null) {
-
throw new NullPointerException("person");
-
}
-
this.name = person.getName();
-
}
-
}
本文来自 汪小哥和再见尼罗河 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u012881904/article/details/64438046?utm_source=copy
https://blog.csdn.net/v2sking/article/details/73431364?utm_source=copy