Home > Java > javaTutorial > body text

What annotations are there for Lombok in java?

王林
Release: 2023-04-30 15:52:06
forward
1480 people have browsed it

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);
     }
 }
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template