1/* 2 * @test /nodynamiccopyright/ 3 * @bug 7196163 4 * @summary Twr with different kinds of variables: local, instance, class, array component, parameter 5 * @compile/fail/ref=TwrVarKinds.out -XDrawDiagnostics TwrVarKinds.java 6 */ 7 8public 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 - covered by TwrForVariable1 test */ 38 39 /* array components - covered by TwrForVariable2 test */ 40 41 /* method parameter - ok */ 42 method(r5); 43 44 /* constructor parameter - ok */ 45 TwrVarKinds r6 = new TwrVarKinds(r5); 46 47 /* lambda parameter - covered by TwrAndLambda */ 48 49 /* exception parameter - ok */ 50 try { 51 throw new ResourceException(); 52 } catch (ResourceException e) { 53 try (e) { 54 } 55 } 56 } 57 58 public TwrVarKinds() { 59 } 60 61 public TwrVarKinds(TwrVarKinds r) { 62 try (r) { 63 } 64 } 65 66 static void method(TwrVarKinds r) { 67 /* parameter */ 68 try (r) { 69 } 70 } 71 72 static void fail(String reason) { 73 throw new RuntimeException(reason); 74 } 75 76 public void close() {} 77 78 static class ResourceException extends Exception implements AutoCloseable { 79 public void close() {} 80 } 81} 82