test/tools/javac/lambda/MethodReference28.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 non-compatible method references are rejected
     6  * @compile/fail/ref=MethodReference28.out -XDrawDiagnostics MethodReference28.java
     7  */
     9 class MethodReference28 {
    11     interface SAM1 {
    12         void m(int i);
    13     }
    15     interface SAM2 {
    16         void m(MethodReference28 rec, int i);
    17     }
    19     static void static_m1(Integer i) { } //ok - boxing
    20     static void static_m2(Integer i1, Integer i2) { } //wrong arity
    21     static void static_m3(String s) { } //type mismatch
    22     static void static_m4(String... ss) { } //type mismatch - varargs
    24     void m1(Integer i) { } //ok - boxing
    25     void m2(Integer i1, Integer i2) { } //wrong arity
    26     void m3(String s) { } //type mismatch
    27     void m4(String... ss) { } //type mismatch - varargs
    29     static void testStatic() {
    30         SAM1 s1 = MethodReference28::static_m1;
    31         SAM1 s2 = MethodReference28::static_m2;
    32         SAM1 s3 = MethodReference28::static_m3;
    33         SAM1 s4 = MethodReference28::static_m4;
    34     }
    36     void testBadMember() {
    37         SAM1 s1 = MethodReference28::m1;
    38         SAM1 s2 = MethodReference28::m2;
    39         SAM1 s3 = MethodReference28::m3;
    40         SAM1 s4 = MethodReference28::m4;
    41     }
    43     void testMember() {
    44         SAM1 s1 = this::m1;
    45         SAM1 s2 = this::m2;
    46         SAM1 s3 = this::m3;
    47         SAM1 s4 = this::m4;
    48     }
    50     static void testUnbound() {
    51         SAM2 s1 = MethodReference28::m1;
    52         SAM2 s2 = MethodReference28::m2;
    53         SAM2 s3 = MethodReference28::m3;
    54         SAM2 s4 = MethodReference28::m4;
    55     }
    56 }

mercurial