lombook : 让代码更优雅

Project Lombok makes java a spicier language by adding ‘handlers’ that know >how to build and compile simple, boilerplate-free, not-quite-java code.

安装与使用

  • 定位到 File > Settings > Plugins
  • 点击 Browse repositories…
  • 搜索 Lombok Plugin
  • 点击 Install plugin
  • 重启 IDEA

@NonNull

public class NonNullExample extends Something {  
    private String name;  

    public NonNullExample(@NonNull Person person) {  
    super("Hello");  
    this.name = person.getName();  
    }  
}
翻译成java代码 ==>
public class NonNullExample extends Something {  
    private String name;  

    public NonNullExample(@NonNull Person person) {  
    super("Hello");  
    if (person == null) {  
    throw new NullPointerException("person");  
    }  
    this.name = person.getName();  
    }  
}

@Cleanup

public class CleanupExample {  
    public static void main(String\[\] args) throws IOException {  
    @Cleanup InputStream in = new FileInputStream(args\[0\]);  
    @Cleanup OutputStream out = new FileOutputStream(args\[1\]);  
    byte\[\] b = new byte\[10000\];  
    while (true) {  
    int r = in.read(b);  
    if (r == -1) break;  
    out.write(b, 0, r);  
    }  
  }  
}
翻译成java代码 ==>
public class CleanupExample {  
    public static void main(String\[\] args) throws IOException {  
    InputStream in = new FileInputStream(args\[0\]);  
    try {  
    OutputStream out = new FileOutputStream(args\[1\]);  
    try {  
    byte\[\] b = new byte\[10000\];  
    while (true) {  
    int r = in.read(b);  
    if (r == -1) break;  
    out.write(b, 0, r);  
    }  
    } finally {  
    if (out != null) {  
    out.close();  
    }  
    }  
    } finally {  
    if (in != null) {  
    in.close();  
      }  
    }  
  }  
}

@Data

相当于

@ToString
@EqualsAndHashCode
@Getter(所有字段)
@Setter (所有非final字段)
@RequiredArgsConstructor

你可能感兴趣的:(java,注解)