本篇文章会讲到:@NonNull、@Cleanup、@UtilityClass、@Log、@SneakyThrows、@Synchronized 注解使用
1、@NonNull使用
-
注解在属性上,标识属性是不能为空,为空则抛出异常。
import lombok.NonNull;
public class Example {
1private String name; 2 3public Example(@NonNull User user) { 4 this.name = user.getName(); 5}}
编译后:
1public class Example { 2 3 private String name; 4 5 public Example(User user) { 6 if (user == null) { 7 throw new NullPointerException("user"); 8 } 9 this.name = user.getName(); 10 } 11 12}
2、@Cleanup使用
-
关闭并释放资源,可以用在 IO 流上
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); } } }
编译后:
1import java.io.*; 2 3public class CleanupExample { 4 public static void main(String[] args) throws IOException { 5 InputStream in = new FileInputStream(args[0]); 6 try { 7 OutputStream out = new FileOutputStream(args[1]); 8 try { 9 byte[] b = new byte[10000]; 10 while (true) { 11 int r = in.read(b); 12 if (r == -1) break; 13 out.write(b, 0, r); 14 } 15 } finally { 16 if (out != null) { 17 out.close(); 18 } 19 } 20 } finally { 21 if (in != null) { 22 in.close(); 23 } 24 } 25 } 26}
3、@UtilityClass使用
-
@UtilityClass官方文档地址
-
注解在类似,所有成员变量和内部类都为标准static静态的。
import lombok.experimental.UtilityClass;
@UtilityClass public class UtilityClassExample { private final int CONSTANT = 5;
public int addSomething(int in) { return in + CONSTANT; } }
编译为:
1public final class UtilityClassExample { 2 private static final int CONSTANT = 5; 3 4 private UtilityClassExample() { 5 throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated"); 6 } 7 8 public static int addSomething(int in) { 9 return in + CONSTANT; 10 } 11}
4、@Log使用
-
该注解用在类上,可以省去从日志工厂生成日志对象这一步,直接进行日志记录。
import lombok.extern.java.Log;
@Log public class Example {
1public static void main(String[] args) { 2 log.severe("Something's wrong here"); 3}}
编译后:
1public class Example { 2 3 private static final java.util.logging.Logger log = 4 java.util.logging.Logger.getLogger(Example.class.getName()); 5 6 public static void main(String... args) { 7 log.severe("Something's wrong here"); 8 } 9 10}
我们也可以在注解中使用 topic 来指定生成 log 对象时的类名。
1@CommonsLog 2private static final org.apache.commons.logging.Log log = 3 org.apache.commons.logging.LogFactory.getLog(LogExample.class); 4 5@JBossLog 6private static final org.jboss.logging.Logger log = 7 org.jboss.logging.Logger.getLogger(LogExample.class); 8 9@Log 10private static final java.util.logging.Logger log = 11 java.util.logging.Logger.getLogger(LogExample.class.getName()); 12 13@Log4j 14private static final org.apache.log4j.Logger log = 15 org.apache.log4j.Logger.getLogger(LogExample.class); 16 17@Log4j2 18private static final org.apache.logging.log4j.Logger log = 19 org.apache.logging.log4j.LogManager.getLogger(LogExample.class); 20 21@Slf4j 22private static final org.slf4j.Logger log = 23 org.slf4j.LoggerFactory.getLogger(LogExample.class); 24 25@XSlf4j 26private static final org.slf4j.ext.XLogger log = 27 org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
5、@SneakyThrows使用
-
该注解用在方法上,可以将方法中的代码用 try-catch 语句包裹起来,捕获异常并在 catch 中用 Lombok.sneakyThrow(e) 把异常抛出。
-
也可以使用 @SneakyThrows(Exception.class) 的形式指定抛出哪种异常。
import lombok.SneakyThrows;
public class SneakyThrowsExample implements Runnable { @SneakyThrows(UnsupportedEncodingException.class) public String utf8ToString(byte[] bytes) { return new String(bytes, "UTF-8"); }
@SneakyThrows public void run() { throw new Throwable(); } }
编译后:
1import lombok.Lombok; 2 3public class SneakyThrowsExample implements Runnable { 4 public String utf8ToString(byte[] bytes) { 5 try { 6 return new String(bytes, "UTF-8"); 7 } catch (UnsupportedEncodingException e) { 8 throw Lombok.sneakyThrow(e); 9 } 10 } 11 12 public void run() { 13 try { 14 throw new Throwable(); 15 } catch (Throwable t) { 16 throw Lombok.sneakyThrow(t); 17 } 18 } 19}
6、@Synchronized使用
-
该注解用在类方法或者实例方法上,效果和 synchronized 关键字相同,区别在于锁对象不同。
-
synchronized 关键字的锁对象分别是“类的 class 对象”和“this 对象”。
-
@Synchronized 的锁对象分别是“私有静态 final 对象 lock”和“私有 final 对象 lock”。当然,也可以自己指定锁对象。
import lombok.Synchronized;
public class SynchronizedExample { private final Object readLock = new Object();
@Synchronized public static void hello() { System.out.println("world"); }
@Synchronized public int answerToLife() { return 42; }
@Synchronized("readLock") public void foo() { System.out.println("bar"); } }
编译后:
1public class SynchronizedExample { 2 private static final Object $LOCK = new Object[0]; 3 private final Object $lock = new Object[0]; 4 private final Object readLock = new Object(); 5 6 public static void hello() { 7 synchronized($LOCK) { 8 System.out.println("world"); 9 } 10 } 11 12 public int answerToLife() { 13 synchronized($lock) { 14 return 42; 15 } 16 } 17 18 public void foo() { 19 synchronized(readLock) { 20 System.out.println("bar"); 21 } 22 } 23}