test/tools/javac/lambda/LambdaConv09.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 SAM conversion handles Object members correctly
aoqi@0 6 * @author Alex Buckley
aoqi@0 7 * @author Maurizio Cimadamore
aoqi@0 8 * @compile/fail/ref=LambdaConv09.out -XDrawDiagnostics LambdaConv09.java
aoqi@0 9 */
aoqi@0 10
aoqi@0 11 class LambdaConv09 {
aoqi@0 12
aoqi@0 13 // Not a SAM type; not enough abstract methods
aoqi@0 14 interface Foo1 {}
aoqi@0 15
aoqi@0 16 // SAM type; Foo has no abstract methods
aoqi@0 17 interface Foo2 { boolean equals(Object object); }
aoqi@0 18
aoqi@0 19
aoqi@0 20 // Not a SAM type; Foo still has no abstract methods
aoqi@0 21 interface Foo3 extends Foo2 { public abstract String toString(); }
aoqi@0 22
aoqi@0 23 // SAM type; Bar has one abstract non-Object method
aoqi@0 24 interface Foo4<T> extends Foo2 { int compare(T o1, T o2); }
aoqi@0 25
aoqi@0 26 // Not a SAM type; still no valid abstract methods
aoqi@0 27 interface Foo5 {
aoqi@0 28 boolean equals(Object object);
aoqi@0 29 String toString();
aoqi@0 30 }
aoqi@0 31
aoqi@0 32 // SAM type; Foo6 has one abstract non-Object method
aoqi@0 33 interface Foo6<T> {
aoqi@0 34 boolean equals(Object obj);
aoqi@0 35 int compare(T o1, T o2);
aoqi@0 36 }
aoqi@0 37
aoqi@0 38 // SAM type; Foo6 has one abstract non-Object method
aoqi@0 39 interface Foo7<T> extends Foo2, Foo6<T> { }
aoqi@0 40
aoqi@0 41 void test() {
aoqi@0 42 Foo1 f1 = ()-> { };
aoqi@0 43 Foo2 f2 = ()-> { };
aoqi@0 44 Foo3 f3 = x -> true;
aoqi@0 45 Foo4 f4 = (x, y) -> 1;
aoqi@0 46 Foo5 f5 = x -> true;
aoqi@0 47 Foo6 f6 = (x, y) -> 1;
aoqi@0 48 Foo7 f7 = (x, y) -> 1;
aoqi@0 49 }
aoqi@0 50 }

mercurial