Introduction to lombok + Examples

The official address of ombok: https://projectlombok.org/

Github address of lombok: https://github.com/rzwitserloot/lombok

 

    lombok is a tool that can help us simplify and eliminate some must-have but bloated Java code in the form of simple annotations. In simple terms, for example, we create a new class, and then write a few fields in it, and then usually In this case, we need to manually create getter and setter methods, constructors, etc. The function of lombok is to save us the trouble of manually creating these codes. It can automatically generate these methods for us when we compile the source code.

 

    The effect that lombok can achieve is that there is no need to write some general methods in the source code, but it will help us generate these methods in the compiled bytecode file, which is the magical effect of lombok.

 

    Although some people may say that IDEs have built-in functions to automatically generate these methods, but using lombok will make your code look more concise and easier to write.

 

@Data example

 

@Getter @Setter Example

Lombok annotated code:

@Getter @Setter private boolean employed = true;
@Setter(AccessLevel.PROTECTED) private String name;

 Equivalent Java source code:

private boolean employed = true;
private String name;

public boolean isEmployed() {
    return employed;
}

public void setEmployed(final boolean employed) {
    this.employed = employed;
}

protected void setName(final String name) {
    this.name = name;
}

 

@ToString example

Lombok annotated code:

@ToString(callSuper=true,exclude="someExcludedField")
public class Foo extends Bar {
    private boolean someBoolean = true;
    private String someStringField;
    private float someExcludedField;
}

 

Equivalent Java source code:

public class Foo extends Bar {
    private boolean someBoolean = true;
    private String someStringField;
    private float someExcludedField;
    
    @ java.lang.Override
    public java.lang.String toString() {
        return "Foo(super=" + super.toString() +
            ", someBoolean=" + someBoolean +
            ", someStringField=" + someStringField + ")";
    }
}

 

@Cleanup example

Lombok annotated code:

public void testCleanUp() {
    try {
        @Cleanup ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(new byte[] {'Y','e','s'});
        System.out.println(baos.toString());
    } catch (IOException e) {
        e.printStackTrace ();
    }
}

 

Equivalent Java source code:

public void testCleanUp() {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            baos.write(new byte[]{'Y', 'e', 's'});
            System.out.println(baos.toString());
        } finally {
            baos.close();
        }
    } catch (IOException e) {
        e.printStackTrace ();
    }
}

 

@Synchronized example

Lombok annotated code:

private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");

@Synchronized
public String synchronizedFormat(Date date) {
    return format.format(date);
}

 

Equivalent Java source code:

private final java.lang.Object $lock = new java.lang.Object[0];
private DateFormat format = new SimpleDateFormat("MM-dd-YYYY");

public String synchronizedFormat(Date date) {
    synchronized ($lock) {
        return format.format(date);
    }
}

 

For more examples and usage, please refer to the official website.

 

 refer to:

    http://www.cnblogs.com/holten/p/5729226.html

    http://www.cnblogs.com/lexiaofei/p/8422463.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326012824&siteId=291194637