Introduction
用Java编写是一门痛苦的事。 甚至简单的代码也是冗长的。 Lombok项目简化了Java语法。 这种减少最多可以达到普通Java代码的90%。
让我们看看龙目岛是如何做到的。
Setting Up
I'm using maven. Here are the Project Lombok maven docs.
我添加了此依赖项:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
我的IDE是Eclipse。
Here are the Eclipse docs.
Cleaning Up Accessor Methods
我的IDE为我生成了数千个getter和setter。 但是,要通过Eclipse的UI生成它们很困难。 Lombok完全消除了这样做的必要!
要为整个类生成访问器方法:
@Getter
@Setter
public class GetterSetterExample {
private int age = 10;
private String name;
}
自定义可见性:
public class GetterSetterExample {
@Getter @Setter private int age = 10;
@Setter(AccessLevel.PROTECTED) private String name;
@Override public String toString() {
return String.format("%s (age: %d)", name, age);
}
}
Null Checking
每次看到空指针异常有了无用的信息,我的血压就会飙升。 我们可以使用@NonNull属性使事情变得容易。
public int getStringLength(@NonNull String str) {
return str.length();
}
如果我这样做:
getStringLength(null);
我得到这个:
Exception in thread "main" java.lang.NullPointerException: str is marked non-null but is null
toString
Java开发人员存在的另一个祸根:toString()方法。 像访问器方法一样,仅需要注解。
@ToString(includeFieldNames = true)
public class Square {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
这样做:
Square square = new Square(5, 10);
System.out.println(square.toString());
将打印出:
Square(width=5, height=10)
Constructors
不能再有其他权利了吗? 好吧,有。 甚至建设者也不能幸免于龙目岛的力量。
为所有变量创建一个构造函数:
@AllArgsConstructor
public class Square {
private int width, height;
}
没有参数构造函数:
@NoArgsConstructor
public class Square {
private int width, height;
}
@RequiredArgsConstructor将为以下变量创建一个构造函数最后或有@NonNull
@RequiredArgsConstructor
public class Square {
private int width, height;
}
@Data, the Mother of All Annotations
仍然有太多代码适合您吗? 好,@数据结合@ToString,@盖特,@Setter,and @RequiredArgsConstructor!
因此,这段邪恶的Java代码(117行):
public class DataExample {
private final String name;
private int age;
private double score;
private String[] tags;
public DataExample(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setScore(double score) {
this.score = score;
}
public double getScore() {
return this.score;
}
public String[] getTags() {
return this.tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
@Override public String toString() {
return "DataExample(" + this.getName() + ", " + this.getAge() + ", " + this.getScore() + ", " + Arrays.deepToString(this.getTags()) + ")";
}
protected boolean canEqual(Object other) {
return other instanceof DataExample;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof DataExample)) return false;
DataExample other = (DataExample) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false;
if (this.getAge() != other.getAge()) return false;
if (Double.compare(this.getScore(), other.getScore()) != 0) return false;
if (!Arrays.deepEquals(this.getTags(), other.getTags())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
final long temp1 = Double.doubleToLongBits(this.getScore());
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + this.getAge();
result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32));
result = (result*PRIME) + Arrays.deepHashCode(this.getTags());
return result;
}
public static class Exercise<T> {
private final String name;
private final T value;
private Exercise(String name, T value) {
this.name = name;
this.value = value;
}
public static <T> Exercise<T> of(String name, T value) {
return new Exercise<T>(name, value);
}
public String getName() {
return this.name;
}
public T getValue() {
return this.value;
}
@Override public String toString() {
return "Exercise(name=" + this.getName() + ", value=" + this.getValue() + ")";
}
protected boolean canEqual(Object other) {
return other instanceof Exercise;
}
@Override public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Exercise)) return false;
Exercise<?> other = (Exercise<?>) o;
if (!other.canEqual((Object)this)) return false;
if (this.getName() == null ? other.getValue() != null : !this.getName().equals(other.getName())) return false;
if (this.getValue() == null ? other.getValue() != null : !this.getValue().equals(other.getValue())) return false;
return true;
}
@Override public int hashCode() {
final int PRIME = 59;
int result = 1;
result = (result*PRIME) + (this.getName() == null ? 43 : this.getName().hashCode());
result = (result*PRIME) + (this.getValue() == null ? 43 : this.getValue().hashCode());
return result;
}
}
}
变成这个友好的代码段(13行):
@Data public class DataExample {
private final String name;
@Setter(AccessLevel.PACKAGE) private int age;
private double score;
private String[] tags;
@ToString(includeFieldNames=true)
@Data(staticConstructor="of")
public static class Exercise<T> {
private final String name;
private final T value;
}
}
Conclusion
Project Lombok is my best find this year. But wait, there's more!
The documentation is fantastic and has further features to sink your teeth into.