[elegant code] selected notes and principles of 01 Lombok
Welcome to b station official account / public number [hexagon warrior Xia Ning], a man who wants to fill all the indicators. github directory Included.
The handsome and beautiful in front of the screen, if it helps you, please like it and add a collection, which is really important to me. Don't worry about where you go next time.
1. Background introduction
In daily development, it is inevitable to generate some cumbersome code automatically. Although the function of ide is very powerful, it can not be dynamic. lombok can solve this problem very well. It will compile the class file into the usual code when it is generated. Here are some comments that I personally think are easier to use
2.lombok
Go first Official website Address. If you want to know more comments, you can go to https://projectlombok.org/features/all
2.1.get/set annotation (important)
This part has @ Data, @ Getter and @ Setter annotations. Generally, ordinary Bean objects will use @ Data annotation (which already contains two other annotations). If it is enum, use @ Getter annotation
@Data static class DataBean { private String name; } // The usage is as follows public static void DataBeanExample() { log.info(new DataBean().getName()); }
The generated core code is as follows
static class DataBean { private String name; public DataBean() { } public String getName() { return this.name; } public void setName(final String name) { this.name = name; } ... }
2.2. General construction method notes (important)
This part of the annotation contains @ NoArgsConstructor parameterless constructs, @ AllArgsConstructor all parameter constructs, @ EqualsAndHashCode, @ ToString
@NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode @ToString static class ConstructorBean { private String name1; private String name2; } // The usage is as follows public static void ConstructorExample() { log.info(new ConstructorBean("1", "2").toString()); }
The generated core code is as follows
static class ConstructorBean { private String name1; private String name2; public ConstructorBean() { } public ConstructorBean(final String name1, final String name2) { this.name1 = name1; this.name2 = name2; } public boolean equals(final Object o) { //.... } protected boolean canEqual(final Object other) { return other instanceof LombokExample.ConstructorBean; } public int hashCode() { int PRIME = true; int result = 1; Object $name1 = this.name1; int result = result * 59 + ($name1 == null ? 43 : $name1.hashCode()); Object $name2 = this.name2; result = result * 59 + ($name2 == null ? 43 : $name2.hashCode()); return result; } public String toString() { return "LombokExample.ConstructorBean(name1=" + this.name1 + ", name2=" + this.name2 + ")"; } }
2.4.build construction method (important)
This part of the annotation contains @ Builder
@Builder @ToString static class BuilderBean { private String name1; private String name2; } // Use as follows public static void BuilderExample() { log.info(BuilderBean.builder().name1("1").name2("2").build().toString()); }
The generated core code is as follows
static class BuilderBean { private String name1; private String name2; BuilderBean(final String name1, final String name2) { this.name1 = name1; this.name2 = name2; } public static LombokExample.BuilderBean.BuilderBeanBuilder builder() { return new LombokExample.BuilderBean.BuilderBeanBuilder(); } public String toString() { return "LombokExample.BuilderBean(name1=" + this.name1 + ", name2=" + this.name2 + ")"; } public static class BuilderBeanBuilder { private String name1; private String name2; BuilderBeanBuilder() { } public LombokExample.BuilderBean.BuilderBeanBuilder name1(final String name1) { this.name1 = name1; return this; } public LombokExample.BuilderBean.BuilderBeanBuilder name2(final String name2) { this.name2 = name2; return this; } public LombokExample.BuilderBean build() { return new LombokExample.BuilderBean(this.name1, this.name2); } public String toString() { return "LombokExample.BuilderBean.BuilderBeanBuilder(name1=" + this.name1 + ", name2=" + this.name2 + ")"; } } }
2.5. Chain construction method
This part of the annotation contains @ Accessors, but because the return value of the set method is rewritten, it is sometimes incompatible with other bean tool classes and is generally not recommended
@Accessors(chain = true) @Setter @ToString static class ChainBean { private String name1; private String name2; } // Use as follows public static void ChainExample() { log.info(new ChainBean().setName1("1").setName2("2").toString()); }
The generated core code is as follows
static class ChainBean { private String name1; private String name2; ChainBean() { } public LombokExample.ChainBean setName1(final String name1) { this.name1 = name1; return this; } public LombokExample.ChainBean setName2(final String name2) { this.name2 = name2; return this; } ... }
2.6. Log comments (important)
This part of the annotation contains @ Slf4j, and other annotations are not important. This will be automatically selected according to the import package
@Slf4j public class LombokExample { public static void main(String[] args) { log.info("123"); } }
The generated core code is as follows
private static final Logger log = LoggerFactory.getLogger(LombokExample.class);
2.7. Close flow
@Cleanup, which can help close the flow. Note that IO exceptions need to be caught. Although it's good, it's not used much with the writing method of try with.
public static void CloseExample() throws FileNotFoundException { try { @Cleanup FileInputStream fileInputStream = new FileInputStream(""); } catch (IOException e) { } }
The generated core code is as follows
You can see that it will help us close. However, it is not recommended in finally
public static void CloseExample() throws FileNotFoundException { try { FileInputStream fileInputStream = new FileInputStream(""); if (Collections.singletonList(fileInputStream).get(0) != null) { fileInputStream.close(); } } catch (IOException var1) { } }
Try with resources is recommended here
public static void CloseExample2() { try (FileInputStream fileInputStream = new FileInputStream("")) { log.info("123"); } catch (IOException e) { } }
The generated core code is as follows
You can see that the flow is closed in finally, and various judgments are very comprehensive
public static void CloseExample2() { try { FileInputStream fileInputStream = new FileInputStream(""); Throwable var1 = null; try { log.info("123"); } catch (Throwable var11) { var1 = var11; throw var11; } finally { if (fileInputStream != null) { if (var1 != null) { try { fileInputStream.close(); } catch (Throwable var10) { var1.addSuppressed(var10); } } else { fileInputStream.close(); } } } } catch (IOException var13) { } }
2.8. Exception annotation
It is easy to use when capturing compile time exceptions, but now more and more tool classes have caught compile time exceptions, and there are not many opportunities for it
@SneakyThrows({IOException.class}) public static void ExceptionExample() { CloseExample(); }
The generated core code is as follows
The only function is to capture compile time exceptions without writing them manually
public static void ExceptionExample() { try { CloseExample(); } catch (IOException var1) { throw var1; } }
3.delombok
If you don't want to use annotations later, you need to use delombok Official website
- Method 1
In the new version of idea, refactor - > delombok has integrated delombok, which is convenient to restore directly
- Method 2
java -jar lombok.jar delombok src -d src-delomboked
Note the replacement package name