1、@NonNull 非空检验可以用@notNull代替,后者还可以有返回信息
1 @NotNull(message = "参数不能为null") 2 private boolean name; 3 @NonNull 4 private int age;
2、@Cleanup---这个在jdk1.7之后,可以用try with resourse
释放资源
1 public void test(){ 2 try { 3 @Cleanup OutputStream outputStream = new FileOutputStream(""); 4 outputStream.write(5); 5 } catch (Exception e) { 6 e.printStackTrace(); 7 } 8 }
反编译后
1 public void test() { 2 try { 3 FileOutputStream e = new FileOutputStream(""); 4 5 try { 6 e.write(5); 7 } finally { 8 if(Collections.singletonList(e).get(0) != null) { 9 e.close(); 10 } 11 12 } 13 } catch (Exception var6) { 14 var6.printStackTrace(); 15 } 16 17 }
try-with-resourse
1 public void test(){ 2 try (OutputStream outputStream = new FileOutputStream("")){ 3 outputStream.write(5); 4 } catch (Exception e) { 5 e.printStackTrace(); 6 } 7 }
反编译后
1 public void test() { 2 try { 3 FileOutputStream e = new FileOutputStream(""); 4 Throwable var2 = null; 5 6 try { 7 e.write(5); 8 } catch (Throwable var12) { 9 var2 = var12; 10 throw var12; 11 } finally { 12 if(e != null) { 13 if(var2 != null) { 14 try { 15 e.close(); 16 } catch (Throwable var11) { 17 var2.addSuppressed(var11); 18 } 19 } else { 20 e.close(); 21 } 22 } 23 24 } 25 } catch (Exception var14) { 26 var14.printStackTrace(); 27 } 28 29 }
3、@getter--@setter---@Accessors
chain属性,可以想jq一样连续编程(默认是false)
1 @Getter 2 @Setter 3 @Accessors(chain = true) 4 public class TestDTO { 5 6 private String name; 7 8 private int age; 9 10 }
1 public class TestDTO { 2 private String name; 3 private int age; 4 5 public TestDTO() { 6 } 7 8 public String getName() { 9 return this.name; 10 } 11 12 public int getAge() { 13 return this.age; 14 } 15 16 public TestDTO setName(String name) { 17 this.name = name; 18 return this; 19 } 20 21 public TestDTO setAge(int age) { 22 this.age = age; 23 return this; 24 } 25 }
4、@ToString
1 @ToString 2 public class TestDTO { 3 4 private String name; 5 6 private int age; 7 8 }
反编译
1 public class TestDTO { 2 private String name; 3 private int age; 4 5 public TestDTO() { 6 } 7 8 public String toString() { 9 return "TestDTO(name=" + this.name + ", age=" + this.age + ")"; 10 } 11 }
ToString里面的属性 includeFieldNames = false
1 @ToString(includeFieldNames = false) 2 public class TestDTO { 3 4 private String name; 5 6 private int age; 7 8 }
反编译后--没有字段的名称
1 public class TestDTO { 2 private String name; 3 private int age; 4 5 public TestDTO() { 6 } 7 8 public String toString() { 9 return "TestDTO(" + this.name + ", " + this.age + ")"; 10 } 11 }
第二个属性
1 @ToString(exclude = "age") 2 public class TestDTO { 3 4 private String name; 5 6 private int age; 7 8 }
反编译后--排除了exclude指定的属性
1 public class TestDTO { 2 private String name; 3 private int age; 4 5 public TestDTO() { 6 } 7 8 public String toString() { 9 return "TestDTO(name=" + this.name + ")"; 10 } 11 }
第三个属性
1 @ToString(of = "age") 2 public class TestDTO { 3 4 private String name; 5 6 private int age; 7 8 }
反编译后--只出现了of = "age" 中的属性
1 public class TestDTO { 2 private String name; 3 private int age; 4 5 public TestDTO() { 6 } 7 8 public String toString() { 9 return "TestDTO(age=" + this.age + ")"; 10 } 11 }
第四个属性
1 @ToString(callSuper =true ) 2 public class TestDTO { 3 4 private String name; 5 6 private int age; 7 8 }
反编译后--输出了父类的toString
1 public class TestDTO { 2 private String name; 3 private int age; 4 5 public TestDTO() { 6 } 7 8 public String toString() { 9 return "TestDTO(super=" + super.toString() + ", name=" + this.name + ", age=" + this.age + ")"; 10 } 11 }
第五个属性
1 @ToString 2 @Getter 3 public class TestDTO { 4 5 private String name; 6 7 private int age; 8 9 }
反编译后--使用了有@getter注解后使用了get方法
1 public class TestDTO { 2 private String name; 3 private int age; 4 5 public TestDTO() { 6 } 7 8 public String toString() { 9 return "TestDTO(name=" + this.getName() + ", age=" + this.getAge() + ")"; 10 } 11 12 public String getName() { 13 return this.name; 14 } 15 16 public int getAge() { 17 return this.age; 18 } 19 }
使用属性
1 @ToString(doNotUseGetters = true) 2 @Getter 3 public class TestDTO { 4 5 private String name; 6 7 private int age; 8 9 }
反编译后--- (doNotUseGetters = true)--没有使用get方法,而是直接使用this.属性
1 public class TestDTO { 2 private String name; 3 private int age; 4 5 public TestDTO() { 6 } 7 8 public String toString() { 9 return "TestDTO(name=" + this.name + ", age=" + this.age + ")"; 10 } 11 12 public String getName() { 13 return this.name; 14 } 15 16 public int getAge() { 17 return this.age; 18 } 19 }
5、@EqualsAndHashCode
1 @EqualsAndHashCode 2 public class TestDTO { 3 4 private String name; 5 6 private int age; 7 8 }
反编译后
1 public class TestDTO { 2 private String name; 3 private int age; 4 5 public TestDTO() { 6 } 7 8 public boolean equals(Object o) { 9 if(o == this) { 10 return true; 11 } else if(!(o instanceof TestDTO)) { 12 return false; 13 } else { 14 TestDTO other = (TestDTO)o; 15 if(!other.canEqual(this)) { 16 return false; 17 } else { 18 String this$name = this.name; 19 String other$name = other.name; 20 if(this$name == null) { 21 if(other$name == null) { 22 return this.age == other.age; 23 } 24 } else if(this$name.equals(other$name)) { 25 return this.age == other.age; 26 } 27 28 return false; 29 } 30 } 31 } 32 33 protected boolean canEqual(Object other) { 34 return other instanceof TestDTO; 35 } 36 37 public int hashCode() { 38 boolean PRIME = true; 39 byte result = 1; 40 String $name = this.name; 41 int result1 = result * 59 + ($name == null?43:$name.hashCode()); 42 result1 = result1 * 59 + this.age; 43 return result1; 44 } 45 }