1. What is Lombok
-
Official website: https://projectlombok.org/
-
Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
-
Colloquialism: Lombok is a kind of Java ™ Utility tools can be used to help developers eliminate verbose code in Java, especially for simple Java objects (POJO s), which achieve this through annotations.
-
Simplify JavaBean development
<dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> </dependency>
- Search and install lombok plug-in in idea
- Lombok @Data annotation
- Press the shortcut key Alt+7 to display all methods and global constants in the current class. The method also includes formal parameters and return values
- Or view -- > tool windows -- > structure -- > Click
2. Lombok principle
- JSR 269: Pluggable Annotation Processing API
- Official website: https://www.jcp.org/en/jsr/detail?id=269 JDK6 provides features that use annotations during Javac compilation
- View the compiled user class
- Principle implementation flow chart
3. Common notes of Lombok
1. @Getter And @ Setter method
-
Test @Setter annotation user java
User.class
-
Test @ Getter(AccessLevel.PROTECTED) annotation. The default is public. You can set the level of different methods
-
Add set and get methods to all variables and annotate them directly in the class
-
be careful:
- lombok will not generate set and get methods for static modified variables
- lombok will not generate set method for final modified characters
-
Custom variables do not generate set and get methods
2. @ToString method
- Custom ToString method to exclude some fields
3. @EqualsAndHashCode
- Customize the EqualsAndHashCode method that excludes some fields
4. @NonNull
- Function: the function of annotation is to make a non empty judgment in the method
- Case:
public void test(@NonNull String s){ System.out.println(s); } public static void main(String[] args) { User user = new User(); user.test(null); }
- Result: null pointer
- View compiled source code:
public void test(@NonNull String s) { if (s == null) { throw new NullPointerException("s is marked non-null but is null"); } else { System.out.println(s); } }
- It can also be used in the constructor or annotated in the field
@NotNull private Integer id;
5. @NoArgsConstructor, @ RequiredArgsConstructor, @ AllArgsConstructor
-
@NoArgsConstructor, add parameterless construction method
-
@RequiredArgsConstructor, which specifies part of the parameterized construction. By default, it will find the variable with @ NonNull to add the construction method, or the variable with final modification and no initial value to add the construction method
-
@AllArgsConstructor, which generates the construction method of all member variables
6. @Data
- Acting on a class, it is a collection of the following annotations: @ ToString, @ EqualsAndHashCode, @ Getter, @ Setter @RequiredArgsConstructor
7. @Builder
- Construct a series of methods with the field name as the method name, and the object returned by the method is UserBuilder
8. @Log
- @Log: acts on a class to generate log variables. There are different annotations for different log implementations:
9. val
HashMap<string,string> map = new HashMap<string,string>();
Simplified writing
val map = new HashMap<string,string>();
public static void main(String[] args) { val sets = new HashSet<string>(); val lists = new ArrayList<string>(); val maps = new HashMap<string, string>(); //=> Equivalent to the following final Set<string> sets2 = new HashSet<>(); final List<string> lists2 = new ArrayList<>(); final Map<string, string> maps2 = new HashMap<>(); }
10. @Cleanup
- It is used to close and release resources and can be used on IO streams;
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); } } }
- The above is equivalent to the following traditional Java code:
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(); } } } }
4. Summary
</string,></string></string></string,></string></string></string,string></string,string></string,string>