test/tools/javac/lambda/MethodReference23.java

Tue, 14 May 2013 11:11:09 -0700

author
rfield
date
Tue, 14 May 2013 11:11:09 -0700
changeset 1752
c09b7234cded
parent 0
959103a6100f
permissions
-rw-r--r--

8012556: Implement lambda methods on interfaces as static
8006140: Javac NPE compiling Lambda expression on initialization expression of static field in interface
Summary: Lambdas occurring in static contexts or those not needing instance information should be generated into static methods. This has long been the case for classes. However, as a work-around to the lack of support for statics on interfaces, interface lambda methods have been generated into default methods. For lambdas in interface static contexts (fields and static methods) this causes an NPE in javac because there is no 'this'. MethodHandles now support static methods on interfaces. This changeset allows lambda methods to be generated as static interface methods. An existing bug in Hotspot (8013875) is exposed in a test when the "-esa" flag is used. This test and another test that already exposed this bug have been marked with @ignore.
Reviewed-by: mcimadamore

     1 /*
     2  * @test /nodynamiccopyright/
     3  * @bug 8003280
     4  * @summary Add lambda tests
     5  *  check that pair of bound/non-bound constructor references is flagged as ambiguous
     6  * @author  Maurizio Cimadamore
     7  * @compile/fail/ref=MethodReference23.out -XDrawDiagnostics MethodReference23.java
     8  */
    10 class MethodReference23 {
    12     class Inner1 {
    13         Inner1(MethodReference23 outer) {};
    14         Inner1() {};
    15     }
    17     static class Inner2 {
    18         Inner2(MethodReference23 outer) {};
    19         Inner2() {};
    20     }
    22     interface SAM11 {
    23         Inner1 m(MethodReference23 rec);
    24     }
    26     interface SAM12 {
    27         Inner1 m();
    28     }
    30     interface SAM21 {
    31         Inner2 m(MethodReference23 rec);
    32     }
    34     interface SAM22 {
    35         Inner2 m();
    36     }
    38     static void call11(SAM11 s) {   }
    40     static void call12(SAM12 s) {   }
    42     static void call21(SAM21 s) {   }
    44     static void call22(SAM22 s) {   }
    46     static void call3(SAM11 s) {   }
    47     static void call3(SAM12 s) {   }
    48     static void call3(SAM21 s) {   }
    49     static void call3(SAM22 s) {   }
    51     static void test11() {
    52         SAM11 s = MethodReference23.Inner1::new; //ok
    53         call11(MethodReference23.Inner1::new); //ok
    54     }
    56     static void test12() {
    57         SAM12 s = MethodReference23.Inner1::new; //fail
    58         call12(MethodReference23.Inner1::new); //fail
    59     }
    61     static void test21() {
    62         SAM21 s = MethodReference23.Inner2::new; //ok
    63         call21(MethodReference23.Inner2::new); //ok
    64     }
    66     static void test22() {
    67         SAM22 s = MethodReference23.Inner2::new; //ok
    68         call22(MethodReference23.Inner2::new); //ok
    69     }
    71     static void test3() {
    72         call3(MethodReference23.Inner2::new); //ambiguous
    73     }
    74 }

mercurial