lombok使用手册
- 格式:pdf
- 大小:127.62 KB
- 文档页数:9
lombok使⽤⼿册
lombok使⽤⼿册:
1、安装插件
1.1、idel:
elipse:下载地址
运⾏jar⽂件
1.2、使⽤lombok还需要引⽤相关jar包
参考⽹址:
2、注解说明
2.1、@Data
@Data
All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and
@RequiredArgsConstructor!
Lombok代码:
@Data
public class Lombok {
private String name;
private String age;
}
编译后代码:
public class Lombok {
private String name; private String age;
public Lombok() {
}
public String getName() {
return this.name;
}
public String getAge() {
return this.age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof Lombok)) {
return false;
} else {
Lombok other = (Lombok)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$age = this.getAge();
String other$age = other.getAge();
if(this$age == null) {
if(other$age != null) {
return false;
}
} else if(!this$age.equals(other$age)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Lombok;
}
public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $name = this.getName();
int result1 = result * 59 + ($name == null?43:$name.hashCode());
String $age = this.getAge();
result1 = result1 * 59 + ($age == null?43:$age.hashCode());
return result1;
}
public String toString() {
return "Lombok(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
}
2.2、@val
编译前代码:
val example = new ArrayList();
example.add("Hello, World!");
val foo = example.get(0);
return foo.toLowerCase();
val map = new HashMap();
map.put(0, "zero");
map.put(5, "five");
for (val entry : map.entrySet()) {
System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
}
编译后代码: final ArrayList example = new ArrayList();
example.add("Hello, World!");
final String foo = example.get(0);
return foo.toLowerCase();
final HashMap map = new HashMap();
map.put(0, "zero");
map.put(5, "five");
for (final Map.Entry entry : map.entrySet()) {
System.out.printf("%d: %s\n", entry.getKey(), entry.getValue());
}
2.3、@var ⽤法参考@val
2.4、@NonNull
编译前代码:
public NonNullExample(@NonNull Person person) {
this.name = person.getName();
}
编译后代码:
public NonNullExample(@NonNull Person person) {
if (person == null) {
throw new NullPointerException("person is marked @NonNull but is null");
}
this.name = person.getName();
}
2.5、@Cleanup
编译前代码:
@Cleanup InputStream in = new FileInputStream(args[0]);
@Cleanup OutputStream out = new FileOutputStream(args[1]);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
编译后代码:
InputStream in = new FileInputStream(args[0]);
try {
OutputStream out = new FileOutputStream(args[1]);
try {
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
} finally {
if (out != null) {
out.close();
}
}
} finally {
if (in != null) {
in.close();
}
}
2.6、@Getter、@Setter
编译前代码:
@Getter @Setter private int age = 10;
@Setter private String name;
编译后代码:
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
protected void setName(String name) {
this.name = name;
}
2.7、@ToString
编译前代码:
@ToString
public class Lombok { @Getter @Setter String name;
@Getter @Setter private String age;
@ToString.Exclude String id;
}
编译后代码:
public class Lombok {
String name;
private String age;
String id;
public Lombok() {
}
public String toString() {
return "Lombok(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return this.age;
}
public void setAge(String age) {
this.age = age;
}
}
2.8、@EqualsAndHashCode
编译前代码:
@EqualsAndHashCode
public class Lombok {
@Getter @Setter String name;
@Getter @Setter private String age;
@EqualsAndHashCode.Exclude String id;
}
编译后代码:
public class Lombok {
String name;
private String age;
String id;
public Lombok() {
}
public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof Lombok)) {
return false;
} else {
Lombok other = (Lombok)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$age = this.getAge();
String other$age = other.getAge();