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 * Overloaded methods take raw SAM types that have type inference according to SAM descriptor
6 should have ambiguous resolution of method
7 * @compile/fail/ref=InferenceTest_neg1_2.out -XDrawDiagnostics InferenceTest_neg1_2.java
8 */
10 public class InferenceTest_neg1_2 {
12 public static void main(String[] args) {
13 InferenceTest_neg1_2 test = new InferenceTest_neg1_2();
14 test.method(n -> null); //method 1-5 all match
15 test.method(n -> "a"); //method 2, 4 match
16 test.method(n -> 0); //method 1, 3, 5 match
17 }
19 void method(SAM1 s) { //method 1
20 Integer i = s.foo("a");
21 }
23 void method(SAM2 s) { //method 2
24 String str = s.foo(0);
25 }
27 void method(SAM3<Integer> s) { //method 3
28 Integer i = s.get(0);
29 }
31 void method(SAM4<Double, String> s) { //method 4
32 String str = s.get(0.0);
33 }
35 void method(SAM5<Integer> s) { //method 5
36 Integer i = s.get(0.0);
37 }
39 interface SAM1 {
40 Integer foo(String a);
41 }
43 interface SAM2 {
44 String foo(Integer a);
45 }
47 interface SAM3<T> {
48 T get(T t);
49 }
51 interface SAM4<T, V> {
52 V get(T t);
53 }
55 interface SAM5<T> {
56 T get(Double i);
57 }
58 }