Tue, 14 May 2013 11:11:09 -0700
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 8008540 8008539 8008538
4 * @summary Constructor reference to non-reifiable array should be rejected
5 * @compile/fail/ref=MethodReference64.out -XDrawDiagnostics MethodReference64.java
6 */
7 class MethodReference64 {
8 interface ClassFactory {
9 Object m();
10 }
12 interface ArrayFactory {
13 Object m(int i);
14 }
16 @interface Anno { }
18 enum E { }
20 interface I { }
22 static class Foo<X> { }
24 void m(ClassFactory cf) { }
25 void m(ArrayFactory cf) { }
27 void testAssign() {
28 ClassFactory c1 = Anno::new; //error
29 ClassFactory c2 = E::new; //error
30 ClassFactory c3 = I::new; //error
31 ClassFactory c4 = Foo<?>::new; //error
32 ClassFactory c5 = 1::new; //error
33 ArrayFactory a1 = Foo<?>[]::new; //ok
34 ArrayFactory a2 = Foo<? extends String>[]::new; //error
35 }
37 void testMethod() {
38 m(Anno::new); //error
39 m(E::new); //error
40 m(I::new); //error
41 m(Foo<?>::new); //error
42 m(1::new); //error
43 m(Foo<?>[]::new); //ok - resolves to m(ArrayFactory)
44 m(Foo<? extends String>[]::new); //error
45 }
46 }