1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/mostSpecific/StructuralMostSpecificTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,304 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, 2013, 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 8006694 1.30 + * @summary Add lambda tests 1.31 + * Automatic test for checking correctness of structural most specific test routine 1.32 + * temporarily workaround combo tests are causing time out in several platforms 1.33 + * @library ../../lib 1.34 + * @build JavacTestingAbstractThreadedTest 1.35 + * @run main/othervm/timeout=600 StructuralMostSpecificTest 1.36 + */ 1.37 + 1.38 +// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) 1.39 +// see JDK-8006746 1.40 + 1.41 +import java.net.URI; 1.42 +import java.util.Arrays; 1.43 +import javax.tools.Diagnostic; 1.44 +import javax.tools.JavaFileObject; 1.45 +import javax.tools.SimpleJavaFileObject; 1.46 +import com.sun.source.util.JavacTask; 1.47 +import com.sun.tools.javac.api.ClientCodeWrapper; 1.48 +import com.sun.tools.javac.util.JCDiagnostic; 1.49 + 1.50 +public class StructuralMostSpecificTest 1.51 + extends JavacTestingAbstractThreadedTest 1.52 + implements Runnable { 1.53 + 1.54 + enum RetTypeKind { 1.55 + SHORT("short"), 1.56 + INT("int"), 1.57 + OBJECT("Object"), 1.58 + INTEGER("Integer"), 1.59 + VOID("void"), 1.60 + J_L_VOID("Void"); 1.61 + 1.62 + String retTypeStr; 1.63 + 1.64 + RetTypeKind(String retTypeStr) { 1.65 + this.retTypeStr = retTypeStr; 1.66 + } 1.67 + 1.68 + boolean moreSpecificThan(RetTypeKind rk) { 1.69 + return moreSpecificThan[this.ordinal()][rk.ordinal()]; 1.70 + } 1.71 + 1.72 + static boolean[][] moreSpecificThan = { 1.73 + // SHORT | INT | OBJECT | INTEGER | VOID | J_L_VOID 1.74 + /* SHORT */ { true , true , true , false , false , false }, 1.75 + /* INT */ { false , true , true , true , false , false }, 1.76 + /* OBJECT */ { false , false , true , false , false , false }, 1.77 + /* INTEGER */ { false , false , true , true , false , false }, 1.78 + /* VOID */ { false , false , false , false , true , true }, 1.79 + /* J_L_VOID */{ false , false , true , false , false , true } }; 1.80 + } 1.81 + 1.82 + enum ArgTypeKind { 1.83 + SHORT("short"), 1.84 + INT("int"), 1.85 + BOOLEAN("boolean"), 1.86 + OBJECT("Object"), 1.87 + INTEGER("Integer"), 1.88 + DOUBLE("Double"); 1.89 + 1.90 + String argTypeStr; 1.91 + 1.92 + ArgTypeKind(String typeStr) { 1.93 + this.argTypeStr = typeStr; 1.94 + } 1.95 + } 1.96 + 1.97 + enum ExceptionKind { 1.98 + NONE(""), 1.99 + EXCEPTION("throws Exception"), 1.100 + SQL_EXCEPTION("throws java.sql.SQLException"), 1.101 + IO_EXCEPTION("throws java.io.IOException"); 1.102 + 1.103 + String exceptionStr; 1.104 + 1.105 + ExceptionKind(String exceptionStr) { 1.106 + this.exceptionStr = exceptionStr; 1.107 + } 1.108 + } 1.109 + 1.110 + enum LambdaReturnKind { 1.111 + VOID("return;"), 1.112 + SHORT("return (short)0;"), 1.113 + INT("return 0;"), 1.114 + INTEGER("return (Integer)null;"), 1.115 + NULL("return null;"); 1.116 + 1.117 + String retStr; 1.118 + 1.119 + LambdaReturnKind(String retStr) { 1.120 + this.retStr = retStr; 1.121 + } 1.122 + 1.123 + boolean compatibleWith(RetTypeKind rk) { 1.124 + return compatibleWith[rk.ordinal()][ordinal()]; 1.125 + } 1.126 + 1.127 + static boolean[][] compatibleWith = { 1.128 + // VOID | SHORT | INT | INTEGER | NULL 1.129 + /* SHORT */ { false , true , false , false , false }, 1.130 + /* INT */ { false , true , true , true , false }, 1.131 + /* OBJECT */ { false , true , true , true , true }, 1.132 + /* INTEGER */ { false , false , true , true , true }, 1.133 + /* VOID */ { true , false , false , false , false }, 1.134 + /* J_L_VOID */{ false , false , false , false , true } }; 1.135 + 1.136 + boolean needsConversion(RetTypeKind rk) { 1.137 + return needsConversion[rk.ordinal()][ordinal()]; 1.138 + } 1.139 + 1.140 + static boolean[][] needsConversion = { 1.141 + // VOID | SHORT | INT | INTEGER | NULL 1.142 + /* SHORT */ { false , false , false , false , false }, 1.143 + /* INT */ { false , false , false , true , false }, 1.144 + /* OBJECT */ { false , true , true , false , false }, 1.145 + /* INTEGER */ { false , false , true , false , false }, 1.146 + /* VOID */ { false , false , false , false , false }, 1.147 + /* J_L_VOID */{ true , false , false , false , false } }; 1.148 + } 1.149 + 1.150 + public static void main(String... args) throws Exception { 1.151 + for (LambdaReturnKind lrk : LambdaReturnKind.values()) { 1.152 + for (RetTypeKind rk1 : RetTypeKind.values()) { 1.153 + for (RetTypeKind rk2 : RetTypeKind.values()) { 1.154 + for (ExceptionKind ek1 : ExceptionKind.values()) { 1.155 + for (ExceptionKind ek2 : ExceptionKind.values()) { 1.156 + for (ArgTypeKind ak11 : ArgTypeKind.values()) { 1.157 + for (ArgTypeKind ak12 : ArgTypeKind.values()) { 1.158 + pool.execute( 1.159 + new StructuralMostSpecificTest(lrk, rk1, 1.160 + rk2, ek1, ek2, ak11, ak12)); 1.161 + } 1.162 + } 1.163 + } 1.164 + } 1.165 + } 1.166 + } 1.167 + } 1.168 + 1.169 + checkAfterExec(); 1.170 + } 1.171 + 1.172 + LambdaReturnKind lrk; 1.173 + RetTypeKind rt1, rt2; 1.174 + ArgTypeKind ak1, ak2; 1.175 + ExceptionKind ek1, ek2; 1.176 + JavaSource source; 1.177 + DiagnosticChecker diagChecker; 1.178 + 1.179 + StructuralMostSpecificTest(LambdaReturnKind lrk, RetTypeKind rt1, RetTypeKind rt2, 1.180 + ExceptionKind ek1, ExceptionKind ek2, ArgTypeKind ak1, ArgTypeKind ak2) { 1.181 + this.lrk = lrk; 1.182 + this.rt1 = rt1; 1.183 + this.rt2 = rt2; 1.184 + this.ek1 = ek1; 1.185 + this.ek2 = ek2; 1.186 + this.ak1 = ak1; 1.187 + this.ak2 = ak2; 1.188 + this.source = new JavaSource(); 1.189 + this.diagChecker = new DiagnosticChecker(); 1.190 + } 1.191 + 1.192 + class JavaSource extends SimpleJavaFileObject { 1.193 + 1.194 + String template = "interface SAM1 {\n" + 1.195 + " #R1 m(#A1 a1) #E1;\n" + 1.196 + "}\n" + 1.197 + "interface SAM2 {\n" + 1.198 + " #R2 m(#A2 a1) #E2;\n" + 1.199 + "}\n" + 1.200 + "class Test {\n" + 1.201 + " void m(SAM1 s) { }\n" + 1.202 + " void m(SAM2 s) { }\n" + 1.203 + " { m((#A1 x)->{ #LR }); }\n" + 1.204 + "}\n"; 1.205 + 1.206 + String source; 1.207 + 1.208 + public JavaSource() { 1.209 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 1.210 + source = template.replaceAll("#LR", lrk.retStr) 1.211 + .replaceAll("#R1", rt1.retTypeStr) 1.212 + .replaceAll("#R2", rt2.retTypeStr) 1.213 + .replaceAll("#A1", ak1.argTypeStr) 1.214 + .replaceAll("#A2", ak2.argTypeStr) 1.215 + .replaceAll("#E1", ek1.exceptionStr) 1.216 + .replaceAll("#E2", ek2.exceptionStr); 1.217 + } 1.218 + 1.219 + @Override 1.220 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.221 + return source; 1.222 + } 1.223 + } 1.224 + 1.225 + public void run() { 1.226 + JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker, 1.227 + Arrays.asList("-XDverboseResolution=all,-predef,-internal,-object-init"), 1.228 + null, Arrays.asList(source)); 1.229 + try { 1.230 + ct.analyze(); 1.231 + } catch (Throwable ex) { 1.232 + throw new 1.233 + AssertionError("Error thron when analyzing the following source:\n" + 1.234 + source.getCharContent(true)); 1.235 + } 1.236 + check(); 1.237 + } 1.238 + 1.239 + void check() { 1.240 + checkCount.incrementAndGet(); 1.241 + 1.242 + if (ak1 != ak2) 1.243 + return; 1.244 + 1.245 + if (!lrk.compatibleWith(rt1) || !lrk.compatibleWith(rt2)) 1.246 + return; 1.247 + 1.248 + if (lrk.needsConversion(rt1) != lrk.needsConversion(rt2)) 1.249 + return; 1.250 + 1.251 + boolean m1MoreSpecific = rt1.moreSpecificThan(rt2); 1.252 + boolean m2MoreSpecific = rt2.moreSpecificThan(rt1); 1.253 + 1.254 + boolean ambiguous = (m1MoreSpecific == m2MoreSpecific); 1.255 + 1.256 + if (ambiguous != diagChecker.ambiguityFound) { 1.257 + throw new Error("invalid diagnostics for source:\n" + 1.258 + source.getCharContent(true) + 1.259 + "\nAmbiguity found: " + diagChecker.ambiguityFound + 1.260 + "\nm1 more specific: " + m1MoreSpecific + 1.261 + "\nm2 more specific: " + m2MoreSpecific + 1.262 + "\nexpected ambiguity: " + ambiguous); 1.263 + } 1.264 + 1.265 + if (!ambiguous) { 1.266 + String sigToCheck = m1MoreSpecific ? "m(SAM1)" : "m(SAM2)"; 1.267 + if (!sigToCheck.equals(diagChecker.mostSpecificSig)) { 1.268 + throw new Error("invalid most specific method selected:\n" + 1.269 + source.getCharContent(true) + 1.270 + "\nMost specific found: " + diagChecker.mostSpecificSig + 1.271 + "\nm1 more specific: " + m1MoreSpecific + 1.272 + "\nm2 more specific: " + m2MoreSpecific); 1.273 + } 1.274 + } 1.275 + } 1.276 + 1.277 + static class DiagnosticChecker 1.278 + implements javax.tools.DiagnosticListener<JavaFileObject> { 1.279 + 1.280 + boolean ambiguityFound; 1.281 + String mostSpecificSig; 1.282 + 1.283 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 1.284 + try { 1.285 + if (diagnostic.getKind() == Diagnostic.Kind.ERROR && 1.286 + diagnostic.getCode().equals("compiler.err.ref.ambiguous")) { 1.287 + ambiguityFound = true; 1.288 + } else if (diagnostic.getKind() == Diagnostic.Kind.NOTE && 1.289 + diagnostic.getCode() 1.290 + .equals("compiler.note.verbose.resolve.multi")) { 1.291 + ClientCodeWrapper.DiagnosticSourceUnwrapper dsu = 1.292 + (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic; 1.293 + JCDiagnostic.MultilineDiagnostic mdiag = 1.294 + (JCDiagnostic.MultilineDiagnostic)dsu.d; 1.295 + int mostSpecificIndex = (Integer)mdiag.getArgs()[2]; 1.296 + mostSpecificSig = 1.297 + ((JCDiagnostic)mdiag.getSubdiagnostics() 1.298 + .get(mostSpecificIndex)).getArgs()[1].toString(); 1.299 + } 1.300 + } catch (RuntimeException t) { 1.301 + t.printStackTrace(); 1.302 + throw t; 1.303 + } 1.304 + } 1.305 + } 1.306 + 1.307 +}