test/tools/javac/lambda/typeInference/combo/TypeInferenceComboTest.java

Tue, 08 Jan 2013 13:47:57 +0000

author
vromero
date
Tue, 08 Jan 2013 13:47:57 +0000
changeset 1482
954541f13717
parent 1415
01c9d4161882
child 1520
5c956be64b9e
permissions
-rw-r--r--

8005167: execution time of combo tests in javac should be improved
Reviewed-by: jjg, jjh

mcimadamore@1415 1 /*
vromero@1482 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1415 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1415 4 *
mcimadamore@1415 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1415 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1415 7 * published by the Free Software Foundation.
mcimadamore@1415 8 *
mcimadamore@1415 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1415 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1415 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1415 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1415 13 * accompanied this code).
mcimadamore@1415 14 *
mcimadamore@1415 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1415 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1415 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1415 18 *
mcimadamore@1415 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1415 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1415 21 * questions.
mcimadamore@1415 22 */
mcimadamore@1415 23
mcimadamore@1415 24 /**
mcimadamore@1415 25 * @test
mcimadamore@1415 26 * @bug 8003280
mcimadamore@1415 27 * @summary Add lambda tests
vromero@1482 28 * perform automated checks in type inference in lambda expressions
vromero@1482 29 * in different contexts
vromero@1482 30 * @library ../../../lib
vromero@1482 31 * @build JavacTestingAbstractThreadedTest
mcimadamore@1415 32 * @compile TypeInferenceComboTest.java
mcimadamore@1415 33 * @run main/timeout=360 TypeInferenceComboTest
mcimadamore@1415 34 */
mcimadamore@1415 35
mcimadamore@1415 36 import java.net.URI;
mcimadamore@1415 37 import java.util.Arrays;
mcimadamore@1415 38 import javax.tools.Diagnostic;
mcimadamore@1415 39 import javax.tools.JavaFileObject;
mcimadamore@1415 40 import javax.tools.SimpleJavaFileObject;
vromero@1482 41 import com.sun.source.util.JavacTask;
mcimadamore@1415 42
vromero@1482 43 public class TypeInferenceComboTest
vromero@1482 44 extends JavacTestingAbstractThreadedTest
vromero@1482 45 implements Runnable {
mcimadamore@1415 46 enum Context {
mcimadamore@1415 47 ASSIGNMENT("SAM#Type s = #LBody;"),
mcimadamore@1415 48 METHOD_CALL("#GenericDeclKind void method1(SAM#Type s) { }\n" +
mcimadamore@1415 49 "void method2() {\n" +
mcimadamore@1415 50 " method1(#LBody);\n" +
mcimadamore@1415 51 "}"),
mcimadamore@1415 52 RETURN_OF_METHOD("SAM#Type method1() {\n" +
mcimadamore@1415 53 " return #LBody;\n" +
mcimadamore@1415 54 "}"),
mcimadamore@1415 55 LAMBDA_RETURN_EXPRESSION("SAM2 s2 = () -> {return (SAM#Type)#LBody;};\n"),
mcimadamore@1415 56 ARRAY_INITIALIZER("Object[] oarray = {\"a\", 1, (SAM#Type)#LBody};");
mcimadamore@1415 57
mcimadamore@1415 58 String context;
mcimadamore@1415 59
mcimadamore@1415 60 Context(String context) {
mcimadamore@1415 61 this.context = context;
mcimadamore@1415 62 }
mcimadamore@1415 63
vromero@1482 64 String getContext(SamKind sk, TypeKind samTargetT, Keyword kw,
vromero@1482 65 TypeKind parameterT, TypeKind returnT, LambdaKind lk,
vromero@1482 66 ParameterKind pk, GenericDeclKind gdk, LambdaBody lb) {
mcimadamore@1415 67 String result = context;
mcimadamore@1415 68 if (sk == SamKind.GENERIC) {
mcimadamore@1415 69 if(this == Context.METHOD_CALL) {
vromero@1482 70 result = result.replaceAll("#GenericDeclKind",
vromero@1482 71 gdk.getGenericDeclKind(samTargetT));
mcimadamore@1415 72 if(gdk == GenericDeclKind.NON_GENERIC)
vromero@1482 73 result = result.replaceAll("#Type", "<" +
vromero@1482 74 samTargetT.typeStr + ">");
mcimadamore@1415 75 else //#GenericDeclKind is <T> or <T extends xxx>
mcimadamore@1415 76 result = result.replaceAll("#Type", "<T>");
mcimadamore@1415 77 }
mcimadamore@1415 78 else {
mcimadamore@1415 79 if(kw == Keyword.VOID)
vromero@1482 80 result = result.replaceAll("#Type", "<" +
vromero@1482 81 samTargetT.typeStr + ">");
mcimadamore@1415 82 else
vromero@1482 83 result = result.replaceAll("#Type", "<? " + kw.keyStr +
vromero@1482 84 " " + samTargetT.typeStr + ">");
mcimadamore@1415 85 }
mcimadamore@1415 86 }
mcimadamore@1415 87 else
vromero@1482 88 result = result.replaceAll("#Type", "").
vromero@1482 89 replaceAll("#GenericDeclKind", "");
mcimadamore@1415 90
vromero@1482 91 return result.replaceAll("#LBody",
vromero@1482 92 lb.getLambdaBody(samTargetT, parameterT, returnT, lk, pk));
mcimadamore@1415 93 }
mcimadamore@1415 94 }
mcimadamore@1415 95
mcimadamore@1415 96 enum SamKind {
mcimadamore@1415 97 GENERIC("interface SAM<T> { #R m(#ARG); }"),
mcimadamore@1415 98 NON_GENERIC("interface SAM { #R m(#ARG); }");
mcimadamore@1415 99
mcimadamore@1415 100 String sam_str;
mcimadamore@1415 101
mcimadamore@1415 102 SamKind(String sam_str) {
mcimadamore@1415 103 this.sam_str = sam_str;
mcimadamore@1415 104 }
mcimadamore@1415 105
mcimadamore@1415 106 String getSam(TypeKind parameterT, TypeKind returnT) {
vromero@1482 107 return sam_str.replaceAll("#ARG",
vromero@1482 108 parameterT == TypeKind.VOID ?
vromero@1482 109 "" : parameterT.typeStr + " arg")
vromero@1482 110 .replaceAll("#R", returnT.typeStr);
mcimadamore@1415 111 }
mcimadamore@1415 112 }
mcimadamore@1415 113
mcimadamore@1415 114 enum TypeKind {
mcimadamore@1415 115 VOID("void", ""),
mcimadamore@1415 116 STRING("String", "\"hello\""),
mcimadamore@1415 117 INTEGER("Integer", "1"),
mcimadamore@1415 118 INT("int", "0"),
vromero@1482 119 COMPARATOR("java.util.Comparator<String>",
vromero@1482 120 "(java.util.Comparator<String>)(a, b) -> a.length()-b.length()"),
mcimadamore@1415 121 SAM("SAM2", "null"),
mcimadamore@1415 122 GENERIC("T", null);
mcimadamore@1415 123
mcimadamore@1415 124 String typeStr;
mcimadamore@1415 125 String valStr;
mcimadamore@1415 126
mcimadamore@1415 127 TypeKind(String typeStr, String valStr) {
mcimadamore@1415 128 this.typeStr = typeStr;
mcimadamore@1415 129 this.valStr = valStr;
mcimadamore@1415 130 }
mcimadamore@1415 131 }
mcimadamore@1415 132
mcimadamore@1415 133 enum LambdaKind {
mcimadamore@1415 134 EXPRESSION("#VAL"),
mcimadamore@1415 135 STATEMENT("{return #VAL;}");
mcimadamore@1415 136
mcimadamore@1415 137 String stmt;
mcimadamore@1415 138
mcimadamore@1415 139 LambdaKind(String stmt) {
mcimadamore@1415 140 this.stmt = stmt;
mcimadamore@1415 141 }
mcimadamore@1415 142 }
mcimadamore@1415 143
mcimadamore@1415 144 enum ParameterKind {
mcimadamore@1415 145 EXPLICIT("#TYPE"),
mcimadamore@1415 146 IMPLICIT("");
mcimadamore@1415 147
mcimadamore@1415 148 String paramTemplate;
mcimadamore@1415 149
mcimadamore@1415 150 ParameterKind(String paramTemplate) {
mcimadamore@1415 151 this.paramTemplate = paramTemplate;
mcimadamore@1415 152 }
mcimadamore@1415 153 }
mcimadamore@1415 154
mcimadamore@1415 155 enum Keyword {
mcimadamore@1415 156 SUPER("super"),
mcimadamore@1415 157 EXTENDS("extends"),
mcimadamore@1415 158 VOID("");
mcimadamore@1415 159
mcimadamore@1415 160 String keyStr;
mcimadamore@1415 161
mcimadamore@1415 162 Keyword(String keyStr) {
mcimadamore@1415 163 this.keyStr = keyStr;
mcimadamore@1415 164 }
mcimadamore@1415 165 }
mcimadamore@1415 166
mcimadamore@1415 167 enum LambdaBody {
vromero@1482 168 //no parameters, return type is one of the TypeKind
vromero@1482 169 RETURN_VOID("() -> #RET"),
vromero@1482 170 //has parameters, return type is one of the TypeKind
vromero@1482 171 RETURN_ARG("(#PK arg) -> #RET");
mcimadamore@1415 172
mcimadamore@1415 173 String bodyStr;
mcimadamore@1415 174
mcimadamore@1415 175 LambdaBody(String bodyStr) {
mcimadamore@1415 176 this.bodyStr = bodyStr;
mcimadamore@1415 177 }
mcimadamore@1415 178
vromero@1482 179 String getLambdaBody(TypeKind samTargetT, TypeKind parameterT,
vromero@1482 180 TypeKind returnT, LambdaKind lk, ParameterKind pk) {
mcimadamore@1415 181 String result = bodyStr.replaceAll("#PK", pk.paramTemplate);
mcimadamore@1415 182
mcimadamore@1415 183 if(result.contains("#TYPE")) {
mcimadamore@1415 184 if (parameterT == TypeKind.GENERIC && this != RETURN_VOID)
vromero@1482 185 result = result.replaceAll("#TYPE",
vromero@1482 186 samTargetT == null? "": samTargetT.typeStr);
mcimadamore@1415 187 else
mcimadamore@1415 188 result = result.replaceAll("#TYPE", parameterT.typeStr);
mcimadamore@1415 189 }
mcimadamore@1415 190 if (this == RETURN_ARG && parameterT == returnT)
mcimadamore@1415 191 return result.replaceAll("#RET", lk.stmt.replaceAll("#VAL", "arg"));
mcimadamore@1415 192 else {
mcimadamore@1415 193 if(returnT != TypeKind.GENERIC)
vromero@1482 194 return result.replaceAll("#RET", lk.stmt.replaceAll("#VAL",
vromero@1482 195 (returnT==TypeKind.VOID &&
vromero@1482 196 lk==LambdaKind.EXPRESSION) ? "{}" : returnT.valStr));
mcimadamore@1415 197 else
vromero@1482 198 return result.replaceAll("#RET",
vromero@1482 199 lk.stmt.replaceAll("#VAL", samTargetT.valStr));
mcimadamore@1415 200 }
mcimadamore@1415 201 }
mcimadamore@1415 202 }
mcimadamore@1415 203
mcimadamore@1415 204 enum GenericDeclKind {
mcimadamore@1415 205 NON_GENERIC(""),
mcimadamore@1415 206 GENERIC_NOBOUND("<T>"),
mcimadamore@1415 207 GENERIC_BOUND("<T extends #ExtendedType>");
mcimadamore@1415 208 String typeStr;
mcimadamore@1415 209
mcimadamore@1415 210 GenericDeclKind(String typeStr) {
mcimadamore@1415 211 this.typeStr = typeStr;
mcimadamore@1415 212 }
mcimadamore@1415 213
mcimadamore@1415 214 String getGenericDeclKind(TypeKind et) {
mcimadamore@1415 215 return typeStr.replaceAll("#ExtendedType", et==null? "":et.typeStr);
mcimadamore@1415 216 }
mcimadamore@1415 217 }
mcimadamore@1415 218
mcimadamore@1415 219 boolean checkTypeInference() {
mcimadamore@1415 220 if (parameterType == TypeKind.VOID) {
mcimadamore@1415 221 if (lambdaBodyType != LambdaBody.RETURN_VOID)
mcimadamore@1415 222 return false;
mcimadamore@1415 223 }
mcimadamore@1415 224 else if (lambdaBodyType != LambdaBody.RETURN_ARG)
mcimadamore@1415 225 return false;
vromero@1482 226 if ( genericDeclKind == GenericDeclKind.GENERIC_NOBOUND ||
vromero@1482 227 genericDeclKind == GenericDeclKind.GENERIC_BOUND ) {
vromero@1482 228 if ( parameterType == TypeKind.GENERIC &&
vromero@1482 229 parameterKind == ParameterKind.IMPLICIT) //cyclic inference
mcimadamore@1415 230 return false;
mcimadamore@1415 231 }
mcimadamore@1415 232 return true;
mcimadamore@1415 233 }
mcimadamore@1415 234
mcimadamore@1415 235 String templateStr = "#C\n" +
mcimadamore@1415 236 "interface SAM2 {\n" +
mcimadamore@1415 237 " SAM m();\n" +
mcimadamore@1415 238 "}\n";
mcimadamore@1415 239 SourceFile samSourceFile = new SourceFile("Sam.java", templateStr) {
mcimadamore@1415 240 public String toString() {
vromero@1482 241 return template.replaceAll("#C",
vromero@1482 242 samKind.getSam(parameterType, returnType));
mcimadamore@1415 243 }
mcimadamore@1415 244 };
mcimadamore@1415 245
mcimadamore@1415 246 SourceFile clientSourceFile = new SourceFile("Client.java",
mcimadamore@1415 247 "class Client { \n" +
mcimadamore@1415 248 " #Context\n" +
mcimadamore@1415 249 "}") {
mcimadamore@1415 250 public String toString() {
vromero@1482 251 return template.replaceAll("#Context",
vromero@1482 252 context.getContext(samKind, samTargetType, keyword,
vromero@1482 253 parameterType, returnType, lambdaKind, parameterKind,
vromero@1482 254 genericDeclKind, lambdaBodyType));
mcimadamore@1415 255 }
mcimadamore@1415 256 };
mcimadamore@1415 257
vromero@1482 258 public void run() {
vromero@1482 259 outWriter.println("kk:");
mcimadamore@1415 260 StringBuilder sb = new StringBuilder("SamKind:");
vromero@1482 261 sb.append(samKind).append(" SamTargetType:")
vromero@1482 262 .append(samTargetType).append(" ParameterType:").append(parameterType)
vromero@1482 263 .append(" ReturnType:").append(returnType).append(" Context:")
vromero@1482 264 .append(context).append(" LambdaKind:").append(lambdaKind)
vromero@1482 265 .append(" LambdaBodyType:").append(lambdaBodyType)
vromero@1482 266 .append(" ParameterKind:").append(parameterKind).append(" Keyword:")
vromero@1482 267 .append(keyword);
vromero@1482 268 outWriter.println(sb);
mcimadamore@1415 269 DiagnosticChecker dc = new DiagnosticChecker();
vromero@1482 270 JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), dc,
vromero@1482 271 null, null, Arrays.asList(samSourceFile, clientSourceFile));
vromero@1482 272 try {
vromero@1482 273 ct.analyze();
vromero@1482 274 } catch (Throwable t) {
vromero@1482 275 processException(t);
vromero@1482 276 }
mcimadamore@1415 277 if (dc.errorFound == checkTypeInference()) {
vromero@1482 278 throw new AssertionError(samSourceFile + "\n\n" +
vromero@1482 279 clientSourceFile + "\n" + parameterType + " " + returnType);
mcimadamore@1415 280 }
mcimadamore@1415 281 }
mcimadamore@1415 282
mcimadamore@1415 283 abstract class SourceFile extends SimpleJavaFileObject {
mcimadamore@1415 284
mcimadamore@1415 285 protected String template;
mcimadamore@1415 286
mcimadamore@1415 287 public SourceFile(String filename, String template) {
mcimadamore@1415 288 super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
mcimadamore@1415 289 this.template = template;
mcimadamore@1415 290 }
mcimadamore@1415 291
mcimadamore@1415 292 @Override
mcimadamore@1415 293 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1415 294 return toString();
mcimadamore@1415 295 }
mcimadamore@1415 296
mcimadamore@1415 297 public abstract String toString();
mcimadamore@1415 298 }
mcimadamore@1415 299
vromero@1482 300 static class DiagnosticChecker
vromero@1482 301 implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@1415 302
mcimadamore@1415 303 boolean errorFound = false;
mcimadamore@1415 304
mcimadamore@1415 305 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@1415 306 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
mcimadamore@1415 307 errorFound = true;
mcimadamore@1415 308 }
mcimadamore@1415 309 }
mcimadamore@1415 310 }
mcimadamore@1415 311
mcimadamore@1415 312 SamKind samKind;
mcimadamore@1415 313 TypeKind samTargetType;
mcimadamore@1415 314 TypeKind parameterType;
mcimadamore@1415 315 TypeKind returnType;
mcimadamore@1415 316 Context context;
mcimadamore@1415 317 LambdaBody lambdaBodyType;
mcimadamore@1415 318 LambdaKind lambdaKind;
mcimadamore@1415 319 ParameterKind parameterKind;
mcimadamore@1415 320 Keyword keyword;
mcimadamore@1415 321 GenericDeclKind genericDeclKind;
mcimadamore@1415 322
vromero@1482 323 TypeInferenceComboTest(SamKind sk, TypeKind samTargetT, TypeKind parameterT,
vromero@1482 324 TypeKind returnT, LambdaBody lb, Context c, LambdaKind lk,
vromero@1482 325 ParameterKind pk, Keyword kw, GenericDeclKind gdk) {
mcimadamore@1415 326 samKind = sk;
mcimadamore@1415 327 samTargetType = samTargetT;
mcimadamore@1415 328 parameterType = parameterT;
mcimadamore@1415 329 returnType = returnT;
mcimadamore@1415 330 context = c;
mcimadamore@1415 331 lambdaKind = lk;
mcimadamore@1415 332 parameterKind = pk;
mcimadamore@1415 333 keyword = kw;
mcimadamore@1415 334 lambdaBodyType = lb;
mcimadamore@1415 335 genericDeclKind = gdk;
mcimadamore@1415 336 }
mcimadamore@1415 337
mcimadamore@1415 338 public static void main(String[] args) throws Exception {
mcimadamore@1415 339 for(Context ct : Context.values()) {
mcimadamore@1415 340 for (TypeKind returnT : TypeKind.values()) {
mcimadamore@1415 341 for (TypeKind parameterT : TypeKind.values()) {
mcimadamore@1415 342 for(LambdaBody lb : LambdaBody.values()) {
mcimadamore@1415 343 for (ParameterKind parameterK : ParameterKind.values()) {
mcimadamore@1415 344 for(LambdaKind lambdaK : LambdaKind.values()) {
mcimadamore@1415 345 for (SamKind sk : SamKind.values()) {
mcimadamore@1415 346 if (sk == SamKind.NON_GENERIC) {
vromero@1482 347 generateNonGenericSAM(ct, returnT,
vromero@1482 348 parameterT, lb, parameterK,
vromero@1482 349 lambdaK, sk);
mcimadamore@1415 350 }
mcimadamore@1415 351 else if (sk == SamKind.GENERIC) {
vromero@1482 352 generateGenericSAM(ct, returnT,
vromero@1482 353 parameterT, lb, parameterK,
vromero@1482 354 lambdaK, sk);
mcimadamore@1415 355 }
mcimadamore@1415 356 }
mcimadamore@1415 357 }
mcimadamore@1415 358 }
mcimadamore@1415 359 }
mcimadamore@1415 360 }
mcimadamore@1415 361 }
mcimadamore@1415 362 }
vromero@1482 363
vromero@1482 364 checkAfterExec(false);
mcimadamore@1415 365 }
vromero@1482 366
vromero@1482 367 static void generateNonGenericSAM(Context ct, TypeKind returnT,
vromero@1482 368 TypeKind parameterT, LambdaBody lb, ParameterKind parameterK,
vromero@1482 369 LambdaKind lambdaK, SamKind sk) {
vromero@1482 370 if(parameterT != TypeKind.GENERIC && returnT != TypeKind.GENERIC ) {
vromero@1482 371 pool.execute(new TypeInferenceComboTest(sk, null, parameterT,
vromero@1482 372 returnT, lb, ct, lambdaK, parameterK, null, null));
vromero@1482 373 }
vromero@1482 374 }
vromero@1482 375
vromero@1482 376 static void generateGenericSAM(Context ct, TypeKind returnT,
vromero@1482 377 TypeKind parameterT, LambdaBody lb, ParameterKind parameterK,
vromero@1482 378 LambdaKind lambdaK, SamKind sk) {
vromero@1482 379 for (Keyword kw : Keyword.values()) {
vromero@1482 380 for (TypeKind samTargetT : TypeKind.values()) {
vromero@1482 381 if(samTargetT != TypeKind.VOID &&
vromero@1482 382 samTargetT != TypeKind.INT &&
vromero@1482 383 samTargetT != TypeKind.GENERIC &&
vromero@1482 384 (parameterT == TypeKind.GENERIC ||
vromero@1482 385 returnT == TypeKind.GENERIC)) {
vromero@1482 386 if(ct != Context.METHOD_CALL) {
vromero@1482 387 pool.execute(
vromero@1482 388 new TypeInferenceComboTest(sk, samTargetT, parameterT,
vromero@1482 389 returnT, lb, ct, lambdaK, parameterK, kw, null));
vromero@1482 390 } else {//Context.METHOD_CALL
vromero@1482 391 for (GenericDeclKind gdk :
vromero@1482 392 GenericDeclKind.values())
vromero@1482 393 pool.execute(
vromero@1482 394 new TypeInferenceComboTest(sk, samTargetT,
vromero@1482 395 parameterT, returnT, lb, ct, lambdaK,
vromero@1482 396 parameterK, kw, gdk));
vromero@1482 397 }
vromero@1482 398 }
vromero@1482 399 }
vromero@1482 400 }
vromero@1482 401 }
vromero@1482 402
mcimadamore@1415 403 }

mercurial