Annotation
Code
Result
Getter & Setter
1 2 3 4 5 6 public class User { @Getter private BigInteger id; @Setter private String username; private String password; private String email; }
ToString
1 2 3 4 5 6 7 @ToString public class User { private BigInteger id; private String username; private String password; private String email; }
1 2 3 4 5 6 7 8 9 10 public String toString () { return "User(id=" + this .getId() + ", username=" + this .getUsername() + ", password=" + this .getPassword() + ", email=" + this .getEmail() + ")" ; }
EqualsAndHashCode
NonNull
1 2 3 4 @NonNull private BigDecimal id;public void test (@NonNull String s) {...}
Performs null check
NoArgsConstructor
1 2 3 4 5 6 7 @NoArgsConstructor public class User { @NonNull private BigInteger id; private String username; private String password; private String email; }
RequiredArgsConstructor
1 2 3 4 5 6 7 @RequiredArgsConstructor public class User { @NonNull private BigInteger id; final private String username; private String password; private String email; }
1 2 3 4 5 6 7 8 9 public User (@NonNull BigInteger id, String username) { if (id == null ) { throw new NullPointerException("id is marked non-null but is null" ); } else { this .id = id; this .username = username; } }
AllArgsConstructor
1 2 3 4 5 6 7 @AllArgsConstructor public class User { @NonNull private BigInteger id; private String username; private String password; private String email; }
1 2 3 4 5 6 7 8 9 10 public User (@NonNull BigInteger id, String username, String password, String email) { if (id == null ) { throw new NullPointerException("id is marked non-null but is null" ); } else { this .id = id; this .username = username; this .password = password; this .email = email; } }
Data
1 2 3 4 5 6 7 @Data public class User { @NonNull private BigInteger id; final private String username; private String password; private String email; }
work as a combination of getter/setter, equalsAndHashcode, toString, requiredArgsConstructors.
Builder
1 2 3 4 5 6 7 8 @Data @Builder public class User { @NonNull private BigInteger id; final private String username; private String password; private String email; }
Log
1 2 @Log log.info("create user" );
val
1 val map = new HashMap<String, String>();
work as val in JavaScript
CleanUp
1 2 @Cleanup InputStream in = null ;@Cleanup FileOutputStream out = null ;
1 2 3 4 5 6 7 8 9 10 11 Object in = null ; try { FileOutputStream out = null ; if (Collections.singletonList(out).get(0 ) != null ) { ((FileOutputStream)out).close(); } } finally { if (Collections.singletonList(in).get(0 ) != null ) { ((InputStream)in).close(); } }