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

mercurial