Lombok is used to simplify unnecessary Java code in the project.

Source: Internet
Author: User

Lombok is used to simplify unnecessary Java code in the project.

When writing and using Java, some template code will inevitably need to be written. Otherwise, get/set, toString, hashCode, close resource, define constructor, and so on. The code will appear redundant and long. The Lombok project can be used to get rid of these things. With a series of annotations, Lombok can help us automatically generate these functions.

Lombok address: https://projectlombok.org/

Reference: https://projectlombok.org/features/index.html

1.Install

Download lombok. jar from the official website, double-click it, and follow the prompts to install it successfully in eclipse.

If maven is used, dependencies must be introduced:

    <dependency>         <groupId>org.projectlombok</groupId>         <artifactId>lombok</artifactId>         <version>1.16.4</version>         <scope>provided</scope>     </dependency>

If you need to use javac or other command tools to compile java classes, you need to put lombok. jar into classpath.

2.Usage (Document: https://projectlombok.org/features/index.html)

1> @ Getter/@ Setter, annotation in a pojo class, will automatically generate the get/set function during compilation.

2> @ ToString Annotation on the class. during compilation, the toString function including all fields is generated;

3> @ resolve sandhashcode: during compilation, the equlas and hashCode functions are generated;

4> @ Cleanup: Annotations can automatically call the close () functions of some resource objects;This is very helpful;

5> @ NoArgsContructor, @ RequireArgsContructor, @ AllArgsContructor, which helps us to generate non-parameter constructors, each non-Null field constructor, and all field parameter constructors;

6> @ Data, All together now: A closed cut@ToString,@EqualsAndHashCode,@GetterOn all fields, and@SetterOn all non-final fields, and@RequiredArgsConstructor! (Equivalent:@ToString,@EqualsAndHashCode,@Getter,@Setter,@RequiredArgsConstructor)

For more annotations, see https://projectlombok.org/features/index.html

3.Example

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Test { private int id; private String name; private String password; public static void main(String[] args) { Test test = new Test(1, "test", "password"); System.out.println(test); System.out.println(test.getName()); }}

Result:

Test(id=1, name=test, password=password)test

Through the @ Data, @ AllArgsConstructor, and @ NoArgsConstructor annotations, the constructors of Test's full field parameters are automatically generated, and toString () and get/set functions are automatically generated.

Let's look at another example:

    public static void main(String[] args) throws IOException{        @Cleanup        InputStream in = new FileInputStream("/home/a.txt");        @Cleanup        OutputStream out = new FileOutputStream("/home/b.txt");        byte[] b = new byte[10000];        while (true) {            int r = in.read(b);            if (r == -1)                break;            out.write(b, 0, r);        }    }

@ Cleanup automatically helps us call the close () method to close resources.

You can use@CleanupTo ensure a given resource is automatically cleaned up before the code execution path exits your current scope. You do this by annotating any local variable declaration with@CleanupAnnotation like so:
@Cleanup InputStream in = new FileInputStream("some/file");
As a result, at the end of the scope you're in,in.close()Is called. This call is guaranteed to run by way of atry/finally construct.

If the type of object you 'd like to cleanup does not haveclose()Method, but some other no-argument method, you canspecify the name of this method like so:
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);
By default, the cleanup method is presumed to beclose(). A cleanup method that takes 1 or more argumentsCannotBe called@Cleanup.

@ Cleanup is implemented through try/finally. If the resource close method is not the default close (), you can also specify the name of the close method @ Cleanup ("closeMethod "), however, the close method cannot have parameters, or @ Cleanup cannot be used.

More references https://projectlombok.org/features/index.html

By using Lombok, you can reduce a lot of Java code and reduce the psychological burden.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.