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
mcimadamore@1415 | 1 | /* |
mcimadamore@1415 | 2 | * @test /nodynamiccopyright/ |
mcimadamore@1415 | 3 | * @bug 8003280 |
mcimadamore@1415 | 4 | * @summary Add lambda tests |
mcimadamore@1415 | 5 | * check that candidates with cyclic type-inference are removed from the |
mcimadamore@1415 | 6 | * set of applicable methods |
mcimadamore@1415 | 7 | * @compile/fail/ref=TargetType21.out -XDrawDiagnostics TargetType21.java |
mcimadamore@1415 | 8 | */ |
mcimadamore@1415 | 9 | |
mcimadamore@1415 | 10 | class TargetType21 { |
mcimadamore@1415 | 11 | interface SAM1 { |
mcimadamore@1415 | 12 | String m1(Integer n) throws Exception; |
mcimadamore@1415 | 13 | } |
mcimadamore@1415 | 14 | |
mcimadamore@1415 | 15 | interface SAM2 { |
mcimadamore@1415 | 16 | void m2(Integer n); |
mcimadamore@1415 | 17 | } |
mcimadamore@1415 | 18 | |
mcimadamore@1415 | 19 | interface SAM3<R,A> { |
mcimadamore@1415 | 20 | R m3(A n); |
mcimadamore@1415 | 21 | } |
mcimadamore@1415 | 22 | |
mcimadamore@1415 | 23 | void call(SAM1 sam) { } |
mcimadamore@1415 | 24 | void call(SAM2 sam) { } |
mcimadamore@1415 | 25 | <R,A> void call(SAM3<R,A> sam) { } |
mcimadamore@1415 | 26 | |
mcimadamore@1415 | 27 | void test() { |
mcimadamore@1480 | 28 | call(x -> { throw new Exception(); }); //ambiguous |
mcimadamore@1510 | 29 | call(x -> { System.out.println(""); }); //ambiguous |
mcimadamore@1510 | 30 | call(x -> { return (Object) null; }); //cyclic inference |
mcimadamore@1510 | 31 | call(x -> { return null; }); //ambiguous |
mcimadamore@1415 | 32 | } |
mcimadamore@1415 | 33 | } |