test/tools/javac/lambda/MethodReference22.java

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

author
rfield
date
Tue, 14 May 2013 11:11:09 -0700
changeset 1752
c09b7234cded
parent 1610
08782b8b03ce
child 2193
d4cbb671de1c
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

aoqi@0 1 /*
aoqi@0 2 * @test /nodynamiccopyright/
aoqi@0 3 * @bug 8003280
aoqi@0 4 * @summary Add lambda tests
aoqi@0 5 * check that pair of bound/non-bound method references checked correctly
aoqi@0 6 * @author Maurizio Cimadamore
aoqi@0 7 * @compile/fail/ref=MethodReference22.out -XDrawDiagnostics MethodReference22.java
aoqi@0 8 */
aoqi@0 9
aoqi@0 10 class MethodReference22 {
aoqi@0 11
aoqi@0 12 void m1(String x) { }
aoqi@0 13 void m1(MethodReference22 rec, String x) { }
aoqi@0 14
aoqi@0 15 static void m2(String x) { }
aoqi@0 16 static void m2(MethodReference22 rec, String x) { }
aoqi@0 17
aoqi@0 18 static void m3(String x) { }
aoqi@0 19 void m3(MethodReference22 rec, String x) { }
aoqi@0 20
aoqi@0 21 void m4(String x) { }
aoqi@0 22 static void m4(MethodReference22 rec, String x) { }
aoqi@0 23
aoqi@0 24 interface SAM1 {
aoqi@0 25 void m(String x);
aoqi@0 26 }
aoqi@0 27
aoqi@0 28 interface SAM2 {
aoqi@0 29 void m(MethodReference22 rec, String x);
aoqi@0 30 }
aoqi@0 31
aoqi@0 32 static void call1(SAM1 s) { }
aoqi@0 33
aoqi@0 34 static void call2(SAM2 s) { }
aoqi@0 35
aoqi@0 36 static void call3(SAM1 s) { }
aoqi@0 37 static void call3(SAM2 s) { }
aoqi@0 38
aoqi@0 39 static void test1() {
aoqi@0 40 SAM1 s1 = MethodReference22::m1; //fail
aoqi@0 41 call1(MethodReference22::m1); //fail
aoqi@0 42 SAM1 s2 = MethodReference22::m2; //ok
aoqi@0 43 call1(MethodReference22::m2); //ok
aoqi@0 44 SAM1 s3 = MethodReference22::m3; //ok
aoqi@0 45 call1(MethodReference22::m3); //ok
aoqi@0 46 SAM1 s4 = MethodReference22::m4; //fail
aoqi@0 47 call1(MethodReference22::m4); //fail
aoqi@0 48 }
aoqi@0 49
aoqi@0 50 static void test2() {
mcimadamore@1610 51 SAM2 s1 = MethodReference22::m1; //ambiguous
mcimadamore@1610 52 call2(MethodReference22::m1); //ambiguous
mcimadamore@1610 53 SAM2 s2 = MethodReference22::m2; //ambiguous
mcimadamore@1610 54 call2(MethodReference22::m2); //ambiguous
mcimadamore@1610 55 SAM2 s3 = MethodReference22::m3; //ambiguous
mcimadamore@1610 56 call2(MethodReference22::m3); //ambiguous
mcimadamore@1610 57 SAM2 s4 = MethodReference22::m4; //ambiguous
mcimadamore@1610 58 call2(MethodReference22::m4); //ambiguous
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 static void test3() {
mcimadamore@1610 62 call3(MethodReference22::m1); //fail
mcimadamore@1610 63 call3(MethodReference22::m2); //ok
aoqi@0 64 call3(MethodReference22::m3); //ok
aoqi@0 65 call3(MethodReference22::m4); //fail
aoqi@0 66 }
aoqi@0 67 }

mercurial