test/tools/javac/lambda/mostSpecific/StructuralMostSpecificTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2013, 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 8006694
aoqi@0 27 * @summary Add lambda tests
aoqi@0 28 * Automatic test for checking correctness of structural most specific test routine
aoqi@0 29 * temporarily workaround combo tests are causing time out in several platforms
aoqi@0 30 * @library ../../lib
aoqi@0 31 * @build JavacTestingAbstractThreadedTest
aoqi@0 32 * @run main/othervm/timeout=600 StructuralMostSpecificTest
aoqi@0 33 */
aoqi@0 34
aoqi@0 35 // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
aoqi@0 36 // see JDK-8006746
aoqi@0 37
aoqi@0 38 import java.net.URI;
aoqi@0 39 import java.util.Arrays;
aoqi@0 40 import javax.tools.Diagnostic;
aoqi@0 41 import javax.tools.JavaFileObject;
aoqi@0 42 import javax.tools.SimpleJavaFileObject;
aoqi@0 43 import com.sun.source.util.JavacTask;
aoqi@0 44 import com.sun.tools.javac.api.ClientCodeWrapper;
aoqi@0 45 import com.sun.tools.javac.util.JCDiagnostic;
aoqi@0 46
aoqi@0 47 public class StructuralMostSpecificTest
aoqi@0 48 extends JavacTestingAbstractThreadedTest
aoqi@0 49 implements Runnable {
aoqi@0 50
aoqi@0 51 enum RetTypeKind {
aoqi@0 52 SHORT("short"),
aoqi@0 53 INT("int"),
aoqi@0 54 OBJECT("Object"),
aoqi@0 55 INTEGER("Integer"),
aoqi@0 56 VOID("void"),
aoqi@0 57 J_L_VOID("Void");
aoqi@0 58
aoqi@0 59 String retTypeStr;
aoqi@0 60
aoqi@0 61 RetTypeKind(String retTypeStr) {
aoqi@0 62 this.retTypeStr = retTypeStr;
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 boolean moreSpecificThan(RetTypeKind rk) {
aoqi@0 66 return moreSpecificThan[this.ordinal()][rk.ordinal()];
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 static boolean[][] moreSpecificThan = {
aoqi@0 70 // SHORT | INT | OBJECT | INTEGER | VOID | J_L_VOID
aoqi@0 71 /* SHORT */ { true , true , true , false , false , false },
aoqi@0 72 /* INT */ { false , true , true , true , false , false },
aoqi@0 73 /* OBJECT */ { false , false , true , false , false , false },
aoqi@0 74 /* INTEGER */ { false , false , true , true , false , false },
aoqi@0 75 /* VOID */ { false , false , false , false , true , true },
aoqi@0 76 /* J_L_VOID */{ false , false , true , false , false , true } };
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 enum ArgTypeKind {
aoqi@0 80 SHORT("short"),
aoqi@0 81 INT("int"),
aoqi@0 82 BOOLEAN("boolean"),
aoqi@0 83 OBJECT("Object"),
aoqi@0 84 INTEGER("Integer"),
aoqi@0 85 DOUBLE("Double");
aoqi@0 86
aoqi@0 87 String argTypeStr;
aoqi@0 88
aoqi@0 89 ArgTypeKind(String typeStr) {
aoqi@0 90 this.argTypeStr = typeStr;
aoqi@0 91 }
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 enum ExceptionKind {
aoqi@0 95 NONE(""),
aoqi@0 96 EXCEPTION("throws Exception"),
aoqi@0 97 SQL_EXCEPTION("throws java.sql.SQLException"),
aoqi@0 98 IO_EXCEPTION("throws java.io.IOException");
aoqi@0 99
aoqi@0 100 String exceptionStr;
aoqi@0 101
aoqi@0 102 ExceptionKind(String exceptionStr) {
aoqi@0 103 this.exceptionStr = exceptionStr;
aoqi@0 104 }
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 enum LambdaReturnKind {
aoqi@0 108 VOID("return;"),
aoqi@0 109 SHORT("return (short)0;"),
aoqi@0 110 INT("return 0;"),
aoqi@0 111 INTEGER("return (Integer)null;"),
aoqi@0 112 NULL("return null;");
aoqi@0 113
aoqi@0 114 String retStr;
aoqi@0 115
aoqi@0 116 LambdaReturnKind(String retStr) {
aoqi@0 117 this.retStr = retStr;
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 boolean compatibleWith(RetTypeKind rk) {
aoqi@0 121 return compatibleWith[rk.ordinal()][ordinal()];
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 static boolean[][] compatibleWith = {
aoqi@0 125 // VOID | SHORT | INT | INTEGER | NULL
aoqi@0 126 /* SHORT */ { false , true , false , false , false },
aoqi@0 127 /* INT */ { false , true , true , true , false },
aoqi@0 128 /* OBJECT */ { false , true , true , true , true },
aoqi@0 129 /* INTEGER */ { false , false , true , true , true },
aoqi@0 130 /* VOID */ { true , false , false , false , false },
aoqi@0 131 /* J_L_VOID */{ false , false , false , false , true } };
aoqi@0 132
aoqi@0 133 boolean needsConversion(RetTypeKind rk) {
aoqi@0 134 return needsConversion[rk.ordinal()][ordinal()];
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 static boolean[][] needsConversion = {
aoqi@0 138 // VOID | SHORT | INT | INTEGER | NULL
aoqi@0 139 /* SHORT */ { false , false , false , false , false },
aoqi@0 140 /* INT */ { false , false , false , true , false },
aoqi@0 141 /* OBJECT */ { false , true , true , false , false },
aoqi@0 142 /* INTEGER */ { false , false , true , false , false },
aoqi@0 143 /* VOID */ { false , false , false , false , false },
aoqi@0 144 /* J_L_VOID */{ true , false , false , false , false } };
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 public static void main(String... args) throws Exception {
aoqi@0 148 for (LambdaReturnKind lrk : LambdaReturnKind.values()) {
aoqi@0 149 for (RetTypeKind rk1 : RetTypeKind.values()) {
aoqi@0 150 for (RetTypeKind rk2 : RetTypeKind.values()) {
aoqi@0 151 for (ExceptionKind ek1 : ExceptionKind.values()) {
aoqi@0 152 for (ExceptionKind ek2 : ExceptionKind.values()) {
aoqi@0 153 for (ArgTypeKind ak11 : ArgTypeKind.values()) {
aoqi@0 154 for (ArgTypeKind ak12 : ArgTypeKind.values()) {
aoqi@0 155 pool.execute(
aoqi@0 156 new StructuralMostSpecificTest(lrk, rk1,
aoqi@0 157 rk2, ek1, ek2, ak11, ak12));
aoqi@0 158 }
aoqi@0 159 }
aoqi@0 160 }
aoqi@0 161 }
aoqi@0 162 }
aoqi@0 163 }
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 checkAfterExec();
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 LambdaReturnKind lrk;
aoqi@0 170 RetTypeKind rt1, rt2;
aoqi@0 171 ArgTypeKind ak1, ak2;
aoqi@0 172 ExceptionKind ek1, ek2;
aoqi@0 173 JavaSource source;
aoqi@0 174 DiagnosticChecker diagChecker;
aoqi@0 175
aoqi@0 176 StructuralMostSpecificTest(LambdaReturnKind lrk, RetTypeKind rt1, RetTypeKind rt2,
aoqi@0 177 ExceptionKind ek1, ExceptionKind ek2, ArgTypeKind ak1, ArgTypeKind ak2) {
aoqi@0 178 this.lrk = lrk;
aoqi@0 179 this.rt1 = rt1;
aoqi@0 180 this.rt2 = rt2;
aoqi@0 181 this.ek1 = ek1;
aoqi@0 182 this.ek2 = ek2;
aoqi@0 183 this.ak1 = ak1;
aoqi@0 184 this.ak2 = ak2;
aoqi@0 185 this.source = new JavaSource();
aoqi@0 186 this.diagChecker = new DiagnosticChecker();
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 class JavaSource extends SimpleJavaFileObject {
aoqi@0 190
aoqi@0 191 String template = "interface SAM1 {\n" +
aoqi@0 192 " #R1 m(#A1 a1) #E1;\n" +
aoqi@0 193 "}\n" +
aoqi@0 194 "interface SAM2 {\n" +
aoqi@0 195 " #R2 m(#A2 a1) #E2;\n" +
aoqi@0 196 "}\n" +
aoqi@0 197 "class Test {\n" +
aoqi@0 198 " void m(SAM1 s) { }\n" +
aoqi@0 199 " void m(SAM2 s) { }\n" +
aoqi@0 200 " { m((#A1 x)->{ #LR }); }\n" +
aoqi@0 201 "}\n";
aoqi@0 202
aoqi@0 203 String source;
aoqi@0 204
aoqi@0 205 public JavaSource() {
aoqi@0 206 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 207 source = template.replaceAll("#LR", lrk.retStr)
aoqi@0 208 .replaceAll("#R1", rt1.retTypeStr)
aoqi@0 209 .replaceAll("#R2", rt2.retTypeStr)
aoqi@0 210 .replaceAll("#A1", ak1.argTypeStr)
aoqi@0 211 .replaceAll("#A2", ak2.argTypeStr)
aoqi@0 212 .replaceAll("#E1", ek1.exceptionStr)
aoqi@0 213 .replaceAll("#E2", ek2.exceptionStr);
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 @Override
aoqi@0 217 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 218 return source;
aoqi@0 219 }
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 public void run() {
aoqi@0 223 JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
aoqi@0 224 Arrays.asList("-XDverboseResolution=all,-predef,-internal,-object-init"),
aoqi@0 225 null, Arrays.asList(source));
aoqi@0 226 try {
aoqi@0 227 ct.analyze();
aoqi@0 228 } catch (Throwable ex) {
aoqi@0 229 throw new
aoqi@0 230 AssertionError("Error thron when analyzing the following source:\n" +
aoqi@0 231 source.getCharContent(true));
aoqi@0 232 }
aoqi@0 233 check();
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 void check() {
aoqi@0 237 checkCount.incrementAndGet();
aoqi@0 238
aoqi@0 239 if (ak1 != ak2)
aoqi@0 240 return;
aoqi@0 241
aoqi@0 242 if (!lrk.compatibleWith(rt1) || !lrk.compatibleWith(rt2))
aoqi@0 243 return;
aoqi@0 244
aoqi@0 245 if (lrk.needsConversion(rt1) != lrk.needsConversion(rt2))
aoqi@0 246 return;
aoqi@0 247
aoqi@0 248 boolean m1MoreSpecific = rt1.moreSpecificThan(rt2);
aoqi@0 249 boolean m2MoreSpecific = rt2.moreSpecificThan(rt1);
aoqi@0 250
aoqi@0 251 boolean ambiguous = (m1MoreSpecific == m2MoreSpecific);
aoqi@0 252
aoqi@0 253 if (ambiguous != diagChecker.ambiguityFound) {
aoqi@0 254 throw new Error("invalid diagnostics for source:\n" +
aoqi@0 255 source.getCharContent(true) +
aoqi@0 256 "\nAmbiguity found: " + diagChecker.ambiguityFound +
aoqi@0 257 "\nm1 more specific: " + m1MoreSpecific +
aoqi@0 258 "\nm2 more specific: " + m2MoreSpecific +
aoqi@0 259 "\nexpected ambiguity: " + ambiguous);
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 if (!ambiguous) {
aoqi@0 263 String sigToCheck = m1MoreSpecific ? "m(SAM1)" : "m(SAM2)";
aoqi@0 264 if (!sigToCheck.equals(diagChecker.mostSpecificSig)) {
aoqi@0 265 throw new Error("invalid most specific method selected:\n" +
aoqi@0 266 source.getCharContent(true) +
aoqi@0 267 "\nMost specific found: " + diagChecker.mostSpecificSig +
aoqi@0 268 "\nm1 more specific: " + m1MoreSpecific +
aoqi@0 269 "\nm2 more specific: " + m2MoreSpecific);
aoqi@0 270 }
aoqi@0 271 }
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 static class DiagnosticChecker
aoqi@0 275 implements javax.tools.DiagnosticListener<JavaFileObject> {
aoqi@0 276
aoqi@0 277 boolean ambiguityFound;
aoqi@0 278 String mostSpecificSig;
aoqi@0 279
aoqi@0 280 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
aoqi@0 281 try {
aoqi@0 282 if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
aoqi@0 283 diagnostic.getCode().equals("compiler.err.ref.ambiguous")) {
aoqi@0 284 ambiguityFound = true;
aoqi@0 285 } else if (diagnostic.getKind() == Diagnostic.Kind.NOTE &&
aoqi@0 286 diagnostic.getCode()
aoqi@0 287 .equals("compiler.note.verbose.resolve.multi")) {
aoqi@0 288 ClientCodeWrapper.DiagnosticSourceUnwrapper dsu =
aoqi@0 289 (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic;
aoqi@0 290 JCDiagnostic.MultilineDiagnostic mdiag =
aoqi@0 291 (JCDiagnostic.MultilineDiagnostic)dsu.d;
aoqi@0 292 int mostSpecificIndex = (Integer)mdiag.getArgs()[2];
aoqi@0 293 mostSpecificSig =
aoqi@0 294 ((JCDiagnostic)mdiag.getSubdiagnostics()
aoqi@0 295 .get(mostSpecificIndex)).getArgs()[1].toString();
aoqi@0 296 }
aoqi@0 297 } catch (RuntimeException t) {
aoqi@0 298 t.printStackTrace();
aoqi@0 299 throw t;
aoqi@0 300 }
aoqi@0 301 }
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 }

mercurial