Official website video
Official address: https://projectlombok.org
The home video of the official website demonstrates how to use Lombok in eclipse.
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, which can be installed into your editor and build tools automatically through plug-ins, to flavor your Java code, no longer need to write getter and equals methods, through a class annotation you have the characteristics of the builder, automatically declare a log variable, and more functions;
Feature list
val
Free use of final local variables; Starting with version 0.10 You can use val as a declaration of a local variable instead of the actual type of writing it. If you do this, the type will be inferred from the initialization expression and the local variable will be set to final. This feature works in the following scenarios: 1. Local variables; 2.foreach cycle; It does not take effect in class members, and initialization of expressions is required. val is actually a sort of type. As a real class, it exists in the package of lombok. You have to introduce val to work (or use lombok.val as a type). The existence of this type in the local variable declaration triggers two operations: 1. Add final keyword; 2. Copy the type of the initialization expression to rewrite the forged val type. Note that this function does not work in netbeans;
Matters needing attention:
For mixed types, a common parent class is inferred, rather than a shared interface. In an ambiguous scenario: Object is inferred if initialized to null.
Example code: lombok is not applicable;
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(); } }
Using lombok;
package com.springpractice._val; import lombok.val; import java.util.ArrayList; import java.util.HashMap; /** * Hello world! */ public class AppLombok { //Cannot be used on class member variables, compilation errors will be reported // 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
Substitute local variables;
package com.springpractice._var; import lombok.var; import java.awt.*; /** * Note: Use of var * @author carter * Creation time: October 10, 2019, 20:08 **/ public class App { public static void main(String[] args) { var x = "Hello"; //Compilation error, type String // x=1; var y = Color.RED; // y=x; //If the type of var is object, it is not its usage scenario var z = new Object(); z = x; System.out.println(x); } }
NonNull
Automatically check and say goodbye to NPE's love and hatred.
lombok is not used;
package com.springpractice.nonnull; import lombok.NonNull; import java.util.Objects; /** * Note: TODO * @author carter * Creation time: October 10, 2019 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; /** * Note: TODO * @author carter * Creation time: October 10, 2019 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 effect is the same:
/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: git@github.com:carterbrother/springbootpractice.git/lombok-demo
The original is not easy to reproduce, please indicate the source.