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 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 /**
27 * @test
28 * @bug 8003639
29 * @summary convert lambda testng tests to jtreg and add them
30 * @run testng MethodReferenceTestNew
31 */
33 import org.testng.annotations.Test;
35 import static org.testng.Assert.assertEquals;
37 /**
38 * @author Robert Field
39 */
41 @Test
42 public class MethodReferenceTestNew {
44 interface M0<T> {
46 T m();
47 }
49 static class N0 {
51 N0() {
52 }
53 }
55 interface M1<T> {
57 T m(Integer a);
58 }
60 static class N1 {
62 int i;
64 N1(int i) {
65 this.i = i;
66 }
67 }
69 interface M2<T> {
71 T m(Integer n, String o);
72 }
74 static class N2 {
76 Number n;
77 Object o;
79 N2(Number n, Object o) {
80 this.n = n;
81 this.o = o;
82 }
84 public String toString() {
85 return "N2(" + n + "," + o + ")";
86 }
87 }
89 interface MV {
91 NV m(Integer ai, int i);
92 }
94 static class NV {
96 int i;
98 NV(int... v) {
99 i = 0;
100 for (int x : v) {
101 i += x;
102 }
103 }
105 public String toString() {
106 return "NV(" + i + ")";
107 }
108 }
110 public void testConstructorReference0() {
111 M0<N0> q;
113 q = N0::new;
114 assertEquals(q.m().getClass().getSimpleName(), "N0");
115 }
117 public void testConstructorReference1() {
118 M1<N1> q;
120 q = N1::new;
121 assertEquals(q.m(14).getClass().getSimpleName(), "N1");
122 }
124 public void testConstructorReference2() {
125 M2<N2> q;
127 q = N2::new;
128 assertEquals(q.m(7, "hi").toString(), "N2(7,hi)");
129 }
131 public void testConstructorReferenceVarArgs() {
132 MV q;
134 q = NV::new;
135 assertEquals(q.m(5, 45).toString(), "NV(50)");
136 }
138 }