zoukankan      html  css  js  c++  java
  • 探究编译后,try-with-resources括号中的object是否关闭,以及两种写法编译后的对比

    源码(@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();
            }
        }
    View Code

    反编译后

    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;
      }
    View Code

    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

  • 相关阅读:
    c# 日期函数DateTime.ToString()日期的各种格式 (本人亲测)
    C# Excel导入、导出【源码下载】
    微信支付服务器CA证书更换服务器安装der证书的方法 DigiCert的根证书
    重置winsock目录解决不能上网的问题
    模型验证组件 FluentValidation
    对于“Newtonsoft.Json”已拥有为“NETStander.Library”定义的依赖项,解决办法
    .NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(一)
    C# 中参数验证方式的演变
    一步一步ITextSharp 低级操作函数使用
    Winform 打印PDF顺序混乱,获取打印队列
  • 原文地址:https://www.cnblogs.com/shixm/p/7402158.html
Copyright © 2011-2022 走看看