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

@NonNulljava

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();  
    }  
}

@Cleanupui

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();  
      }  
    }  
  }  
}

@Datathis

至關於翻譯

@ToString
@EqualsAndHashCode
@Getter(全部字段)
@Setter (全部非final字段)
@RequiredArgsConstructor
相關文章
相關標籤/搜索