CleanCode(1):Loombok

Lombok simple annotations, to generate the actual code corresponding to the class after compilation, the source code so that cleaner;

The main notes introduce several commonly used:

@Data : with class, the equivalent of using both @ ToString, @ EqualsAndHashCode, @ Getter , @ Setter and @RequiredArgsConstrutor these annotations, POJO class is very useful for

@ Getter / @ Setter: use on the property, not their own handwriting setter and getter methods, you can specify the extent of access

@ToString: with class, method can automatically override toString

@EqualsAndHashCode: with class, the automatic generation equals and hashCode methods

@NonNull: Before using the method parameters, will automatically check on the parameters of non-null, null throw NPE (NullPointerException)

@Cleanup: automatic management of resources, local variables used before, is about to execute within the current range of variables will clean up resources before you get out, try-finally generated code stream is closed

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor: with class, automatically generated constructor with no arguments constructor and use all parameters and all @NonNull properties as constructor arguments
@Accessors (chain = true) using a chain set properties, set this method returns the object.

@Value: with class is immutable form @Data, the equivalent of adding final declaration for the property, provided only getter methods, without providing setter methods
@Builder: with class, constructor, method, to provide you with complex builder API method
@SneakyThrows: automatic ejection abnormality subjects, without the explicit use of the method throws clause
@Synchronized: used in the method, a method is declared synchronized and automatically lock
@Getter (lazy = true) : You can replace the classic Double Check Lock boilerplate code
@Log: generate different types of log objects based on different notes, but all log instance name, there are a variety of optional implementation class


Specific examples:

1, @ Accessors (chain = true  ) using a chain set properties, set this method returns the object.

@Setter
@Getter
@Accessors(chain = true)
public class User {
    private String id;
    private String name;
    private Integer age;
}

public static void main(String[] args) {
   User user = new User().setId(1).setName("name").setAge(1);

 

 

 

 

Guess you like

Origin www.cnblogs.com/clarino/p/12032646.html