< prev index next >

test/tools/javac/TryWithResources/TwrForVariable1.java

Print this page




  20             assertCloseCount(2);
  21         } catch (Exception ex) {
  22         }
  23         try (new TwrForVariable1() { }.finalWrapper.finalField) {
  24             assertCloseCount(3);
  25         } catch (Exception ex) {
  26         }
  27         try ((args.length > 0 ? v : new TwrForVariable1()).finalWrapper.finalField) {
  28             assertCloseCount(4);
  29         } catch (Exception ex) {
  30         }
  31         try {
  32             throw new CloseableException();
  33         } catch (CloseableException ex) {
  34             try (ex) {
  35                 assertCloseCount(5);
  36             }
  37         }
  38 
  39         assertCloseCount(6);












  40     }
  41 
  42     static void assertCloseCount(int expectedCloseCount) {
  43         if (closeCount != expectedCloseCount)
  44             throw new RuntimeException("bad closeCount: " + closeCount +
  45                                        "; expected: " + expectedCloseCount);
  46     }
  47 
  48     public void close() {
  49         closeCount++;
  50     }
  51 
  52     final FinalWrapper finalWrapper = new FinalWrapper();
  53 
  54     static class FinalWrapper {
  55         public final AutoCloseable finalField = new AutoCloseable() {
  56             @Override
  57             public void close() throws Exception {
  58                 closeCount++;
  59             }


  20             assertCloseCount(2);
  21         } catch (Exception ex) {
  22         }
  23         try (new TwrForVariable1() { }.finalWrapper.finalField) {
  24             assertCloseCount(3);
  25         } catch (Exception ex) {
  26         }
  27         try ((args.length > 0 ? v : new TwrForVariable1()).finalWrapper.finalField) {
  28             assertCloseCount(4);
  29         } catch (Exception ex) {
  30         }
  31         try {
  32             throw new CloseableException();
  33         } catch (CloseableException ex) {
  34             try (ex) {
  35                 assertCloseCount(5);
  36             }
  37         }
  38 
  39         assertCloseCount(6);
  40 
  41         // null test cases
  42         TwrForVariable1 n = null;
  43 
  44         try (n) {
  45         }
  46         try (n) {
  47             throw new Exception();
  48         } catch (Exception e) {
  49         }
  50 
  51         assertCloseCount(6);
  52     }
  53 
  54     static void assertCloseCount(int expectedCloseCount) {
  55         if (closeCount != expectedCloseCount)
  56             throw new RuntimeException("bad closeCount: " + closeCount +
  57                                        "; expected: " + expectedCloseCount);
  58     }
  59 
  60     public void close() {
  61         closeCount++;
  62     }
  63 
  64     final FinalWrapper finalWrapper = new FinalWrapper();
  65 
  66     static class FinalWrapper {
  67         public final AutoCloseable finalField = new AutoCloseable() {
  68             @Override
  69             public void close() throws Exception {
  70                 closeCount++;
  71             }
< prev index next >