源码(@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