test/tools/javac/lambda/LambdaConv09.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 SAM conversion handles Object members correctly
     6  * @author  Alex Buckley
     7  * @author  Maurizio Cimadamore
     8  * @compile/fail/ref=LambdaConv09.out -XDrawDiagnostics LambdaConv09.java
     9  */
    11 class LambdaConv09 {
    13     // Not a SAM type; not enough abstract methods
    14     interface Foo1 {}
    16     // SAM type; Foo has no abstract methods
    17     interface Foo2 { boolean equals(Object object); }
    20     // Not a SAM type; Foo still has no abstract methods
    21     interface Foo3 extends Foo2 { public abstract String toString(); }
    23     // SAM type; Bar has one abstract non-Object method
    24     interface Foo4<T> extends Foo2 { int compare(T o1, T o2); }
    26     // Not a SAM type; still no valid abstract methods
    27     interface Foo5 {
    28         boolean equals(Object object);
    29         String toString();
    30     }
    32     // SAM type; Foo6 has one abstract non-Object method
    33     interface Foo6<T> {
    34         boolean equals(Object obj);
    35         int compare(T o1, T o2);
    36     }
    38     // SAM type; Foo6 has one abstract non-Object method
    39     interface Foo7<T> extends Foo2, Foo6<T> { }
    41     void test() {
    42         Foo1 f1 = ()-> { };
    43         Foo2 f2 = ()-> { };
    44         Foo3 f3 = x -> true;
    45         Foo4 f4 = (x, y) -> 1;
    46         Foo5 f5 = x -> true;
    47         Foo6 f6 = (x, y) -> 1;
    48         Foo7 f7 = (x, y) -> 1;
    49     }
    50 }

mercurial