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 8003280
4 * @summary Add lambda tests
5 * check that code generation handles void-compatibility correctly
6 * @compile/fail/ref=LambdaConv21.out -XDrawDiagnostics LambdaConv21.java
7 */
9 class LambdaConv21 {
11 interface SAM_void<X> {
12 void m();
13 }
15 interface SAM_java_lang_Void {
16 Void m();
17 }
19 static void m_void() { }
21 static Void m_java_lang_Void() { return null; }
23 static void testExpressionLambda() {
24 SAM_void s1 = ()->m_void(); //ok
25 SAM_java_lang_Void s2 = ()->m_void(); //no - incompatible target
26 SAM_void s3 = ()->m_java_lang_Void(); //ok - expression statement lambda is compatible with void
27 SAM_java_lang_Void s4 = ()->m_java_lang_Void(); //ok
28 }
30 static void testStatementLambda() {
31 SAM_void s1 = ()-> { m_void(); }; //ok
32 SAM_java_lang_Void s2 = ()-> { m_void(); }; //no - missing return value
33 SAM_void s3 = ()-> { return m_java_lang_Void(); }; //no - unexpected return value
34 SAM_java_lang_Void s4 = ()-> { return m_java_lang_Void(); }; //ok
35 SAM_void s5 = ()-> { m_java_lang_Void(); }; //ok
36 SAM_java_lang_Void s6 = ()-> { m_java_lang_Void(); }; //no - missing return value
37 }
38 }