preface

I used to be a.NET developer, and now I’m a Java developer. As I enjoyed the mature Java ecosystem, I often missed c#’s concise syntax: automatic attributes, type inference, automatic initializers….

Fish, I want, bear PAWS also I want; You can’t have both;

Until I met Lombok.

Add dependencies and IDE plug-ins

Maven adds dependencies:

< the dependency > < groupId > org. Projectlombok < / groupId > < artifactId > lombok - maven < / artifactId > < version > 1.16.14.0 < / version > </dependency>Copy the code

Because Lombok changes bytecode generation by manipulating the AST(abstract syntax tree) at compiler compile time, you need to add plug-ins to the IDE to enable Lombok. Here’s how Idea adds plug-ins:

use

“Automatic Java properties” — @Getter & @Setter

Java programmers inevitably create a lot of POJOs in their projects, and the most lines of code in poJOs are getters and setters that we don’t care about. Although the IDE helps us generate code, long code is still a pain in the ass. See how Lombok helps us simplify code:

public class User {
    @Getter
    @Setter
    private String name;
    @Getter(AccessLevel.PROTECTED)
    @Setter(AccessLevel.PROTECTED)
    private Integer age;
}Copy the code

So @getter and @setter can annotate on a class and a property, and if you put it on a class, it will generate Getter and Setter methods for all non-static properties, and if you put an annotation on a property, it will generate Getter and Setter methods for that property.

AccessLevel can be used to set the AccessLevel of Getter&Setter methods.

“Type inference in Java” — @val

Is it strange why lombok uses val instead of var like c#? Because Lombok thought that Java might later add the keyword var to support type inference, the truth is….

   @Test
    public void valTest() {
        val user = getUser();
        user.setName("javaNoob");
        System.out.println(user.getName());
    }

    private User getUser(){
        return  new User();
    }Copy the code

Java without an “automatic initializer” can only use constructors — @noargsconstructor, @requiredargsconstructor, @allargsconstructor

@NoArgsConstructor @AllArgsConstructor @RequiredArgsConstructor public class User { @Getter @Setter private String name;  @Getter(AccessLevel.PROTECTED) @Setter(AccessLevel.PROTECTED) private Integer age; }Copy the code

As the name suggests: NoArgsConstructor generates the no-argument constructor, AllArgsConstructor generates the all-argument constructor, and RequiredArgsConstructor generates the @nonNULL annotation field constructor.

@nonNULL automatically generates a non-null check

Java programmers should be familiar with NullPointerException. The @nonNULL annotation helps us generate Null checks.

    @NonNull
    private String name;
    @Test
    public void valTest() {
        val user = getUser();
        user.setName(null);
        System.out.println(user.getName());
    }
    /**
    java.lang.NullPointerException: name
    at com.github.javanoob.lombok.User.setName(User.java:11)
    */Copy the code

The equivalent of

private void setName(String name) { if (name==null){ throw new NullPointerException("name"); }}Copy the code

@synchronized turns a method into a Synchronized method

    @Synchronized
    private void syncMethod(){
        System.out.println("I am synchronized");
    }Copy the code

The equivalent of

private static final Object $LOCK = new Object[0] ; private void syncMethod(){ synchronized($LOCK) { System.out.println("I am synchronized"); }}Copy the code

other

@toString generates ToString methods; @equalsandHashCode helps you generate the equal() and hashCode() methods. @log generates Log code for you.

There are also some silly @cleanup that can be completely replaced with try-with-resource, and @sneakythrows that has no usage scenarios.

conclusion

I wonder when writing Java code can be as fun as C#

The relevant data

  1. Website Feature List

  2. Making lombok source

  3. Lombok principle analysis