langtools/test/tools/javac/TryWithResources/TwrAndLambda.java
changeset 34565 627464b87753
equal deleted inserted replaced
34564:ce1498f6591f 34565:627464b87753
       
     1 /*
       
     2  * @test /nodynamiccopyright/
       
     3  * @bug 7196163
       
     4  * @summary Twr with resource variables as lambda expressions and method references
       
     5  * @compile/fail/ref=TwrAndLambda.out -XDrawDiagnostics TwrAndLambda.java
       
     6  */
       
     7 
       
     8 public class TwrAndLambda {
       
     9 
       
    10     public static void main(String... args) {
       
    11 
       
    12         // Lambda expression
       
    13         AutoCloseable v1 = () -> {};
       
    14         // Static method reference
       
    15         AutoCloseable v2 = TwrAndLambda::close1;
       
    16         // Instance method reference
       
    17         AutoCloseable v3 = new TwrAndLambda()::close2;
       
    18         // Lambda expression which is not AutoCloseable
       
    19         Runnable r1 = () -> {};
       
    20         // Static method reference which is not AutoCloseable
       
    21         Runnable r2 = TwrAndLambda::close1;
       
    22         // Instance method reference which is not AutoCloseable
       
    23         Runnable r3 = new TwrAndLambda()::close2;
       
    24 
       
    25         try (v1) {
       
    26         } catch(Exception e) {}
       
    27         try (v2) {
       
    28         } catch(Exception e) {}
       
    29         try (v3) {
       
    30         } catch(Exception e) {}
       
    31         try (r1) {
       
    32         } catch(Exception e) {}
       
    33         try (r2) {
       
    34         } catch(Exception e) {}
       
    35         try (r3) {
       
    36         } catch(Exception e) {}
       
    37 
       
    38         // lambda invocation
       
    39         I i = (x) -> { try(x) { } catch (Exception e) { } };
       
    40         i.m(v1);
       
    41         i.m(v2);
       
    42         i.m(v3);
       
    43         i.m(r1);
       
    44         i.m(r2);
       
    45         i.m(r3);
       
    46     }
       
    47 
       
    48     static interface I {
       
    49         public void m(AutoCloseable r);
       
    50     }
       
    51 
       
    52     public static void close1() { }
       
    53 
       
    54     public void close2() { }
       
    55 }