test/tools/javac/lambda/TargetType66.java

Tue, 14 May 2013 11:11:09 -0700

author
rfield
date
Tue, 14 May 2013 11:11:09 -0700
changeset 1752
c09b7234cded
parent 1677
94a202228ec2
child 2000
4a6acc42c3a1
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

mcimadamore@1677 1 /*
mcimadamore@1677 2 * @test /nodynamiccopyright/
mcimadamore@1677 3 * @bug 8009131
mcimadamore@1677 4 * @summary Overload: javac should discard methods that lead to errors in lambdas with implicit parameter types
mcimadamore@1677 5 * @compile/fail/ref=TargetType66.out -XDrawDiagnostics TargetType66.java
mcimadamore@1677 6 */
mcimadamore@1677 7 class TargetType66 {
mcimadamore@1677 8 interface SAM1 {
mcimadamore@1677 9 void m(String s);
mcimadamore@1677 10 }
mcimadamore@1677 11
mcimadamore@1677 12 interface SAM2 {
mcimadamore@1677 13 void m(Integer s);
mcimadamore@1677 14 }
mcimadamore@1677 15
mcimadamore@1677 16 void g(SAM1 s1) { }
mcimadamore@1677 17 void g(SAM2 s2) { }
mcimadamore@1677 18
mcimadamore@1677 19 void test() {
mcimadamore@1677 20 g(x->{ String s = x; }); //g(SAM1)
mcimadamore@1677 21 g(x->{ Integer i = x; }); //g(SAM2)
mcimadamore@1677 22 g(x->{ Object o = x; }); //ambiguous
mcimadamore@1677 23 g(x->{ Character c = x; }); //error: inapplicable methods
mcimadamore@1677 24 g(x->{ Character c = ""; }); //error: incompatible types
mcimadamore@1677 25 }
mcimadamore@1677 26 }

mercurial