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) 2011, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /**
25 * @test
26 * @bug 8003280
27 * @summary Add lambda tests
28 * This test is for identifying SAM types #4, see Helper.java for SAM types
29 * @compile LambdaTest2_SAM2.java Helper.java
30 * @run main LambdaTest2_SAM2
31 */
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.ArrayList;
36 import java.util.concurrent.TimeoutException;
37 import java.io.*;
38 import java.sql.SQLException;
39 import java.sql.SQLTransientException;
41 public class LambdaTest2_SAM2 {
42 private static List<String> strs = new ArrayList<String>();
44 public static void main(String[] args) {
45 strs.add("copy");
46 strs.add("paste");
47 strs.add("delete");
48 strs.add("rename");
50 LambdaTest2_SAM2 test = new LambdaTest2_SAM2();
52 //type #4 a):
53 test.methodAB((List list) -> 100);
55 //type #4 b):
56 test.methodFGHI((String s) -> new Integer(22));
57 //type #4 b):
58 test.methodJK((String s) -> new ArrayList<Number>());
59 test.methodJK((String s) -> new ArrayList());
60 //type #4 b):
61 test.methodJL((String s) -> new ArrayList<Number>());
62 test.methodJL((String s) -> new ArrayList());
63 //type #4 b):
64 test.methodJKL((String s) -> new ArrayList<Number>());
65 test.methodJKL((String s) -> new ArrayList());
66 //type #4 b):
67 test.methodJKLM((String s) -> new ArrayList<Number>());
68 test.methodJKLM((String s) -> new ArrayList());
70 // tyep #4 c):
71 test.methodNO((File f) -> {
72 String temp = null;
73 StringBuffer sb = new StringBuffer();
74 try
75 {
76 BufferedReader br = new BufferedReader(new FileReader(f));
77 while((temp=br.readLine()) != null)
78 sb.append(temp).append("\n");
79 }
80 catch(FileNotFoundException fne){throw fne;}
81 catch(IOException e){e.printStackTrace();}
82 return sb.toString();
83 });
84 // tyep #4 c):
85 test.methodNOP((File f) -> {
86 String temp = null;
87 StringBuffer sb = new StringBuffer();
88 try
89 {
90 BufferedReader br = new BufferedReader(new FileReader(f));
91 while((temp=br.readLine()) != null)
92 sb.append(temp).append("\n");
93 }
94 catch(IOException e){e.printStackTrace();}
95 return sb.toString();
96 });
97 // type #4 c):
98 test.methodBooDoo((String s) -> s.length());
100 //type #4 d):
101 test.methodQR((Iterable i) -> new ArrayList<String>());
102 test.methodQR((Iterable i) -> new ArrayList());
103 //type #4 d):
104 test.methodUV((List<String> list) -> {
105 test.exceptionMethod1();
106 test.exceptionMethod2();
107 return new ArrayList<String>();
108 });
109 test.methodUV((List<String> list) -> {
110 test.exceptionMethod1();
111 test.exceptionMethod2();
112 return new ArrayList();
113 });
114 //type #4 d):
115 test.methodUVW((List list) -> {
116 test.exceptionMethod1();
117 test.exceptionMethod2();
118 return new ArrayList<String>();
119 });
120 test.methodUVW((List list) -> {
121 test.exceptionMethod1();
122 test.exceptionMethod2();
123 return new ArrayList();
124 });
125 }
127 private void exceptionMethod1() throws EOFException{
128 }
130 private void exceptionMethod2() throws SQLTransientException{
131 }
133 //type #4 a): SAM type ([List], int, {})
134 void methodAB (AB ab) {
135 System.out.println("methodAB(): SAM type interface AB object instantiated: " + ab);
136 System.out.println(ab.getOldest(strs));
137 }
139 //type #4 b): SAM type ([String], Integer, {})
140 void methodFGHI(FGHI f) {
141 System.out.println("methodFGHI(): SAM type interface FGHI object instantiated: " + f);
142 System.out.println(f.getValue("str"));
143 }
145 //type #4 b): SAM type ([String], List<Number>, {})
146 void methodJK(JK jk) {
147 System.out.println("methodJK(): SAM type interface JK object instantiated: " + jk);
148 for(Number n : jk.getAll("in"))
149 System.out.println(n);
150 }
152 //type #4 b): SAM type ([String], List<Number>, {})
153 void methodJL(JL jl) {
154 System.out.println("methodJL(): SAM type interface JL object instantiated: " + jl);
155 for(Number n : ((J)jl).getAll("in")) //cast should be redundant - see 7062745
156 System.out.println(n);
157 }
159 //type #4 b): SAM type ([String], List<Number>, {})
160 void methodJKL(JKL jkl) { //commented - see 7062745
161 System.out.println("methodJKL(): SAM type interface JKL object instantiated: " + jkl);
162 for(Number n : ((J)jkl).getAll("in"))
163 System.out.println(n);
164 }
166 //type #4 b): SAM type ([String], List<Number>, {})
167 void methodJKLM(JKLM jklm) { //commented - see 7062745
168 System.out.println("methodJKLM(): SAM type interface JKLM object instantiated: " + jklm);
169 for(Number n : ((J)jklm).getAll("in"))
170 System.out.println(n);
171 }
173 //type #4 c): SAM type ([File], String, {FileNotFoundException})
174 void methodNO(NO no) {
175 System.out.println("methodNO(): SAM type interface \"NO\" object instantiated: " + no);
176 try {
177 System.out.println("text=" + no.getText(new File("a.txt")));
178 System.out.println("got here, no exception thrown");
179 }
180 catch(FileNotFoundException e){e.printStackTrace();}
181 }
183 //type #4 c): SAM type ([File]), String, {})
184 void methodNOP(NOP nop) {
185 System.out.println("methodNOP(): SAM type interface \"NOP\" object instantiated: " + nop);
186 System.out.println("text=" + nop.getText(new File("a.txt")));
187 }
189 //type #4 c): SAM type ([String], int, {})
190 void methodBooDoo(BooDoo bd) {
191 System.out.println("methodBooDoo(): SAM type interface BooDoo object instantiated: " + bd);
192 System.out.println("result=" + bd.getAge("lambda"));
193 }
195 //type #4 d): SAM type ([Iterable], Iterable<String>, {})
196 void methodQR(QR qr) {
197 System.out.println("methodQR(): SAM type interface QR object instantiated: " + qr);
198 System.out.println("Iterable returned: " + qr.m(new SQLException()));
199 }
201 //type #4 d): SAM type ([List<String>], List<String>/List, {EOFException, SQLTransientException})
202 void methodUV(UV uv) {
203 System.out.println("methodUV(): SAM type interface UV object instantiated: " + uv);
204 try{
205 System.out.println("result returned: " + uv.foo(strs));
206 }catch(EOFException e){
207 System.out.println(e.getMessage());
208 }catch(SQLTransientException ex){
209 System.out.println(ex.getMessage());
210 }
211 }
213 //type #4 d): SAM type ([List], List<String>/List, {EOFException, SQLTransientException})
214 void methodUVW(UVW uvw) {
215 System.out.println("methodUVW(): SAM type interface UVW object instantiated: " + uvw);
216 try{
217 System.out.println("passing List<String>: " + uvw.foo(strs));
218 System.out.println("passing List: " + uvw.foo(new ArrayList()));
219 }catch(EOFException e){
220 System.out.println(e.getMessage());
221 }catch(SQLTransientException ex){
222 System.out.println(ex.getMessage());
223 }
224 }
225 }