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

Sat, 17 Nov 2012 19:01:03 +0000

author
mcimadamore
date
Sat, 17 Nov 2012 19:01:03 +0000
changeset 1415
01c9d4161882
child 1482
954541f13717
permissions
-rw-r--r--

8003280: Add lambda tests
Summary: Turn on lambda expression, method reference and default method support
Reviewed-by: jjg

mcimadamore@1415 1 /*
mcimadamore@1415 2 * Copyright (c) 2011, 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
mcimadamore@1415 28 * perform automated checks in type inference in lambda expressions in different contexts
mcimadamore@1415 29 * @compile TypeInferenceComboTest.java
mcimadamore@1415 30 * @run main/timeout=360 TypeInferenceComboTest
mcimadamore@1415 31 */
mcimadamore@1415 32
mcimadamore@1415 33 import com.sun.source.util.JavacTask;
mcimadamore@1415 34 import java.net.URI;
mcimadamore@1415 35 import java.util.Arrays;
mcimadamore@1415 36 import javax.tools.Diagnostic;
mcimadamore@1415 37 import javax.tools.JavaCompiler;
mcimadamore@1415 38 import javax.tools.JavaFileObject;
mcimadamore@1415 39 import javax.tools.SimpleJavaFileObject;
mcimadamore@1415 40 import javax.tools.ToolProvider;
mcimadamore@1415 41 import javax.tools.StandardJavaFileManager;
mcimadamore@1415 42
mcimadamore@1415 43 public class TypeInferenceComboTest {
mcimadamore@1415 44 enum Context {
mcimadamore@1415 45 ASSIGNMENT("SAM#Type s = #LBody;"),
mcimadamore@1415 46 METHOD_CALL("#GenericDeclKind void method1(SAM#Type s) { }\n" +
mcimadamore@1415 47 "void method2() {\n" +
mcimadamore@1415 48 " method1(#LBody);\n" +
mcimadamore@1415 49 "}"),
mcimadamore@1415 50 RETURN_OF_METHOD("SAM#Type method1() {\n" +
mcimadamore@1415 51 " return #LBody;\n" +
mcimadamore@1415 52 "}"),
mcimadamore@1415 53 LAMBDA_RETURN_EXPRESSION("SAM2 s2 = () -> {return (SAM#Type)#LBody;};\n"),
mcimadamore@1415 54 ARRAY_INITIALIZER("Object[] oarray = {\"a\", 1, (SAM#Type)#LBody};");
mcimadamore@1415 55
mcimadamore@1415 56 String context;
mcimadamore@1415 57
mcimadamore@1415 58 Context(String context) {
mcimadamore@1415 59 this.context = context;
mcimadamore@1415 60 }
mcimadamore@1415 61
mcimadamore@1415 62 String getContext(SamKind sk, TypeKind samTargetT, Keyword kw, TypeKind parameterT, TypeKind returnT, LambdaKind lk, ParameterKind pk, GenericDeclKind gdk, LambdaBody lb) {
mcimadamore@1415 63 String result = context;
mcimadamore@1415 64 if (sk == SamKind.GENERIC) {
mcimadamore@1415 65 if(this == Context.METHOD_CALL) {
mcimadamore@1415 66 result = result.replaceAll("#GenericDeclKind", gdk.getGenericDeclKind(samTargetT));
mcimadamore@1415 67 if(gdk == GenericDeclKind.NON_GENERIC)
mcimadamore@1415 68 result = result.replaceAll("#Type", "<" + samTargetT.typeStr + ">");
mcimadamore@1415 69 else //#GenericDeclKind is <T> or <T extends xxx>
mcimadamore@1415 70 result = result.replaceAll("#Type", "<T>");
mcimadamore@1415 71 }
mcimadamore@1415 72 else {
mcimadamore@1415 73 if(kw == Keyword.VOID)
mcimadamore@1415 74 result = result.replaceAll("#Type", "<" + samTargetT.typeStr + ">");
mcimadamore@1415 75 else
mcimadamore@1415 76 result = result.replaceAll("#Type", "<? " + kw.keyStr + " " + samTargetT.typeStr + ">");
mcimadamore@1415 77 }
mcimadamore@1415 78 }
mcimadamore@1415 79 else
mcimadamore@1415 80 result = result.replaceAll("#Type", "").replaceAll("#GenericDeclKind", "");
mcimadamore@1415 81
mcimadamore@1415 82 return result.replaceAll("#LBody", lb.getLambdaBody(samTargetT, parameterT, returnT, lk, pk));
mcimadamore@1415 83 }
mcimadamore@1415 84 }
mcimadamore@1415 85
mcimadamore@1415 86 enum SamKind {
mcimadamore@1415 87 GENERIC("interface SAM<T> { #R m(#ARG); }"),
mcimadamore@1415 88 NON_GENERIC("interface SAM { #R m(#ARG); }");
mcimadamore@1415 89
mcimadamore@1415 90 String sam_str;
mcimadamore@1415 91
mcimadamore@1415 92 SamKind(String sam_str) {
mcimadamore@1415 93 this.sam_str = sam_str;
mcimadamore@1415 94 }
mcimadamore@1415 95
mcimadamore@1415 96 String getSam(TypeKind parameterT, TypeKind returnT) {
mcimadamore@1415 97 return sam_str.replaceAll("#ARG", parameterT == TypeKind.VOID ? "" : parameterT.typeStr + " arg")
mcimadamore@1415 98 .replaceAll("#R", returnT.typeStr);
mcimadamore@1415 99 }
mcimadamore@1415 100 }
mcimadamore@1415 101
mcimadamore@1415 102 enum TypeKind {
mcimadamore@1415 103 VOID("void", ""),
mcimadamore@1415 104 STRING("String", "\"hello\""),
mcimadamore@1415 105 INTEGER("Integer", "1"),
mcimadamore@1415 106 INT("int", "0"),
mcimadamore@1415 107 COMPARATOR("java.util.Comparator<String>", "(java.util.Comparator<String>)(a, b) -> a.length()-b.length()"),
mcimadamore@1415 108 SAM("SAM2", "null"),
mcimadamore@1415 109 GENERIC("T", null);
mcimadamore@1415 110
mcimadamore@1415 111 String typeStr;
mcimadamore@1415 112 String valStr;
mcimadamore@1415 113
mcimadamore@1415 114 TypeKind(String typeStr, String valStr) {
mcimadamore@1415 115 this.typeStr = typeStr;
mcimadamore@1415 116 this.valStr = valStr;
mcimadamore@1415 117 }
mcimadamore@1415 118 }
mcimadamore@1415 119
mcimadamore@1415 120 enum LambdaKind {
mcimadamore@1415 121 EXPRESSION("#VAL"),
mcimadamore@1415 122 STATEMENT("{return #VAL;}");
mcimadamore@1415 123
mcimadamore@1415 124 String stmt;
mcimadamore@1415 125
mcimadamore@1415 126 LambdaKind(String stmt) {
mcimadamore@1415 127 this.stmt = stmt;
mcimadamore@1415 128 }
mcimadamore@1415 129 }
mcimadamore@1415 130
mcimadamore@1415 131 enum ParameterKind {
mcimadamore@1415 132 EXPLICIT("#TYPE"),
mcimadamore@1415 133 IMPLICIT("");
mcimadamore@1415 134
mcimadamore@1415 135 String paramTemplate;
mcimadamore@1415 136
mcimadamore@1415 137 ParameterKind(String paramTemplate) {
mcimadamore@1415 138 this.paramTemplate = paramTemplate;
mcimadamore@1415 139 }
mcimadamore@1415 140 }
mcimadamore@1415 141
mcimadamore@1415 142 enum Keyword {
mcimadamore@1415 143 SUPER("super"),
mcimadamore@1415 144 EXTENDS("extends"),
mcimadamore@1415 145 VOID("");
mcimadamore@1415 146
mcimadamore@1415 147 String keyStr;
mcimadamore@1415 148
mcimadamore@1415 149 Keyword(String keyStr) {
mcimadamore@1415 150 this.keyStr = keyStr;
mcimadamore@1415 151 }
mcimadamore@1415 152 }
mcimadamore@1415 153
mcimadamore@1415 154 enum LambdaBody {
mcimadamore@1415 155 RETURN_VOID("() -> #RET"),//no parameters, return type is one of the TypeKind
mcimadamore@1415 156 RETURN_ARG("(#PK arg) -> #RET");//has parameters, return type is one of the TypeKind
mcimadamore@1415 157
mcimadamore@1415 158 String bodyStr;
mcimadamore@1415 159
mcimadamore@1415 160 LambdaBody(String bodyStr) {
mcimadamore@1415 161 this.bodyStr = bodyStr;
mcimadamore@1415 162 }
mcimadamore@1415 163
mcimadamore@1415 164 String getLambdaBody(TypeKind samTargetT, TypeKind parameterT, TypeKind returnT, LambdaKind lk, ParameterKind pk) {
mcimadamore@1415 165 String result = bodyStr.replaceAll("#PK", pk.paramTemplate);
mcimadamore@1415 166
mcimadamore@1415 167 if(result.contains("#TYPE")) {
mcimadamore@1415 168 if (parameterT == TypeKind.GENERIC && this != RETURN_VOID)
mcimadamore@1415 169 result = result.replaceAll("#TYPE", samTargetT == null? "": samTargetT.typeStr);
mcimadamore@1415 170 else
mcimadamore@1415 171 result = result.replaceAll("#TYPE", parameterT.typeStr);
mcimadamore@1415 172 }
mcimadamore@1415 173 if (this == RETURN_ARG && parameterT == returnT)
mcimadamore@1415 174 return result.replaceAll("#RET", lk.stmt.replaceAll("#VAL", "arg"));
mcimadamore@1415 175 else {
mcimadamore@1415 176 if(returnT != TypeKind.GENERIC)
mcimadamore@1415 177 return result.replaceAll("#RET", lk.stmt.replaceAll("#VAL", (returnT==TypeKind.VOID && lk==LambdaKind.EXPRESSION)? "{}" : returnT.valStr));
mcimadamore@1415 178 else
mcimadamore@1415 179 return result.replaceAll("#RET", lk.stmt.replaceAll("#VAL", samTargetT.valStr));
mcimadamore@1415 180 }
mcimadamore@1415 181 }
mcimadamore@1415 182 }
mcimadamore@1415 183
mcimadamore@1415 184 enum GenericDeclKind {
mcimadamore@1415 185 NON_GENERIC(""),
mcimadamore@1415 186 GENERIC_NOBOUND("<T>"),
mcimadamore@1415 187 GENERIC_BOUND("<T extends #ExtendedType>");
mcimadamore@1415 188 String typeStr;
mcimadamore@1415 189
mcimadamore@1415 190 GenericDeclKind(String typeStr) {
mcimadamore@1415 191 this.typeStr = typeStr;
mcimadamore@1415 192 }
mcimadamore@1415 193
mcimadamore@1415 194 String getGenericDeclKind(TypeKind et) {
mcimadamore@1415 195 return typeStr.replaceAll("#ExtendedType", et==null? "":et.typeStr);
mcimadamore@1415 196 }
mcimadamore@1415 197 }
mcimadamore@1415 198
mcimadamore@1415 199 boolean checkTypeInference() {
mcimadamore@1415 200 if (parameterType == TypeKind.VOID) {
mcimadamore@1415 201 if (lambdaBodyType != LambdaBody.RETURN_VOID)
mcimadamore@1415 202 return false;
mcimadamore@1415 203 }
mcimadamore@1415 204 else if (lambdaBodyType != LambdaBody.RETURN_ARG)
mcimadamore@1415 205 return false;
mcimadamore@1415 206 if ( genericDeclKind == GenericDeclKind.GENERIC_NOBOUND || genericDeclKind == GenericDeclKind.GENERIC_BOUND ) {
mcimadamore@1415 207 if ( parameterType == TypeKind.GENERIC && parameterKind == ParameterKind.IMPLICIT) //cyclic inference
mcimadamore@1415 208 return false;
mcimadamore@1415 209 }
mcimadamore@1415 210 return true;
mcimadamore@1415 211 }
mcimadamore@1415 212
mcimadamore@1415 213 String templateStr = "#C\n" +
mcimadamore@1415 214 "interface SAM2 {\n" +
mcimadamore@1415 215 " SAM m();\n" +
mcimadamore@1415 216 "}\n";
mcimadamore@1415 217 SourceFile samSourceFile = new SourceFile("Sam.java", templateStr) {
mcimadamore@1415 218 public String toString() {
mcimadamore@1415 219 return template.replaceAll("#C", samKind.getSam(parameterType, returnType));
mcimadamore@1415 220 }
mcimadamore@1415 221 };
mcimadamore@1415 222
mcimadamore@1415 223 SourceFile clientSourceFile = new SourceFile("Client.java",
mcimadamore@1415 224 "class Client { \n" +
mcimadamore@1415 225 " #Context\n" +
mcimadamore@1415 226 "}") {
mcimadamore@1415 227 public String toString() {
mcimadamore@1415 228 return template.replaceAll("#Context", context.getContext(samKind, samTargetType, keyword, parameterType, returnType, lambdaKind, parameterKind, genericDeclKind, lambdaBodyType));
mcimadamore@1415 229 }
mcimadamore@1415 230 };
mcimadamore@1415 231
mcimadamore@1415 232 void test() throws Exception {
mcimadamore@1415 233 System.out.println("kk:");
mcimadamore@1415 234 StringBuilder sb = new StringBuilder("SamKind:");
mcimadamore@1415 235 sb.append(samKind).append(" SamTargetType:").append(samTargetType).append(" ParameterType:").append(parameterType)
mcimadamore@1415 236 .append(" ReturnType:").append(returnType).append(" Context:").append(context).append(" LambdaKind:").append(lambdaKind)
mcimadamore@1415 237 .append(" LambdaBodyType:").append(lambdaBodyType).append(" ParameterKind:").append(parameterKind).append(" Keyword:").append(keyword);
mcimadamore@1415 238 System.out.println(sb);
mcimadamore@1415 239 DiagnosticChecker dc = new DiagnosticChecker();
mcimadamore@1415 240 JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
mcimadamore@1415 241 ct.analyze();
mcimadamore@1415 242 if (dc.errorFound == checkTypeInference()) {
mcimadamore@1415 243 throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile + "\n" + parameterType + " " + returnType);
mcimadamore@1415 244 }
mcimadamore@1415 245 }
mcimadamore@1415 246
mcimadamore@1415 247 abstract class SourceFile extends SimpleJavaFileObject {
mcimadamore@1415 248
mcimadamore@1415 249 protected String template;
mcimadamore@1415 250
mcimadamore@1415 251 public SourceFile(String filename, String template) {
mcimadamore@1415 252 super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
mcimadamore@1415 253 this.template = template;
mcimadamore@1415 254 }
mcimadamore@1415 255
mcimadamore@1415 256 @Override
mcimadamore@1415 257 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1415 258 return toString();
mcimadamore@1415 259 }
mcimadamore@1415 260
mcimadamore@1415 261 public abstract String toString();
mcimadamore@1415 262 }
mcimadamore@1415 263
mcimadamore@1415 264 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@1415 265
mcimadamore@1415 266 boolean errorFound = false;
mcimadamore@1415 267
mcimadamore@1415 268 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@1415 269 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
mcimadamore@1415 270 errorFound = true;
mcimadamore@1415 271 }
mcimadamore@1415 272 }
mcimadamore@1415 273 }
mcimadamore@1415 274
mcimadamore@1415 275 SamKind samKind;
mcimadamore@1415 276 TypeKind samTargetType;
mcimadamore@1415 277 TypeKind parameterType;
mcimadamore@1415 278 TypeKind returnType;
mcimadamore@1415 279 Context context;
mcimadamore@1415 280 LambdaBody lambdaBodyType;
mcimadamore@1415 281 LambdaKind lambdaKind;
mcimadamore@1415 282 ParameterKind parameterKind;
mcimadamore@1415 283 Keyword keyword;
mcimadamore@1415 284 GenericDeclKind genericDeclKind;
mcimadamore@1415 285
mcimadamore@1415 286 static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
mcimadamore@1415 287 static StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
mcimadamore@1415 288
mcimadamore@1415 289 TypeInferenceComboTest(SamKind sk, TypeKind samTargetT, TypeKind parameterT, TypeKind returnT, LambdaBody lb, Context c, LambdaKind lk, ParameterKind pk, Keyword kw, GenericDeclKind gdk) {
mcimadamore@1415 290 samKind = sk;
mcimadamore@1415 291 samTargetType = samTargetT;
mcimadamore@1415 292 parameterType = parameterT;
mcimadamore@1415 293 returnType = returnT;
mcimadamore@1415 294 context = c;
mcimadamore@1415 295 lambdaKind = lk;
mcimadamore@1415 296 parameterKind = pk;
mcimadamore@1415 297 keyword = kw;
mcimadamore@1415 298 lambdaBodyType = lb;
mcimadamore@1415 299 genericDeclKind = gdk;
mcimadamore@1415 300 }
mcimadamore@1415 301
mcimadamore@1415 302 public static void main(String[] args) throws Exception {
mcimadamore@1415 303 for(Context ct : Context.values()) {
mcimadamore@1415 304 for (TypeKind returnT : TypeKind.values()) {
mcimadamore@1415 305 for (TypeKind parameterT : TypeKind.values()) {
mcimadamore@1415 306 for(LambdaBody lb : LambdaBody.values()) {
mcimadamore@1415 307 for (ParameterKind parameterK : ParameterKind.values()) {
mcimadamore@1415 308 for(LambdaKind lambdaK : LambdaKind.values()) {
mcimadamore@1415 309 for (SamKind sk : SamKind.values()) {
mcimadamore@1415 310 if (sk == SamKind.NON_GENERIC) {
mcimadamore@1415 311 if(parameterT != TypeKind.GENERIC && returnT != TypeKind.GENERIC )
mcimadamore@1415 312 new TypeInferenceComboTest(sk, null, parameterT, returnT, lb, ct, lambdaK, parameterK, null, null).test();
mcimadamore@1415 313 }
mcimadamore@1415 314 else if (sk == SamKind.GENERIC) {
mcimadamore@1415 315 for (Keyword kw : Keyword.values()) {
mcimadamore@1415 316 for (TypeKind samTargetT : TypeKind.values()) {
mcimadamore@1415 317 if(samTargetT != TypeKind.VOID && samTargetT != TypeKind.INT && samTargetT != TypeKind.GENERIC
mcimadamore@1415 318 && (parameterT == TypeKind.GENERIC || returnT == TypeKind.GENERIC)) {
mcimadamore@1415 319 if(ct != Context.METHOD_CALL) {
mcimadamore@1415 320 new TypeInferenceComboTest(sk, samTargetT, parameterT, returnT, lb, ct, lambdaK, parameterK, kw, null).test();
mcimadamore@1415 321 }
mcimadamore@1415 322 else {//Context.METHOD_CALL
mcimadamore@1415 323 for (GenericDeclKind gdk : GenericDeclKind.values())
mcimadamore@1415 324 new TypeInferenceComboTest(sk, samTargetT, parameterT, returnT, lb, ct, lambdaK, parameterK, kw, gdk).test();
mcimadamore@1415 325 }
mcimadamore@1415 326 }
mcimadamore@1415 327 }
mcimadamore@1415 328 }
mcimadamore@1415 329 }
mcimadamore@1415 330 }
mcimadamore@1415 331 }
mcimadamore@1415 332 }
mcimadamore@1415 333 }
mcimadamore@1415 334 }
mcimadamore@1415 335 }
mcimadamore@1415 336 }
mcimadamore@1415 337 }
mcimadamore@1415 338 }

mercurial