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

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

mercurial