極簡代碼神器:Lombok使用教程
- 2019 年 10 月 3 日
- 筆記
Lombok ???????? java ???????????? java Bean ???? Getter?Setter??????? logger?ToString?HashCode?Builder ? java????????????????????? java Bean ????????
lombok ?????????????????????????????????????????Java???????????????????20% ???? 80%????
??????? lombok ??????
@Data
@Data ????????????@ToString
? @EqualAndHashCode
?@Getter/@Setter
??@RequiredArgsConstructor
???????????@Data ????????POJO(Plain Old Java Objects) ? bean ????????????????????????????????????toString?equals ? hashcode ???????????????final ????????????@NonNull
??????????final?????????????null?
@Data ????????? @toString ? @EqualAndHashCode? @Getter? @Setter ? @RequiredArgsConstructor ?????@Data = @Getter + @Setter + @ToString + @EqualsAndHashCode + @RequiredArgsConstructor
???@Data ??????????????callSuper?includeFieldNames ? exclude
?????????????????????????????????;
?????getters/setters ????public ?????????????????@Setter @Getter?????????????????????(??? AccessLevel.NONE??)????? getter?setter?
???? transient
?????????? hashcode ? equals?????????????????????????????????setter / getter??
????????????????????????????????????????????????????
???????? equals ??????????????? equals ???????????????????????????????????????????????????????????? @Data ????????toString?equals???getter?setter?
?????@ lombok.experimental.Tolerate ??????????????????? lombok ?
???
import lombok.AccessLevel; import lombok.Data; import lombok.Setter; import lombok.ToString; @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; } }
??????? lombok ??????
import java.util.Arrays; 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; } } }
@NonNull
????? @NonNull
?????????? null – check
??lombok????????????????@Data??Lombok??????????@[email protected]???????????????????null-check???
Null – Check ???????????
if(param == null){ throw new NullPointerException("param is marked @NonNull but is null") }
??????????????????
public class NonNullExample { @Getter private String name; public NonNullExample(@NonNull String name){ this.name = name; } }
???? @NonNull ?????????????
import lombok.NonNull; public class NonNullExample { private String name; public NonNullExample(@NonNull String name) { if (name == null) { throw new NullPointerException("name is marked @NonNull but is null"); } this.name = name; } }
@Getter & @Setter
????? @Getter ? @Setter ?????? getter/setter?
??? getter ????????????????? foo?????? getFoo()???????? boolean ???? isFoo()?????? foo ?????? setter ?? setFoo?????? void ??????????????????????????????????
?????AccessLevel ????????? Getter / Setter ???????? public ??????AccessLevel?????? PUBLIC
, PROTECTED
, PACKAGE
, and PRIVATE
.
????????? @Getter / @Setter ???????????????????????? get and set ??
???????? AccessLevel.NONE ???? get and set ????????? @Data?@Getter / @Setter ??????
public class GetterSetterExample { @Setter @Getter private int age = 10; @Setter(AccessLevel.PROTECTED) private String name; @Getter(AccessLevel.PRIVATE) private String high; }
????
public class GetterSetterExample { private int age = 10; private String name; private String high; public int getAge() { return age; } public void setAge(int age) { this.age = age; } protected void setName(String name) { this.name = name; } private String getHigh(){ return high; } }
@ToString
@ToString ????????? toString() ???????????????????????????????????????
???? includeFieldNames = true
??? toString() ????????????????
???????????????????????????????????? @ToString.Exclude
?????????ToString(onlyExplicitlyIncluded = true)
??????????????????@ ToString.Include
???????????
???? callSuper
? true ????toString???????????????????java.lang.Object ? toString() ???????????????????????????????
?????toString ??????????????????????(???)?????????@ ToString.Include
?????
????? @ToString.Include?name =“some other name”?
?????????????????? @ ToString.Include?rank = -1?
??????????????????????0???????????????????????????????????????
@ToString public class ToStringExample { // ???????? private static final int STATIC_VAR = 10; private String name; private String[] tags; private Shape shape = new Square(5, 10); // ????????? toString ?? @ToString.Exclude private int id; public String getName() { return this.name; } // callSuper ????????? toString(), // includeFieldNames ?????????? @ToString(callSuper = true, includeFieldNames = true) public static class Square extends Shape{ private final int width, height; public Square(int width, int height) { this.width = width; this.height = height; } } public static class Shape {} }
?????????
ToStringExample toStringExample = new ToStringExample(); System.out.println(toStringExample);
????
ToStringExample(name=null, tags=null, shape=ToStringExample.Square(super=com.project.lombok.ToStringExample$Square@1b9e1916, width=5, height=10))
??? callSuper = true? ??????
ToStringExample(name=null, tags=null, shape=ToStringExample.Square(width=5, height=10))
?????????????????????? Shape ???????callSuper ??? false
??? includeFieldNames
?????????????? includeFieldNames ???? true
?? includeFieldNames = false???????
ToStringExample(name=null, tags=null, shape=ToStringExample.Square(super=com.project.lombok.ToStringExample$Square@1b9e1916, 5, 10))
???????????? includeFieldNames = false ?????Shape ?????????
???@ToString ??????????????????
import java.util.Arrays; public class ToStringExample { private static final int STATIC_VAR = 10; private String name; private Shape shape = new Square(5, 10); private String[] tags; private int id; public String getName() { return this.getName(); } public static class Square extends Shape { private final int width, height; public Square(int width, int height) { this.width = width; this.height = height; } @Override public String toString() { return "Square(super=" + super.toString() + ", width=" + this.width + ", height=" + this.height + ")"; } } @Override public String toString() { return "ToStringExample(" + this.getName() + ", " + this.shape + ", " + Arrays.deepToString(this.tags) + ")"; } public static class Shape {} }
@EqualsAndHashCode
??????????@EqualsAndHashCode
???? lombok ???? equals
? hashCode
?????????????????? transient ?????????????? @EqualsAndHashCode.Include
? @EqualsAndHashCode.Exclude
????????????????
?????????? @EqualsAndHashCode.Include ??? @EqualsAndHashCode?onlyExplicitlyIncluded = true??????????????????????
??? @EqualsAndHashCode ??????????????????????????????????equals
? hashcode
?????????????????????????????equals / hashCode??????? callSuper ? true??????????????? equals ? hachcode ???
?? hashCode ???super.hashCode ????????????? equals ????????????????????????????? false??????????equals ????????????????lombok??? equals
?????????????
???????(?????java.lang.Object
?)?? callSuper ??? true ?????????? lombok ????? equals()
??? hashCode()
?????? Object ?????????? Object ????????????? hashCode ?
???????????? callSuper ? true ????????????????????lombok???????????????????????????????? callSuper ????????? lombok.equalsAndHashCode.callSuper
??key?
???????
@EqualsAndHashCode public class EqualsAndHashCodeExample { private transient int transientVar = 10; private String name; private double score; @EqualsAndHashCode.Exclude private Shape shape = new Square(5,10); private String[] tags; @EqualsAndHashCode.Exclude private int id; public String getName() { return name; } @EqualsAndHashCode(callSuper = true) public static class Square extends Shape { private final int width,height; public Square(int width,int height){ this.width = width; this.height = height; } } public static class Shape {} }
@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor
lombok ???????????????????????????????
@NoArgsConstructor
????????????????final
?????????? final ???????????????????? @NoArgsConstructor ?????????
???????? @NoArgsConstructor ??????@NoArgsConstructor(force=true)?lombok?????final ????????????id ? int ????? id ????? 0????????????????? false / null / 0
???? Java ????? hibernate ? ??????????????????????? @Data ?????????????????
????????????@NonNull ??? @NoArgsConstructor ????
@NoArgsConstructor @Getter public class NoArgsConstructorExample { private Long id ; private @NonNull String name; private Integer age; public static void main(String[] args) { System.out.println(new NoArgsConstructorExample().getName()); } }
????? null ?????? @NonNull
???????????? @NoArgsConstructor
???
@RequiredArgsConstructor
???????????????????1???????????????? final ???????????????? @NonNull ??????????????????????????????????????? @NonNull ??????????null ?????????? @NonNull
??????? null??????????? NullPointerException?????????????????????
??????????? @NonNull ? final ?????????????
@RequiredArgsConstructor public class RequiredArgsConstructorExample { @NonNull private int id; private final String name; private boolean human; }
???????????
public class RequiredArgsConstructorExample { @NonNull private int id; private final String name; private boolean human; public RequiredArgsConstructorExample(@NonNull int id, String name) { if (id == null) { throw new NullPointerException("id is marked @NonNull but is null"); } else { this.id = id; this.name = name; } } }
@AllArgsConstructor
: @AllArgsConstructor ??????????????1???????????@NonNull ?????????????????
@AllArgsConstructor public class AllArgsConstructorExample { private int id; private String name; private int age; }
???????????
public AllArgsConstructorExample(int id, String name, int age) { this.id = id; this.name = name; this.age = age; }
????????????????????????????????????????????????????????????????staticName????????@RequiredArgsConstructor?staticName =“of”?????????
@RequiredArgsConstructor(staticName = "of") @AllArgsConstructor(access = AccessLevel.PROTECTED) public class ConstructorExample<T> { private int x, y; @NonNull private T description; @NoArgsConstructor public static class NoArgsExample { @NonNull private String field; } }
????
public class ConstructorExample<T> { private int x, y; @NonNull private T description; private ConstructorExample(T description) { if (description == null) throw new NullPointerException("description"); this.description = description; } public static <T> ConstructorExample<T> of(T description) { return new ConstructorExample<T>(description); } @java.beans.ConstructorProperties({"x", "y", "description"}) protected ConstructorExample(int x, int y, T description) { if (description == null) throw new NullPointerException("description"); this.x = x; this.y = y; this.description = description; } public static class NoArgsExample { @NonNull private String field; public NoArgsExample() { } } }
?????
https://www.hellojava.com/a/74973.html
https://www.projectlombok.org/features/constructor
???cxuan
????
2. ???????
3. ????????
4. ????????
5. ???????