LomBok 的相关目录已经整理出来,希望大家可以根据需求自助学习,好工具要大家分享:
@Cleanup ????
@Getter, @Setter
@ToString
@EqualsAndHashCode
@Constructor
@Data & @Value
@SneakyThrows
@Synchronized
@Getter(lazy=true)
@Log
?
@SneakyThrows的用法比较简单,其实就是对于异常的一个整理,减少了到处写cache的不便利性。比如在线程中,cache所有异常,再比如在一些不太可能发生异常的地方,但是你又必须cache checked exception的地方使用这个annotation会显得代码比较规整,易读。或许也会显得高大上一点吧
[code="java"]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();
?? }
}
[code="java"] import lombok.Lombok;
public class SneakyThrowsExample implements Runnable {
?? public String utf8ToString(byte[] bytes) {
???? try {
?????? return new String(bytes, "UTF-8");
???? } catch (UnsupportedEncodingException e) {
?????? throw Lombok.sneakyThrow(e);
???? }
?? }
??
?? public void run() {
???? try {
?????? throw new Throwable();
???? } catch (Throwable t) {
?????? throw Lombok.sneakyThrow(t);
???? }
?? }
}
?