1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/FunctionalInterfaceConversionTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,309 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * @test 1.29 + * @bug 8003280 8004102 8006694 1.30 + * @summary Add lambda tests 1.31 + * perform several automated checks in lambda conversion, esp. around accessibility 1.32 + * temporarily workaround combo tests are causing time out in several platforms 1.33 + * @author Maurizio Cimadamore 1.34 + * @library ../lib 1.35 + * @build JavacTestingAbstractThreadedTest 1.36 + * @run main/timeout=600/othervm FunctionalInterfaceConversionTest 1.37 + */ 1.38 + 1.39 +// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) 1.40 +// see JDK-8006746 1.41 + 1.42 +import java.io.IOException; 1.43 +import java.net.URI; 1.44 +import java.util.Arrays; 1.45 +import javax.tools.Diagnostic; 1.46 +import javax.tools.JavaCompiler; 1.47 +import javax.tools.JavaFileObject; 1.48 +import javax.tools.SimpleJavaFileObject; 1.49 +import javax.tools.ToolProvider; 1.50 +import com.sun.source.util.JavacTask; 1.51 + 1.52 +public class FunctionalInterfaceConversionTest 1.53 + extends JavacTestingAbstractThreadedTest 1.54 + implements Runnable { 1.55 + 1.56 + enum PackageKind { 1.57 + NO_PKG(""), 1.58 + PKG_A("a"); 1.59 + 1.60 + String pkg; 1.61 + 1.62 + PackageKind(String pkg) { 1.63 + this.pkg = pkg; 1.64 + } 1.65 + 1.66 + String getPkgDecl() { 1.67 + return this == NO_PKG ? 1.68 + "" : 1.69 + "package " + pkg + ";"; 1.70 + } 1.71 + 1.72 + String getImportStat() { 1.73 + return this == NO_PKG ? 1.74 + "" : 1.75 + "import " + pkg + ".*;"; 1.76 + } 1.77 + } 1.78 + 1.79 + enum SamKind { 1.80 + CLASS("public class Sam { }"), 1.81 + ABSTACT_CLASS("public abstract class Sam { }"), 1.82 + ANNOTATION("public @interface Sam { }"), 1.83 + ENUM("public enum Sam { }"), 1.84 + INTERFACE("public interface Sam { \n #METH; \n }"); 1.85 + 1.86 + String sam_str; 1.87 + 1.88 + SamKind(String sam_str) { 1.89 + this.sam_str = sam_str; 1.90 + } 1.91 + 1.92 + String getSam(String methStr) { 1.93 + return sam_str.replaceAll("#METH", methStr); 1.94 + } 1.95 + } 1.96 + 1.97 + enum ModifierKind { 1.98 + PUBLIC("public"), 1.99 + PACKAGE(""); 1.100 + 1.101 + String modifier_str; 1.102 + 1.103 + ModifierKind(String modifier_str) { 1.104 + this.modifier_str = modifier_str; 1.105 + } 1.106 + 1.107 + boolean stricterThan(ModifierKind that) { 1.108 + return this.ordinal() > that.ordinal(); 1.109 + } 1.110 + } 1.111 + 1.112 + enum TypeKind { 1.113 + EXCEPTION("Exception"), 1.114 + PKG_CLASS("PackageClass"); 1.115 + 1.116 + String typeStr; 1.117 + 1.118 + private TypeKind(String typeStr) { 1.119 + this.typeStr = typeStr; 1.120 + } 1.121 + } 1.122 + 1.123 + enum ExprKind { 1.124 + LAMBDA("x -> null"), 1.125 + MREF("this::m"); 1.126 + 1.127 + String exprStr; 1.128 + 1.129 + private ExprKind(String exprStr) { 1.130 + this.exprStr = exprStr; 1.131 + } 1.132 + } 1.133 + 1.134 + enum MethodKind { 1.135 + NONE(""), 1.136 + NON_GENERIC("public abstract #R m(#ARG s) throws #T;"), 1.137 + GENERIC("public abstract <X> #R m(#ARG s) throws #T;"); 1.138 + 1.139 + String methodTemplate; 1.140 + 1.141 + private MethodKind(String methodTemplate) { 1.142 + this.methodTemplate = methodTemplate; 1.143 + } 1.144 + 1.145 + String getMethod(TypeKind retType, TypeKind argType, TypeKind thrownType) { 1.146 + return methodTemplate.replaceAll("#R", retType.typeStr). 1.147 + replaceAll("#ARG", argType.typeStr). 1.148 + replaceAll("#T", thrownType.typeStr); 1.149 + } 1.150 + } 1.151 + 1.152 + public static void main(String[] args) throws Exception { 1.153 + for (PackageKind samPkg : PackageKind.values()) { 1.154 + for (ModifierKind modKind : ModifierKind.values()) { 1.155 + for (SamKind samKind : SamKind.values()) { 1.156 + for (MethodKind samMeth : MethodKind.values()) { 1.157 + for (MethodKind clientMeth : MethodKind.values()) { 1.158 + for (TypeKind retType : TypeKind.values()) { 1.159 + for (TypeKind argType : TypeKind.values()) { 1.160 + for (TypeKind thrownType : TypeKind.values()) { 1.161 + for (ExprKind exprKind : ExprKind.values()) { 1.162 + pool.execute( 1.163 + new FunctionalInterfaceConversionTest( 1.164 + samPkg, modKind, samKind, 1.165 + samMeth, clientMeth, retType, 1.166 + argType, thrownType, exprKind)); 1.167 + } 1.168 + } 1.169 + } 1.170 + } 1.171 + } 1.172 + } 1.173 + } 1.174 + } 1.175 + } 1.176 + 1.177 + checkAfterExec(false); 1.178 + } 1.179 + 1.180 + PackageKind samPkg; 1.181 + ModifierKind modKind; 1.182 + SamKind samKind; 1.183 + MethodKind samMeth; 1.184 + MethodKind clientMeth; 1.185 + TypeKind retType; 1.186 + TypeKind argType; 1.187 + TypeKind thrownType; 1.188 + ExprKind exprKind; 1.189 + DiagnosticChecker dc; 1.190 + 1.191 + SourceFile samSourceFile = new SourceFile("Sam.java", "#P \n #C") { 1.192 + @Override 1.193 + public String toString() { 1.194 + return template.replaceAll("#P", samPkg.getPkgDecl()). 1.195 + replaceAll("#C", samKind.getSam( 1.196 + samMeth.getMethod(retType, argType, thrownType))); 1.197 + } 1.198 + }; 1.199 + 1.200 + SourceFile pkgClassSourceFile = 1.201 + new SourceFile("PackageClass.java", 1.202 + "#P\n #M class PackageClass extends Exception { }") { 1.203 + @Override 1.204 + public String toString() { 1.205 + return template.replaceAll("#P", samPkg.getPkgDecl()). 1.206 + replaceAll("#M", modKind.modifier_str); 1.207 + } 1.208 + }; 1.209 + 1.210 + SourceFile clientSourceFile = 1.211 + new SourceFile("Client.java", 1.212 + "#I\n abstract class Client { \n" + 1.213 + " Sam s = #E;\n" + 1.214 + " #M \n }") { 1.215 + @Override 1.216 + public String toString() { 1.217 + return template.replaceAll("#I", samPkg.getImportStat()) 1.218 + .replaceAll("#E", exprKind.exprStr) 1.219 + .replaceAll("#M", clientMeth.getMethod(retType, argType, thrownType)); 1.220 + } 1.221 + }; 1.222 + 1.223 + FunctionalInterfaceConversionTest(PackageKind samPkg, ModifierKind modKind, 1.224 + SamKind samKind, MethodKind samMeth, MethodKind clientMeth, 1.225 + TypeKind retType, TypeKind argType, TypeKind thrownType, 1.226 + ExprKind exprKind) { 1.227 + this.samPkg = samPkg; 1.228 + this.modKind = modKind; 1.229 + this.samKind = samKind; 1.230 + this.samMeth = samMeth; 1.231 + this.clientMeth = clientMeth; 1.232 + this.retType = retType; 1.233 + this.argType = argType; 1.234 + this.thrownType = thrownType; 1.235 + this.exprKind = exprKind; 1.236 + this.dc = new DiagnosticChecker(); 1.237 + } 1.238 + 1.239 + @Override 1.240 + public void run() { 1.241 + final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 1.242 + 1.243 + JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), dc, null, null, 1.244 + Arrays.asList(samSourceFile, pkgClassSourceFile, clientSourceFile)); 1.245 + try { 1.246 + ct.analyze(); 1.247 + } catch (IOException ex) { 1.248 + throw new AssertionError("Test failing with cause", ex.getCause()); 1.249 + } 1.250 + if (dc.errorFound == checkSamConversion()) { 1.251 + throw new AssertionError(samSourceFile + "\n\n" + 1.252 + pkgClassSourceFile + "\n\n" + clientSourceFile); 1.253 + } 1.254 + } 1.255 + 1.256 + boolean checkSamConversion() { 1.257 + if (samKind != SamKind.INTERFACE) { 1.258 + //sam type must be an interface 1.259 + return false; 1.260 + } else if (samMeth == MethodKind.NONE) { 1.261 + //interface must have at least a method 1.262 + return false; 1.263 + } else if (exprKind == ExprKind.LAMBDA && 1.264 + samMeth != MethodKind.NON_GENERIC) { 1.265 + //target method for lambda must be non-generic 1.266 + return false; 1.267 + } else if (exprKind == ExprKind.MREF && 1.268 + clientMeth == MethodKind.NONE) { 1.269 + return false; 1.270 + } else if (samPkg != PackageKind.NO_PKG && 1.271 + modKind != ModifierKind.PUBLIC && 1.272 + (retType == TypeKind.PKG_CLASS || 1.273 + argType == TypeKind.PKG_CLASS || 1.274 + thrownType == TypeKind.PKG_CLASS)) { 1.275 + //target must not contain inaccessible types 1.276 + return false; 1.277 + } else { 1.278 + return true; 1.279 + } 1.280 + } 1.281 + 1.282 + abstract class SourceFile extends SimpleJavaFileObject { 1.283 + 1.284 + protected String template; 1.285 + 1.286 + public SourceFile(String filename, String template) { 1.287 + super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE); 1.288 + this.template = template; 1.289 + } 1.290 + 1.291 + @Override 1.292 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.293 + return toString(); 1.294 + } 1.295 + 1.296 + @Override 1.297 + public abstract String toString(); 1.298 + } 1.299 + 1.300 + static class DiagnosticChecker 1.301 + implements javax.tools.DiagnosticListener<JavaFileObject> { 1.302 + 1.303 + boolean errorFound = false; 1.304 + 1.305 + @Override 1.306 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 1.307 + if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { 1.308 + errorFound = true; 1.309 + } 1.310 + } 1.311 + } 1.312 +}