1 /*
   2  * @test /nodynamiccopyright/
   3  * @bug 7196163
   4  * @summary Twr with different kinds of variables: local, instance, class, array compontnt, parameter
   5  * @compile/fail/ref=TwrVarKinds.out -XDrawDiagnostics TwrVarKinds.java
   6  */
   7 
   8 public class TwrVarKinds implements AutoCloseable {
   9 
  10     final static TwrVarKinds r1 = new TwrVarKinds();
  11     final TwrVarKinds r2 = new TwrVarKinds();
  12     static TwrVarKinds r3 = new TwrVarKinds();
  13     TwrVarKinds r4 = new TwrVarKinds();
  14 
  15     public static void main(String... args) {
  16 
  17         TwrVarKinds r5 = new TwrVarKinds();
  18 
  19         /* static final field - ok */
  20         try (r1) {
  21         }
  22 
  23         /* non-static final field - ok */
  24         try (r1.r2) {
  25         }
  26 
  27         /* static non-final field - wrong */
  28         try (r3) {
  29             fail("Static non-final field is not allowed");
  30         }
  31 
  32         /* non-static non-final field - wrong */
  33         try (r1.r4) {
  34             fail("Non-static non-final field is not allowed");
  35         }
  36 
  37         /* local variable - ok */
  38         try (r5) {
  39         }
  40 
  41         /* array components - wrong */
  42         TwrVarKinds[] arr1 = new TwrVarKinds[1];
  43         TwrVarKinds[][] arr2 = new TwrVarKinds[1][1];
  44         try (arr1[0]) {
  45             fail("Array access not allowed");
  46         }
  47         try (arr[0][0]) {
  48             fail("Array access not allowed");
  49         }
  50 
  51         /* method parameter - ok */
  52         method(r5);
  53 
  54         /* constructor parameter - ok */
  55         TwrVarKinds r6 = new TwrVarKinds(r5);
  56 
  57         /* lambda parameter - ok */
  58         I i = (x) -> { try (x) {} };
  59         i.m(r5);
  60 
  61         /* exception parameter - ok */
  62         try {
  63             throw new ResourceException();
  64         } catch (ResourceException e) {
  65             try (e) {
  66             }
  67         }
  68     }
  69 
  70     public TwrVarKinds() {
  71     }
  72 
  73     public TwrVarKinds(TwrVarKinds r) {
  74         try (r) {
  75         }
  76     }
  77 
  78     static void method(TwrVarKinds r) {
  79         /* parameter */
  80         try (r) {
  81         }
  82     }
  83 
  84     static void fail(String reason) {
  85         throw new RuntimeException(reason);
  86     }
  87 
  88     public void close() {}
  89 
  90     static interface I {
  91         public void m(TwrVarKinds r);
  92     }
  93 
  94     static class ResourceException extends Exception implements AutoCloseable {
  95         public void close() {}
  96     }
  97 }