java code plug simplified --Lombok

1. Concept

  a brief introduction:. Lombok is a can help us simplify the elimination of some of the must have tools but looked very bloated Java code by using the corresponding annotations, you can generate the corresponding source code at compile time by a simple method comment form

  b action:. Lombok by way annotation, the compiler automatically generates attribute constructors, getter / setter, equals, hashcode, toString method. Appears that there is no magic getter and setter methods in the source code, but there are getter and setter methods in the compiled byte code file. This eliminates the need to manually rebuild the trouble code, making the code look more succinctly

  . C Principle:

    1) javac source code analysis, generates an abstract syntax tree (AST)

    Process 2) invoke the running to achieve a "JSR 269 API" program of Lombok

    3) At this time, on the right AST Lombok obtained a first step of processing, where to find the class @Data annotation corresponding syntax tree (AST), and then modify the syntax tree (AST), the tree node corresponding increase getter and setter methods defined

    4) javac using the modified abstract syntax tree (AST) generated bytecode file, that is, to add new class node (block)

 

2. Install the plug-in

  a In the IDEA Settings -. Plugins search for "lombok", Install to install the plug-lombok

 

 

 

3. Use

  a. Import dependency maven

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.10</version>
      <scope>provided</scope>
    </dependency>

 

  b.创建Entity实体类

@Getter             //get方法
@Setter             //set方法
@NoArgsConstructor  //无参构造
@AllArgsConstructor //全参构造
@RequiredArgsConstructor(staticName = "of") //会生成一个包含常量,和标识了NotNull的变量的构造方法。生成的构造方法是私有的private
@ToString           //重写toString方法
@EqualsAndHashCode  //重写equals方法和hashCode方法
public class Cmdty {

    @NonNull
    private String id;
    @NonNull
    private int cmdtyCode;
    private String cmdtyName;
    private long price;

}

 

  c.测试

    public static void main( String[] args ) {
        String id = UUID.randomUUID().toString().replaceAll("-","");
        int cmdtyCode = 10001;
        String cmdtyName = "商品1";
        long price = 100;
        //测试Constructor全参构造@AllArgs
        Cmdty cmdty1 = new Cmdty(id, cmdtyCode, cmdtyName, price);
        //测试get方法@Getter
        System.out.println("Cmdty: id[" + cmdty1.getId() + "], cmdtyCode[" + cmdty1.getCmdtyCode() + "], cmdtyName[" + cmdty1.getCmdtyName() + "], price[" + cmdty1.getPrice() + "]");

        //测试Constructor指定参数自定义构造@RequiredArgsConstructor(staticName = "of")
        Cmdty cmdty2 = Cmdty.of(id, cmdtyCode);
        //测试重写toString方法@ToString
        System.out.println(cmdty2.toString());

        //测试重写equals方法和hashCode方法@EqualsAndHashCode
        System.out.println(cmdty1.equals(cmdty2));
        System.out.println("hashCode: cmdty1[" + cmdty1.hashCode() + "], cmdty2[" + cmdty2.hashCode() + "]");

        //测试set方法@Setter
        cmdty2.setCmdtyName(cmdtyName);
        cmdty2.setPrice(price);
        //测试重写equals方法和hashCode方法@EqualsAndHashCode
        System.out.println(cmdty1.equals(cmdty2));
        System.out.println("hashCode: cmdty1[" + cmdty1.hashCode() + "], cmdty2[" + cmdty2.hashCode() + "]");
    }

 

Guess you like

Origin www.cnblogs.com/vettel0329/p/11987336.html