lombok深入实践

48次阅读

共计 6296 个字符,预计需要花费 16 分钟才能阅读完成。

官网视频

官网地址: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