探究编译后,try-with-resources括号中的object是否关闭,以及两种写法编译后的对比
2024-10-15 11:37:36
源码(@TargetApi(Build.VERSION_CODES.KITKAT))
public List<T> test1() {
String sql = "selxe xxxxxxxxxxx";
try (Cursor cursor = dbManager.getReadableDatabase().rawQuery(sql, null)) {
List<T> lst = new ArrayList<>();
while (cursor.moveToNext()) {
lst.add(findEntity(cursor));
}
return lst;
}
} public List<T> test2() {
String sql = "selxe xxxxxxxxxxx";
Cursor cursor = dbManager.getReadableDatabase().rawQuery(sql, null);
try {
List<T> lst = new ArrayList<>();
while (cursor.moveToNext()) {
lst.add(findEntity(cursor));
}
return lst;
} finally {
cursor.close();
}
}
反编译后
public List<T> test1()
{
Cursor localCursor = this.dbManager.getReadableDatabase().rawQuery("selxe xxxxxxxxxxx", null);
try
{
localArrayList = new ArrayList();
while (localCursor.moveToNext())
localArrayList.add(findEntity(localCursor));
}
catch (Throwable localThrowable3)
{
ArrayList localArrayList;
Object localObject1;
try
{
throw localThrowable3;
}
finally
{
localThrowable1 = localThrowable3;
}
if ((localCursor == null) || (localThrowable1 != null));
while (true)
{
try
{
localCursor.close();
throw localObject1;
if ((localCursor == null) || (0 != 0))
try
{
localCursor.close();
return localArrayList;
}
catch (Throwable localThrowable4)
{
null.addSuppressed(localThrowable4);
return localArrayList;
}
localCursor.close();
return localArrayList;
}
catch (Throwable localThrowable2)
{
localThrowable1.addSuppressed(localThrowable2);
continue;
}
localCursor.close();
}
}
finally
{
while (true)
Throwable localThrowable1 = null;
}
} public List<T> test2()
{
Cursor localCursor = this.dbManager.getReadableDatabase().rawQuery("selxe xxxxxxxxxxx", null);
ArrayList localArrayList;
try
{
localArrayList = new ArrayList();
while (localCursor.moveToNext())
localArrayList.add(findEntity(localCursor));
}
finally
{
localCursor.close();
}
localCursor.close();
return localArrayList;
}
The try
-with-resources statement is a try
statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try
-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable
, which includes all objects which implement java.io.Closeable
, can be used as a resource.
参考:https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
最新文章
- JSHelper时间格式化
- xml配置文件的读写
- 查看 并发请求数及其TCP连接状态【转】
- java高薪之路__007_反射
- windows下重启mysql
- LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告
- 有关对字符串的处理,需要用到List时的简化写法
- Matrix 二维树状数组的第二类应用
- Drools
- C++对象模型2--指针cout结果
- zBase --轻量级DOM操作库
- QUICK-AP + BETTERCAP 搭建热点, 欺骗局域网内用户下载任意可执行文件
- RTMP协议中文翻译(首发)
- Selenium_WebDriver_定位元素
- 【转载】java abstract class和interface的区别
- django rest_framework 序列化组件详解
- Nmap 使用技巧及其攻略
- JAVA框架 SpringMVC RequestMapping讲解
- windows系统中Dotnet core runtime 安装后,无法启动次程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll的解决方法
- python 进程队列
热门文章
- JSP和Struts2、Hibernate、Spring3基础内容和原理
- 前端-JavaScript1-7——JavaScript之数学运算符
- redis的pub/sub命令
- About Gnu Linker1
- jenkins自动构建站点
- 移植QT库的问题:QT_INSTALL/include/QtCore/qatomic_arm.h:131: Error: no such instruction: `swpb %al,
- jmeter联合selenium webdriver进行自动化测试-简单1
- Android 开发 获取Android设备的屏幕高宽
- 在c#中利用keep-alive处理socket网络异常断开的方法
- Linux查看密码