Lombok入门使用教程及其优缺点详解( 三 )


复制代码@RequiredArgsConstructor(staticName = "myShape")@AllArgsConstructor(access = AccessLevel.PROTECTED)@NoArgsConstructorpublic class Shape {private int x;@NonNullprivate double y;@NonNullprivate String name;}实际效果相当于:
public class Shape {private int x;private double y;private String name;public Shape(){}protected Shape(int x,double y,String name){this.x = x;this.y = y;this.name = name;}public Shape(double y,String name){this.y = y;this.name = name;}public static Shape myShape(double y,String name){return new Shape(y,name);}}7)@Data注解在类上,会为类的所有属性自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法 。@Value注解和@Data类似,区别在于它会把所有成员变量默认定义为private final修饰,并且不会生成set方法 。
官方实例如下:
@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;}}实际效果相当于:
复制代码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 = https://www.isolves.com/it/cxkf/yy/JAVA/2020-10-23/value;}public static Exercise of(String name, T value) {return new Exercise(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;}}}8)@SneakyThrows这个注解用在方法上,可以将方法中的代码用try-catch语句包裹起来,捕获异常并在catch中用Lombok.sneakyThrow(e)把异常抛出,可以使用@SneakyThrows(Exception.class)的形式指定抛出哪种异常,很简单的注解,直接看个例子:
public class SneakyThrows implements Runnable {@SneakyThrows(UnsupportedEncodingException.class)public String utf8ToString(byte[] bytes) {return new String(bytes, "UTF-8");}@SneakyThrowspublic void run() {throw new Throwable();}}实际效果相当于:
public class SneakyThrows implements Runnable {public String utf8ToString(byte[] bytes) {try{return new String(bytes, "UTF-8");}catch(UnsupportedEncodingException uee){throw Lombok.sneakyThrow(uee);}}public void run() {try{throw new Throwable();}catch(Throwable t){throw Lombok.sneakyThrow(t);}}}


推荐阅读