四、Lombok注解使用

2018-09-14  本文已影响189人  maololo

官方文档:https://projectlombok.org/features/all

1、Lombok注解

2、@Data@Value的使用

官方说明:All together now:A shortcut for @ToString, @EqualsAndHashCode,@Getter on all fields,@Setter on all non-final fields, and @RequiredArgsConstructor!

3、@NonNull的使用

// 使用
public String getName(@NonNull Person person) {
        return person.getName;
}
// 不使用
public String getName(Person person) {
        if (person == null) {
            throw new NullPointerException("person");
        }
        return person.getName;
 }

4、@Cleanup自动帮我们调用close()方法

// 使用
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);
        }
}
// 不使用
public static void main(String[] args) throws IOException {
        InputStream in = new FileInputStream(args[0]);
        try {
            OutputStream out = new FileOutputStream(args[1]);
            try {
                byte[] b = new byte[10000];
                while (true) {
                    int r = in.read(b);
                    if (r == -1) break;
                    out.write(b, 0, r);
                }
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
}

5、@ToString@EqualsAndHashCode使用

// 来排除param1和param2两个成员变量
@ToString(exclude={"param1","param2"})
// 来指定使用param1和param2两个成员变量
@ToString(of={"param1","param2"})

6、@NoArgsConstructor@RequiredArgsConstructor@AllArgsConstructor使用

// 使用注解
@RequiredArgsConstructor(staticName = "sunsfan")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor
public class Shape {
    private int x;
    @NonNull
    private double y;
    @NonNull
    private String name;
}

// 不使用注解
public class Shape {
    private int x;
    private double y;
    private String name;

    public Shape(){
    }

    protected Shape(int x, double y, String name){
        this.x = x;
        this.y = y;
        this.name = name;
    }

    public Shape(double y, String name){
        this.y = y;
        this.name = name;
    }

    public static Shape sunsfan(double y, String name){
        return new Shape(y, name);
    }
}

7、@SneakyThrows使用

// 使用注解
public class SneakyThrows implements Runnable {
    @SneakyThrows(UnsupportedEncodingException.class)
    public String utf8ToString(byte[] bytes) {
        return new String(bytes, "UTF-8");
    }

    @SneakyThrows
    public void run() {
        throw new Throwable();
    }
}
// 不使用注解
public class SneakyThrows implements Runnable {
    public String utf8ToString(byte[] bytes) {
        try{
            return new String(bytes, "UTF-8");
        }catch(UnsupportedEncodingException uee){
            throw Lombok.sneakyThrow(uee);
        }
    }

    public void run() {
        try{
            throw new Throwable();
        }catch(Throwable t){
            throw Lombok.sneakyThrow(t);
        }
    }
}

8、@Synchronized使用

// 使用注解
public class Synchronized {
    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");
    }
}
// 不使用
public class Synchronized {
   private static final Object $LOCK = new Object[0];
   private final Object $lock = new Object[0];
   private final Object readLock = new Object();

   public static void hello() {
     synchronized($LOCK) {
       System.out.println("world");
     }
   }

   public int answerToLife() {
     synchronized($lock) {
       return 42;
     }
   }

   public void foo() {
     synchronized(readLock) {
       System.out.println("bar");
     }
   }
 }

9、@Log的使用

@CommonsLog
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@JBossLog
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);
@Log
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
上一篇 下一篇

猜你喜欢

热点阅读