我有一个关于@SneakyThrows的问题可以用来偷偷抛出已检查的异常而不在你的方法的throws子句中实际声明这一点.
public class Demo {
public static void main(String[] args) {
}
private void throwE() throws ClassNotFoundException {
}
@SneakyThrows
private void t() {
throwE();
}
}
Run Code Online (Sandbox Code Playgroud)
这是由lombok生成的.
public class Demo {
public Demo() {
}
public static void main(String[] args) throws IOException {
}
private void throwE() throws ClassNotFoundException {
}
private void t() {
try {
this.throwE();
} catch (Throwable var2) {
throw var2;
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么lombok生成的代码可以在不声明throws子句的情况下伪造编译器.
public static void main(String[] args) throws ParseException {
long DAY = 1000 * 60 * 60 * 24;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date yDate = simpleDateFormat.parse("2020-06-12 23:58:33");
System.out.println(yDate.getTime() - (yDate.getTime() % DAY));
Date date = simpleDateFormat.parse("2020-06-13 00:29:38");
System.out.println(date.getTime() - date.getTime() % DAY);
}
Run Code Online (Sandbox Code Playgroud)
控制台打印:1591920000000 1591920000000