lombok depth practice

Official website video

Official website address: https: //projectlombok.org

Official website home page video demonstrates how to use the 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.

translation:

Lombok project is a java library that can be automatically installed by means of plug-in to your editor and build tools, the Java code to your taste, no longer have to write getter and equals methods, and through annotation on a class you has the characteristics builder automatically declares a variable log, there's more;

Feature List

val

Free to use final local variables;
from the introduction of version 0.10
you can use the val declared as local variables, instead of writing it's actual type. If you do this, the type will be inferred from the initialization expression, local variable will be set to the final, this feature works in the following scenarios:
1. Local variables;
2.foreach cycle;
not in the members of the class takes effect, initialization expression is needed.
val is actually a type of ordering, as there is a real class in lombok package, you must introduce val to work (or use lombok.val as the type), the local variable declaration in the presence of this type will trigger two operations:
1. increase the final keyword;
2. copy type initializer expressions, to rewrite the type of counterfeit val;
note that this feature does not work in the netbeans;

Precautions:

For the type of mixing will be extrapolated common parent, rather than shared interface.
In ambiguous scenarios: if the initialization is null, it will be extrapolated Object.

Example Code: NA 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();

    }

}

Use 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();

    }
}

where

Alternative local variables;

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

Automatic inspection, bid farewell to the NPE both good and bad.

Do not use 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);
    }

}

Use 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);
    }


}

The same effect:

/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

Code path: [email protected]: carterbrother / springbootpractice.git / lombok-demo

The original is not easy, please indicate the source.

Guess you like

Origin www.cnblogs.com/snidget/p/11650423.html