Lombok Code Template Tutorial: You Probably Don’t Need to

  • 时间:2020-09-11 08:23:45
  • 分类:网络文摘
  • 阅读:74 次

Java is Verbose. And worse still, there is little Syntax Sugar.

Usually, we want a class that contains Data only. And we need getters and setters to wrap the private field. Also, we need to add the toString, equals, hashCode etc template methods – that are boring.

For example, here is a data class that contains only 1 private string field.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.company;
 
import java.util.Objects;
 
public class HelloACM {
    private String data;
 
    public HelloACM(String data) {
        this.data = data;
    }
 
    public String getData() {
        return data;
    }
 
    public void setData(String data) {
        this.data = data;
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof HelloACM)) return false;
        HelloACM helloACM = (HelloACM) o;
        return Objects.equals(getData(), helloACM.getData());
    }
 
    @Override
    public int hashCode() {
        return Objects.hash(getData());
    }
 
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("HelloACM{");
        sb.append("data='").append(data).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
package com.company;

import java.util.Objects;

public class HelloACM {
    private String data;

    public HelloACM(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof HelloACM)) return false;
        HelloACM helloACM = (HelloACM) o;
        return Objects.equals(getData(), helloACM.getData());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getData());
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("HelloACM{");
        sb.append("data='").append(data).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

You could imagine how this gets verbose if you have several fields to encapsulate in a single class. And these code are just basic – not containing much business logics – the presence of these code make the class less readable.

Using Lombok Library

Project Lombok https://projectlombok.org is a convenient way to solve this problem. All the above Java code can be replaced by the following clean and easy-to-understand code.

1
2
3
4
5
6
package com.company;
 
@Data
public class HelloACM {
    private String data;
}
package com.company;

@Data
public class HelloACM {
    private String data;
}

That is it. You just need to add the @Data before the class definition – and all those boilerplate methods will be provided by the Lombok Library. The code is still clean.

In order for this to work, you will need to download the lombok.jar and provide it in the class path. Alternatively, if you are using IDE such as Intellij, you could download the plugin:

intellij-lombok-plugin Lombok Code Template Tutorial: You Probably Don't Need to Write Getters and Setters in Java java

intellij-lombok-plugin

And the methods are recongnized in the Structure window:

java-lombook-code Lombok Code Template Tutorial: You Probably Don't Need to Write Getters and Setters in Java java

java-lombook-code

Lombok also supports the @Getter and @Setter if you want to make getters and setters for some particular fields, for example:

1
2
3
public class HelloACM {
   private @Getter @Setter String data;
}
public class HelloACM {
   private @Getter @Setter String data;
}

And this should enable you to use getData() and setData() methods.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
枸杞子泡水喝的养生功效及其禁忌  怎么吃大蒜可以最大限度发挥抗癌功效  九个好的饮食习惯有助于你远离癌症  莲藕生吃和熟吃的保健功效不相同  四种酒这样喝可以变保健良药  美食伟哥枸杞怎么吃有助于壮阳  枸杞子食疗配方助电脑族保护眼睛  胡萝卜怎么吃营养最丰富防癌又明目  过量食用生姜有增大患肝癌的风险  长芽了不能吃的食物有哪些? 
评论列表
添加评论