Sunday, July 12, 2020

How Lombok saves developers' time

Nowadays, all the cool kids use lombok to make beautiful java code. So let's talk about it.

Project Lombok is a java library that automatically plugs into your editor and build tools to generate boilerplate code such as class constructors, field getter and setter, hashCode etc.

In order to use it, set lombok as one of the maven dependencies.

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

With the dependency, a lombok.jar will be introduced in the project compilation classpath. The lombok.jar contains a file named /META-INF/services/javax.annotation.processing.Processor. When javac sees this file in a compilation classpath, it runs annotation processors defined there during compilation. As a result, the lombok annotations such as @Getter is replaced by java getter code blocks. As you can see, lombok is a jar that works at compilation time.

The javac can understand the lombok annotations doesn't mean your IDE can understand them. Eclipse, for example, need to have a the lombok.jar registered, so that it can use it to compile your code before running it. The Eclipse installation is easy, lombok.jar did that for you. You just have to download the jar, run it with
java -jar lombok.jar

the rest of the work is handled by the jar. It scan your computer to find the eclipse and install itself into your chosen eclipse programs.

Now you can write code such as
package com.example.my.mydemo.dao;
import lombok.Getter;
import lombok.Setter;

@Setter @Getter
public class Client {
    private String name;
    private String email;
    private long id;
}

With the lombok annotation @Getter @Setter, javac or eclipse treat them as equivalent to the following code

package com.example.my.mydemo.dao;
public class Client {
    private String name;
    private String email;
    private long id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}

So, when other java code need to access getter and setters, it won't fail.
package com.example.my.mydemo.dao;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.example.my.mydemo.model.Client;

public class ClientDao {
    private final JdbcTemplate jdbcTemplate;
    private static final String CLIENTSELECT = "select name, email, id from "
            + "Client where id = ?";
    public ClientDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    public Client getClient(String clientId) {
        Client client = jdbcTemplate.queryForObject(CLIENTSELECT, new Object[]{clientId},
                new BeanPropertyRowMapper<Client>(Client.class));
        return client;
    }

}

That is easy.

Even better, we can use @Data annotation to get many code for free, it is equivalent to @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode all together.


1 comment:

  1. apple watch 6 titanium 3 pcs slot machine game
    apple watch 6 2020 ford ecosport titanium titanium 3 pcs ceramic or titanium flat iron slot machine game machine ion titanium on brassy hair - apple watch 6 titanium titanium canteen 3 pcs slot machine game machine, apple watch 6 titanium 3 pcs slot oakley titanium sunglasses machine game machine

    ReplyDelete

meta.ai impression

Meta.ai is released by meta yesterday, it is super fast you can generate image while typing! You can ask meta.ai to draw a cat with curvy fu...