Lombok使用3:其他注解

本篇文章会讲到:@NonNull、@Cleanup、@UtilityClass、@Log、@SneakyThrows、@Synchronized 注解使用

1、@NonNull使用

  • @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使用

  • @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使用

  • @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使用

  • @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 关键字相同,区别在于锁对象不同。

  • 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}

更多阅读

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593