注解类型 Cleanup
确保您注解的变量声明将在任何情况下通过调用其 close 方法进行清理。 通过将局部变量声明之后到作用域结束的所有语句包装到一个 try 代码块中来实现,该代码块作为 finally 操作关闭资源。
完整文档请见 @Cleanup 的 project lombok 功能页面。
示例
public void copyFile(String in, String out) throws IOException {
@Cleanup FileInputStream inStream = new FileInputStream(in);
@Cleanup FileOutputStream outStream = new FileOutputStream(out);
byte[] b = new byte[65536];
while (true) {
int r = inStream.read(b);
if (r == -1) break;
outStream.write(b, 0, r);
}
}
将生成 public void copyFile(String in, String out) throws IOException {
@Cleanup FileInputStream inStream = new FileInputStream(in);
try {
@Cleanup FileOutputStream outStream = new FileOutputStream(out);
try {
byte[] b = new byte[65536];
while (true) {
int r = inStream.read(b);
if (r == -1) break;
outStream.write(b, 0, r);
}
} finally {
if (outStream != null) outStream.close();
}
} finally {
if (inStream != null) inStream.close();
}
}
-
可选元素概要
可选元素
-
元素详细信息
-
value
-