Lombok介绍及使用方法

  最近看Disconf源码的时候,发现很多地方报错,字段的get方法获取不到,类名前面都加了个@Data的注解,百度了一下这个注解,进一步了解到Lombok的功能,真是相见恨晚啊,一直都有这样的想法,终于不经意被看到了。看来后面还是要多看看别人的源码啊

  这里把里面最常用的功能记下来,更多的内容可以到官网上去查查。地址如下:

    lombok的官方地址:https://projectlombok.org/

    lombok的Github地址:https://github.com/rzwitserloot/lombok

  Idea下安装方法参照我写的另一篇文章:http://www.cnblogs.com/aligege/p/7797642.html

 

几个比较常用的注解:

1、@NonNull: 可以帮助我们避免空指针。

使用lombok:

import lombok.NonNull;
    public class NonNullExample extends Something {
        private String name;  
        public NonNullExample(@NonNull Person person) {
        super("Hello");
        this.name = person.getName();
    }
}

不使用lombok:

public class NonNullExample extends Something {
    private String name;  
    public NonNullExample(@NonNull Person person) {
        super("Hello");
        if (person == null) {
            throw new NullPointerException("person");
        }
        this.name = person.getName();
    }
}

 

2、@Cleanup: 自动帮我们调用close()方法。

使用lombok:

import lombok.Cleanup;
import java.io.*;
public class CleanupExample {
    public static void main(String[] args) throws IOException {
        @Cleanup InputStream in = new FileInputStream(args[0]);
        @Cleanup OutputStream out = new FileOutputStream(args[1]);
        byte[] b = new byte[10000];
        while (true) {
            int r = in.read(b);
            if (r == -1) break;
            out.write(b, 0, r);
        }
    }
}

不使用lombok:

import java.io.*;
    public class CleanupExample {
        public static void main(String[] args) throws IOException {
            InputStream in = new FileInputStream(args[0]);
            try {
                OutputStream out = new FileOutputStream(args[1]);
                try {
                    byte[] b = new byte[10000];
                    while (true) {
                    int r = in.read(b);
                    if (r == -1) break;
                    out.write(b, 0, r);
                    }
                } finally {
                    if (out != null) {
                        out.close();
                    }
                }
            } finally {
                if (in != null) {
                in.close();
            }
        }
    }
}

 

3、@Getter / @Setter: 自动生成Getter/Setter方法

使用lombok:

import lombok.AccessLevel;
    import lombok.Getter;
    import lombok.Setter;
    public class GetterSetterExample {
        @Getter @Setter private int age = 10;
        @Setter(AccessLevel.PROTECTED) private String name;
    }

 

不使用lombok:

public class GetterSetterExample {
    private int age = 10;
    private String name;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    protected void setName(String name) {
        this.name = name;
    }
}

@NoArgsConstructor: 自动生成无参数构造函数。

@AllArgsConstructor: 自动生成全参数构造函数。

@Data: 自动为所有字段添加@ToString, @EqualsAndHashCode, @Getter方法,为非final字段添加@Setter,和@RequiredArgsConstructor!

 

posted @ 2017-11-07 09:57  阿狸哥哥  阅读(714)  评论(0编辑  收藏  举报