Home  >  Article  >  Java  >  What annotations are there for Lombok in java?

What annotations are there for Lombok in java?

王林
王林forward
2023-04-30 15:52:061449browse

Annotation examples

1. @ToString: implement toString() method

2.@Data: annotated on the class; provides getting and getting of all attributes of the class setting method, in addition to providing equals, canEqual, hashCode, and toString methods

3, @Setter: annotated on attributes; providing setting methods for attributes. @Getter: Annotated on the attribute; provides a getting method for the attribute

@Log4j: Annotated on the class; provides a log4j log object with an attribute named log for the class

@NoArgsConstructor: Annotated on On the class; provide a no-parameter constructor for the class

@AllArgsConstructor: annotated on the class; provide a full-parameter constructor for the class

@Cleanup: close the stream

@ToString: Implement toString() method

@EqualsAndHashCode: Implement equals() method and hashCode() method

@Synchronized: Object synchronization

@SneakyThrows: Throw Exception

Example

Cleanup annotation is used before a variable to ensure that the resource represented by the variable is automatically closed. The default is to call the resource with close().

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[1024];
     while (true) {
       int r = in.read(b);
       if (r == -1) break;
       out.write(b, 0, r);
     }
 }

The above is the detailed content of What annotations are there for Lombok in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete