李福春 · 2019年11月12日

lombok深入实践

官网视频

官网地址:https://projectlombok.org

官网的首页视频演示在eclipse中如何使用Lombok;

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

翻译:

Lombok项目是一个java的库,可以自动的通过插件的方式安装到你的编辑器和构建工具中,给你的Java代码调味,再也不用写 getter和equals方法了,通过一个类上的注解你拥有了建造者的特征,自动的声明了一个日志变量,还有更多功能;

特征列表

val

自由使用final本地变量;
从0.10版本开始引入
你可以使用val作为本地变量的申明,来替代写它的实际类型。如果你这样做了,类型将会从初始化表达式中被推断,本地变量也会被设置为final,这个特征工作在如下场景:
1.本地变量;
2.foreach循环;
在类的成员中并不会生效,初始化表达式是需要的。
val实际上是一个类型的排序,作为一个真实的类存在于lombok的包中,你必须引入val才能工作(或者使用lombok.val作为类型),在本地变量声明中这种类型的存在会触发两个操作:
1.增加final关键字;
2.复制初始化表达式的类型,用来重写这个伪造的val类型;
注意这个功能在netbeans中不会起作用;

注意事项:

对于混合的类型,会推断得到公共的父类,而不是共享的接口。
在含糊的场景下:如果初始化为null,会推断得到Object.

实例代码:不适用lombok;

package com.springpractice._val;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Hello world!
 */
public class App {

    public String test1() {
        final ArrayList<String> exampleList = new ArrayList<>();
        exampleList.add("hello world");

        final String firstElement = exampleList.get(0);
        final String returnString = firstElement.toLowerCase();
        System.out.println(returnString);
        return returnString;
    }

    public void test2() {

        final HashMap<Integer, String> map = new HashMap<>();
        map.put(0, "zero");
        map.put(5, "five");

        for (final Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.printf("%d: %s \n", entry.getKey(), entry.getValue());
        }

    }


    public static void main(String[] args) {
        final App app = new App();
        app.test1();
        System.out.println("======");
        app.test2();

    }

}

使用lombok ;

package com.springpractice._val;

import lombok.val;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Hello world!
 */
public class AppLombok {
         //不能使用在类的成员变量上,会报编译错误
            // val String name;
    public String test1() {
        val exampleList = new ArrayList<String>();
        exampleList.add("hello world");

        val firstElement = exampleList.get(0);

        final String returnString = firstElement.toLowerCase();
        System.out.println(returnString);
        return returnString;
    }

    public void test2() {

        val map = new HashMap<Integer, String>();
        map.put(0, "zero");
        map.put(5, "five");

        for (val entry : map.entrySet()) {
            System.out.printf("%d: %s \n", entry.getKey(), entry.getValue());
        }

    }


    public static void main(String[] args) {
        final AppLombok app = new AppLombok();
        app.test1();
        System.out.println("======");
        app.test2();

    }
}

var

替代本地变量;
package com.springpractice._var;

import lombok.var;

import java.awt.*;

/**
 * 说明:var的使用
 * @author carter
 * 创建时间: 2019年10月10日 20:08
 **/

public class App {

    public static void main(String[] args) {

        var x = "Hello";

        //编译报错,类型是String
//        x=1;

        var y = Color.RED;

//        y=x;

        //var的类型如果是object ,则不是它的使用场景
        var z = new Object();

        z = x;

        System.out.println(x);

    }

}

NonNull

自动检查,告别NPE的爱恨情仇。

不使用lombok;

package com.springpractice.nonnull;

import lombok.NonNull;

import java.util.Objects;

/**
 * 说明:TODO
 * @author carter
 * 创建时间: 2019年10月10日 20:16
 **/

public class App  extends Something{

    private String name;

    public App(Person person){
        super("Hello");
        if (Objects.isNull(person)){
            throw new NullPointerException("person is marked @NonNull but is null");
        }
        this.name = person.getName();

    }

    public static void main(String[] args) {
        new App(null);
    }

}

使用lombok;

package com.springpractice.nonnull;

import lombok.NonNull;

/**
 * 说明:TODO
 * @author carter
 * 创建时间: 2019年10月10日 20:20
 **/

public class AppLombok extends Something {

    private String name;

    public AppLombok(@NonNull Person person){
        super("Hello");
        this.name = person.getName();
    }

    public static void main(String[] args) {
        new AppLombok(null);
    }


}

效果一样:

/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/bin/java -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/tools.jar:/Users/lifesense-szyf01/src/git/github/springbootpractice/lombok-demo/target/classes:/Users/lifesense-szyf01/.m2/repository/org/projectlombok/lombok/1.18.8/lombok-1.18.8.jar com.springpractice.nonnull.AppLombok
Exception in thread "main" java.lang.NullPointerException: person is marked non-null but is null
    at com.springpractice.nonnull.AppLombok.<init>(AppLombok.java:15)
    at com.springpractice.nonnull.AppLombok.main(AppLombok.java:21)

Process finished with exit code 1

代码路径: git@github.com:carterbrother/springbootpractice.git/lombok-demo

原创不易,转载请注明出处。
推荐阅读
关注数
0
文章数
53
爱技术,爱编码,爱生活!
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息