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

changeset 1415
01c9d4161882
child 1482
954541f13717
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/typeInference/combo/TypeInferenceComboTest.java	Sat Nov 17 19:01:03 2012 +0000
     1.3 @@ -0,0 +1,338 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 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
    1.30 + * @summary Add lambda tests
    1.31 + *  perform automated checks in type inference in lambda expressions in different contexts
    1.32 + * @compile  TypeInferenceComboTest.java
    1.33 + * @run main/timeout=360 TypeInferenceComboTest
    1.34 + */
    1.35 +
    1.36 +import com.sun.source.util.JavacTask;
    1.37 +import java.net.URI;
    1.38 +import java.util.Arrays;
    1.39 +import javax.tools.Diagnostic;
    1.40 +import javax.tools.JavaCompiler;
    1.41 +import javax.tools.JavaFileObject;
    1.42 +import javax.tools.SimpleJavaFileObject;
    1.43 +import javax.tools.ToolProvider;
    1.44 +import javax.tools.StandardJavaFileManager;
    1.45 +
    1.46 +public class TypeInferenceComboTest {
    1.47 +    enum Context {
    1.48 +        ASSIGNMENT("SAM#Type s = #LBody;"),
    1.49 +        METHOD_CALL("#GenericDeclKind void method1(SAM#Type s) { }\n" +
    1.50 +                    "void method2() {\n" +
    1.51 +                    "    method1(#LBody);\n" +
    1.52 +                    "}"),
    1.53 +        RETURN_OF_METHOD("SAM#Type method1() {\n" +
    1.54 +                "    return #LBody;\n" +
    1.55 +                "}"),
    1.56 +        LAMBDA_RETURN_EXPRESSION("SAM2 s2 = () -> {return (SAM#Type)#LBody;};\n"),
    1.57 +        ARRAY_INITIALIZER("Object[] oarray = {\"a\", 1, (SAM#Type)#LBody};");
    1.58 +
    1.59 +        String context;
    1.60 +
    1.61 +        Context(String context) {
    1.62 +            this.context = context;
    1.63 +        }
    1.64 +
    1.65 +        String getContext(SamKind sk, TypeKind samTargetT, Keyword kw, TypeKind parameterT, TypeKind returnT, LambdaKind lk, ParameterKind pk, GenericDeclKind gdk, LambdaBody lb) {
    1.66 +            String result = context;
    1.67 +            if (sk == SamKind.GENERIC) {
    1.68 +                if(this == Context.METHOD_CALL) {
    1.69 +                    result = result.replaceAll("#GenericDeclKind", gdk.getGenericDeclKind(samTargetT));
    1.70 +                    if(gdk == GenericDeclKind.NON_GENERIC)
    1.71 +                        result = result.replaceAll("#Type", "<" + samTargetT.typeStr + ">");
    1.72 +                    else //#GenericDeclKind is <T> or <T extends xxx>
    1.73 +                        result = result.replaceAll("#Type", "<T>");
    1.74 +                }
    1.75 +                else {
    1.76 +                    if(kw == Keyword.VOID)
    1.77 +                        result = result.replaceAll("#Type", "<" + samTargetT.typeStr + ">");
    1.78 +                    else
    1.79 +                        result = result.replaceAll("#Type", "<? " + kw.keyStr + " " + samTargetT.typeStr + ">");
    1.80 +                }
    1.81 +            }
    1.82 +            else
    1.83 +                result = result.replaceAll("#Type", "").replaceAll("#GenericDeclKind", "");
    1.84 +
    1.85 +            return result.replaceAll("#LBody", lb.getLambdaBody(samTargetT, parameterT, returnT, lk, pk));
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    enum SamKind {
    1.90 +        GENERIC("interface SAM<T> { #R m(#ARG); }"),
    1.91 +        NON_GENERIC("interface SAM { #R m(#ARG); }");
    1.92 +
    1.93 +        String sam_str;
    1.94 +
    1.95 +        SamKind(String sam_str) {
    1.96 +            this.sam_str = sam_str;
    1.97 +        }
    1.98 +
    1.99 +        String getSam(TypeKind parameterT, TypeKind returnT) {
   1.100 +            return sam_str.replaceAll("#ARG", parameterT == TypeKind.VOID ? "" : parameterT.typeStr + " arg")
   1.101 +                          .replaceAll("#R", returnT.typeStr);
   1.102 +        }
   1.103 +    }
   1.104 +
   1.105 +    enum TypeKind {
   1.106 +        VOID("void", ""),
   1.107 +        STRING("String", "\"hello\""),
   1.108 +        INTEGER("Integer", "1"),
   1.109 +        INT("int", "0"),
   1.110 +        COMPARATOR("java.util.Comparator<String>", "(java.util.Comparator<String>)(a, b) -> a.length()-b.length()"),
   1.111 +        SAM("SAM2", "null"),
   1.112 +        GENERIC("T", null);
   1.113 +
   1.114 +        String typeStr;
   1.115 +        String valStr;
   1.116 +
   1.117 +        TypeKind(String typeStr, String valStr) {
   1.118 +            this.typeStr = typeStr;
   1.119 +            this.valStr = valStr;
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    enum LambdaKind {
   1.124 +        EXPRESSION("#VAL"),
   1.125 +        STATEMENT("{return #VAL;}");
   1.126 +
   1.127 +        String stmt;
   1.128 +
   1.129 +        LambdaKind(String stmt) {
   1.130 +            this.stmt = stmt;
   1.131 +        }
   1.132 +    }
   1.133 +
   1.134 +    enum ParameterKind {
   1.135 +        EXPLICIT("#TYPE"),
   1.136 +        IMPLICIT("");
   1.137 +
   1.138 +        String paramTemplate;
   1.139 +
   1.140 +        ParameterKind(String paramTemplate) {
   1.141 +             this.paramTemplate = paramTemplate;
   1.142 +        }
   1.143 +    }
   1.144 +
   1.145 +    enum Keyword {
   1.146 +        SUPER("super"),
   1.147 +        EXTENDS("extends"),
   1.148 +        VOID("");
   1.149 +
   1.150 +        String keyStr;
   1.151 +
   1.152 +        Keyword(String keyStr) {
   1.153 +            this.keyStr = keyStr;
   1.154 +        }
   1.155 +    }
   1.156 +
   1.157 +    enum LambdaBody {
   1.158 +        RETURN_VOID("() -> #RET"),//no parameters, return type is one of the TypeKind
   1.159 +        RETURN_ARG("(#PK arg) -> #RET");//has parameters, return type is one of the TypeKind
   1.160 +
   1.161 +        String bodyStr;
   1.162 +
   1.163 +        LambdaBody(String bodyStr) {
   1.164 +            this.bodyStr = bodyStr;
   1.165 +        }
   1.166 +
   1.167 +        String getLambdaBody(TypeKind samTargetT, TypeKind parameterT, TypeKind returnT, LambdaKind lk, ParameterKind pk) {
   1.168 +            String result = bodyStr.replaceAll("#PK", pk.paramTemplate);
   1.169 +
   1.170 +            if(result.contains("#TYPE")) {
   1.171 +                if (parameterT == TypeKind.GENERIC && this != RETURN_VOID)
   1.172 +                    result = result.replaceAll("#TYPE", samTargetT == null? "": samTargetT.typeStr);
   1.173 +                else
   1.174 +                    result = result.replaceAll("#TYPE", parameterT.typeStr);
   1.175 +            }
   1.176 +            if (this == RETURN_ARG && parameterT == returnT)
   1.177 +                return result.replaceAll("#RET", lk.stmt.replaceAll("#VAL", "arg"));
   1.178 +            else {
   1.179 +                if(returnT != TypeKind.GENERIC)
   1.180 +                    return result.replaceAll("#RET", lk.stmt.replaceAll("#VAL", (returnT==TypeKind.VOID && lk==LambdaKind.EXPRESSION)? "{}" : returnT.valStr));
   1.181 +                else
   1.182 +                    return result.replaceAll("#RET", lk.stmt.replaceAll("#VAL", samTargetT.valStr));
   1.183 +            }
   1.184 +        }
   1.185 +    }
   1.186 +
   1.187 +    enum GenericDeclKind {
   1.188 +        NON_GENERIC(""),
   1.189 +        GENERIC_NOBOUND("<T>"),
   1.190 +        GENERIC_BOUND("<T extends #ExtendedType>");
   1.191 +        String typeStr;
   1.192 +
   1.193 +        GenericDeclKind(String typeStr) {
   1.194 +            this.typeStr = typeStr;
   1.195 +        }
   1.196 +
   1.197 +        String getGenericDeclKind(TypeKind et) {
   1.198 +            return typeStr.replaceAll("#ExtendedType", et==null? "":et.typeStr);
   1.199 +        }
   1.200 +    }
   1.201 +
   1.202 +    boolean checkTypeInference() {
   1.203 +        if (parameterType == TypeKind.VOID) {
   1.204 +            if (lambdaBodyType != LambdaBody.RETURN_VOID)
   1.205 +                return false;
   1.206 +        }
   1.207 +        else if (lambdaBodyType != LambdaBody.RETURN_ARG)
   1.208 +            return false;
   1.209 +        if (  genericDeclKind == GenericDeclKind.GENERIC_NOBOUND || genericDeclKind == GenericDeclKind.GENERIC_BOUND ) {
   1.210 +            if ( parameterType == TypeKind.GENERIC && parameterKind == ParameterKind.IMPLICIT) //cyclic inference
   1.211 +                return false;
   1.212 +        }
   1.213 +        return true;
   1.214 +    }
   1.215 +
   1.216 +    String templateStr = "#C\n" +
   1.217 +                         "interface SAM2 {\n" +
   1.218 +                         "    SAM m();\n" +
   1.219 +                         "}\n";
   1.220 +    SourceFile samSourceFile = new SourceFile("Sam.java", templateStr) {
   1.221 +        public String toString() {
   1.222 +            return template.replaceAll("#C", samKind.getSam(parameterType, returnType));
   1.223 +        }
   1.224 +    };
   1.225 +
   1.226 +    SourceFile clientSourceFile = new SourceFile("Client.java",
   1.227 +                                                 "class Client { \n" +
   1.228 +                                                 "    #Context\n" +
   1.229 +                                                 "}") {
   1.230 +        public String toString() {
   1.231 +            return template.replaceAll("#Context", context.getContext(samKind, samTargetType, keyword, parameterType, returnType, lambdaKind, parameterKind, genericDeclKind, lambdaBodyType));
   1.232 +        }
   1.233 +    };
   1.234 +
   1.235 +    void test() throws Exception {
   1.236 +        System.out.println("kk:");
   1.237 +        StringBuilder sb = new StringBuilder("SamKind:");
   1.238 +        sb.append(samKind).append(" SamTargetType:").append(samTargetType).append(" ParameterType:").append(parameterType)
   1.239 +            .append(" ReturnType:").append(returnType).append(" Context:").append(context).append(" LambdaKind:").append(lambdaKind)
   1.240 +            .append(" LambdaBodyType:").append(lambdaBodyType).append(" ParameterKind:").append(parameterKind).append(" Keyword:").append(keyword);
   1.241 +        System.out.println(sb);
   1.242 +        DiagnosticChecker dc = new DiagnosticChecker();
   1.243 +        JavacTask ct = (JavacTask)comp.getTask(null, fm, dc, null, null, Arrays.asList(samSourceFile, clientSourceFile));
   1.244 +        ct.analyze();
   1.245 +        if (dc.errorFound == checkTypeInference()) {
   1.246 +            throw new AssertionError(samSourceFile + "\n\n" + clientSourceFile + "\n" + parameterType + " " + returnType);
   1.247 +        }
   1.248 +    }
   1.249 +
   1.250 +    abstract class SourceFile extends SimpleJavaFileObject {
   1.251 +
   1.252 +        protected String template;
   1.253 +
   1.254 +        public SourceFile(String filename, String template) {
   1.255 +            super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
   1.256 +            this.template = template;
   1.257 +        }
   1.258 +
   1.259 +        @Override
   1.260 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.261 +            return toString();
   1.262 +        }
   1.263 +
   1.264 +        public abstract String toString();
   1.265 +    }
   1.266 +
   1.267 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.268 +
   1.269 +        boolean errorFound = false;
   1.270 +
   1.271 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.272 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.273 +                errorFound = true;
   1.274 +            }
   1.275 +        }
   1.276 +    }
   1.277 +
   1.278 +    SamKind samKind;
   1.279 +    TypeKind samTargetType;
   1.280 +    TypeKind parameterType;
   1.281 +    TypeKind returnType;
   1.282 +    Context context;
   1.283 +    LambdaBody lambdaBodyType;
   1.284 +    LambdaKind lambdaKind;
   1.285 +    ParameterKind parameterKind;
   1.286 +    Keyword keyword;
   1.287 +    GenericDeclKind genericDeclKind;
   1.288 +
   1.289 +    static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   1.290 +    static StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   1.291 +
   1.292 +    TypeInferenceComboTest(SamKind sk, TypeKind samTargetT, TypeKind parameterT, TypeKind returnT, LambdaBody lb, Context c, LambdaKind lk, ParameterKind pk, Keyword kw, GenericDeclKind gdk) {
   1.293 +        samKind = sk;
   1.294 +        samTargetType = samTargetT;
   1.295 +        parameterType = parameterT;
   1.296 +        returnType = returnT;
   1.297 +        context = c;
   1.298 +        lambdaKind = lk;
   1.299 +        parameterKind = pk;
   1.300 +        keyword = kw;
   1.301 +        lambdaBodyType = lb;
   1.302 +        genericDeclKind = gdk;
   1.303 +    }
   1.304 +
   1.305 +    public static void main(String[] args) throws Exception {
   1.306 +        for(Context ct : Context.values()) {
   1.307 +            for (TypeKind returnT : TypeKind.values()) {
   1.308 +                for (TypeKind parameterT : TypeKind.values()) {
   1.309 +                    for(LambdaBody lb : LambdaBody.values()) {
   1.310 +                        for (ParameterKind parameterK : ParameterKind.values()) {
   1.311 +                            for(LambdaKind lambdaK : LambdaKind.values()) {
   1.312 +                                for (SamKind sk : SamKind.values()) {
   1.313 +                                    if (sk == SamKind.NON_GENERIC) {
   1.314 +                                        if(parameterT != TypeKind.GENERIC && returnT != TypeKind.GENERIC )
   1.315 +                                            new TypeInferenceComboTest(sk, null, parameterT, returnT, lb, ct, lambdaK, parameterK, null, null).test();
   1.316 +                                    }
   1.317 +                                    else if (sk == SamKind.GENERIC) {
   1.318 +                                        for (Keyword kw : Keyword.values()) {
   1.319 +                                            for (TypeKind samTargetT : TypeKind.values()) {
   1.320 +                                                if(samTargetT != TypeKind.VOID && samTargetT != TypeKind.INT && samTargetT != TypeKind.GENERIC
   1.321 +                                                   && (parameterT == TypeKind.GENERIC || returnT == TypeKind.GENERIC)) {
   1.322 +                                                    if(ct != Context.METHOD_CALL) {
   1.323 +                                                        new TypeInferenceComboTest(sk, samTargetT, parameterT, returnT, lb, ct, lambdaK, parameterK, kw, null).test();
   1.324 +                                                    }
   1.325 +                                                    else {//Context.METHOD_CALL
   1.326 +                                                        for (GenericDeclKind gdk : GenericDeclKind.values())
   1.327 +                                                            new TypeInferenceComboTest(sk, samTargetT, parameterT, returnT, lb, ct, lambdaK, parameterK, kw, gdk).test();
   1.328 +                                                    }
   1.329 +                                                }
   1.330 +                                            }
   1.331 +                                        }
   1.332 +                                    }
   1.333 +                                }
   1.334 +                            }
   1.335 +                        }
   1.336 +                    }
   1.337 +                }
   1.338 +            }
   1.339 +        }
   1.340 +    }
   1.341 +}

mercurial