test/tools/javac/lambda/EffectivelyFinalTest.java

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

author
rfield
date
Tue, 14 May 2013 11:11:09 -0700
changeset 1752
c09b7234cded
parent 1415
01c9d4161882
child 2020
bb7271e64ef6
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@1297 1 /*
mcimadamore@1415 2 * @test /nodynamiccopyright/
mcimadamore@1415 3 * @bug 8003280
mcimadamore@1415 4 * @summary Add lambda tests
mcimadamore@1415 5 * Integrate effectively final check with DA/DU analysis
mcimadamore@1415 6 * @compile/fail/ref=EffectivelyFinalTest01.out -XDrawDiagnostics EffectivelyFinalTest.java
mcimadamore@1297 7 * @compile/fail/ref=EffectivelyFinalTest02.out -source 7 -Xlint:-options -XDrawDiagnostics EffectivelyFinalTest.java
mcimadamore@1297 8 */
mcimadamore@1297 9 class EffectivelyFinalTest {
mcimadamore@1297 10
mcimadamore@1297 11 void m1(int x) {
mcimadamore@1297 12 int y = 1;
mcimadamore@1297 13 new Object() { { System.out.println(x+y); } }; //ok - both x and y are EF
mcimadamore@1297 14 }
mcimadamore@1297 15
mcimadamore@1297 16 void m2(int x) {
mcimadamore@1297 17 int y;
mcimadamore@1297 18 y = 1;
mcimadamore@1297 19 new Object() { { System.out.println(x+y); } }; //ok - both x and y are EF
mcimadamore@1297 20 }
mcimadamore@1297 21
mcimadamore@1297 22 void m3(int x, boolean cond) {
mcimadamore@1297 23 int y;
mcimadamore@1297 24 if (cond) y = 1;
mcimadamore@1297 25 new Object() { { System.out.println(x+y); } }; //error - y not DA
mcimadamore@1297 26 }
mcimadamore@1297 27
mcimadamore@1297 28 void m4(int x, boolean cond) {
mcimadamore@1297 29 int y;
mcimadamore@1297 30 if (cond) y = 1;
mcimadamore@1297 31 else y = 2;
mcimadamore@1297 32 new Object() { { System.out.println(x+y); } }; //ok - both x and y are EF
mcimadamore@1297 33 }
mcimadamore@1297 34
mcimadamore@1297 35 void m5(int x, boolean cond) {
mcimadamore@1297 36 int y;
mcimadamore@1297 37 if (cond) y = 1;
mcimadamore@1297 38 y = 2;
mcimadamore@1297 39 new Object() { { System.out.println(x+y); } }; //error - y not EF
mcimadamore@1297 40 }
mcimadamore@1297 41
mcimadamore@1297 42 void m6(int x) {
mcimadamore@1297 43 new Object() { { System.out.println(x+1); } }; //error - x not EF
mcimadamore@1415 44 x++; // Illegal: x is not effectively final.
mcimadamore@1297 45 }
mcimadamore@1297 46
mcimadamore@1297 47 void m7(int x) {
mcimadamore@1297 48 new Object() { { System.out.println(x=1); } }; //error - x not EF
mcimadamore@1297 49 }
mcimadamore@1297 50
mcimadamore@1297 51 void m8() {
mcimadamore@1297 52 int y;
mcimadamore@1297 53 new Object() { { System.out.println(y=1); } }; //error - y not EF
mcimadamore@1297 54 }
mcimadamore@1297 55 }

mercurial