test/tools/javac/lambda/LambdaParserTest.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/LambdaParserTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,317 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 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 7115050 8003280 8005852 8006694
    1.30 + * @summary Add lambda tests
    1.31 + *  Add parser support for lambda expressions
    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 LambdaParserTest
    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 +
    1.48 +public class LambdaParserTest
    1.49 +    extends JavacTestingAbstractThreadedTest
    1.50 +    implements Runnable {
    1.51 +
    1.52 +    enum LambdaKind {
    1.53 +        NILARY_EXPR("()->x"),
    1.54 +        NILARY_STMT("()->{ return x; }"),
    1.55 +        ONEARY_SHORT_EXPR("#PN->x"),
    1.56 +        ONEARY_SHORT_STMT("#PN->{ return x; }"),
    1.57 +        ONEARY_EXPR("(#M1 #T1 #PN)->x"),
    1.58 +        ONEARY_STMT("(#M1 #T1 #PN)->{ return x; }"),
    1.59 +        TWOARY_EXPR("(#M1 #T1 #PN, #M2 #T2 y)->x"),
    1.60 +        TWOARY_STMT("(#M1 #T1 #PN, #M2 #T2 y)->{ return x; }");
    1.61 +
    1.62 +        String lambdaTemplate;
    1.63 +
    1.64 +        LambdaKind(String lambdaTemplate) {
    1.65 +            this.lambdaTemplate = lambdaTemplate;
    1.66 +        }
    1.67 +
    1.68 +        String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2,
    1.69 +                ModifierKind mk1, ModifierKind mk2, LambdaParameterName pn) {
    1.70 +            return lambdaTemplate.replaceAll("#M1", mk1.modifier)
    1.71 +                    .replaceAll("#M2", mk2.modifier)
    1.72 +                    .replaceAll("#T1", pk1.parameterType)
    1.73 +                    .replaceAll("#T2", pk2.parameterType)
    1.74 +                    .replaceAll("#PN", pn.nameStr);
    1.75 +        }
    1.76 +
    1.77 +        int arity() {
    1.78 +            switch (this) {
    1.79 +                case NILARY_EXPR:
    1.80 +                case NILARY_STMT: return 0;
    1.81 +                case ONEARY_SHORT_EXPR:
    1.82 +                case ONEARY_SHORT_STMT:
    1.83 +                case ONEARY_EXPR:
    1.84 +                case ONEARY_STMT: return 1;
    1.85 +                case TWOARY_EXPR:
    1.86 +                case TWOARY_STMT: return 2;
    1.87 +                default: throw new AssertionError("Invalid lambda kind " + this);
    1.88 +            }
    1.89 +        }
    1.90 +
    1.91 +        boolean isShort() {
    1.92 +            return this == ONEARY_SHORT_EXPR ||
    1.93 +                    this == ONEARY_SHORT_STMT;
    1.94 +        }
    1.95 +    }
    1.96 +
    1.97 +    enum LambdaParameterName {
    1.98 +        IDENT("x"),
    1.99 +        UNDERSCORE("_");
   1.100 +
   1.101 +        String nameStr;
   1.102 +
   1.103 +        LambdaParameterName(String nameStr) {
   1.104 +            this.nameStr = nameStr;
   1.105 +        }
   1.106 +    }
   1.107 +
   1.108 +    enum LambdaParameterKind {
   1.109 +        IMPLICIT(""),
   1.110 +        EXPLIICT_SIMPLE("A"),
   1.111 +        EXPLIICT_SIMPLE_ARR1("A[]"),
   1.112 +        EXPLIICT_SIMPLE_ARR2("A[][]"),
   1.113 +        EXPLICIT_VARARGS("A..."),
   1.114 +        EXPLICIT_GENERIC1("A<X>"),
   1.115 +        EXPLICIT_GENERIC2("A<? extends X, ? super Y>"),
   1.116 +        EXPLICIT_GENERIC2_VARARGS("A<? extends X, ? super Y>..."),
   1.117 +        EXPLICIT_GENERIC2_ARR1("A<? extends X, ? super Y>[]"),
   1.118 +        EXPLICIT_GENERIC2_ARR2("A<? extends X, ? super Y>[][]");
   1.119 +
   1.120 +        String parameterType;
   1.121 +
   1.122 +        LambdaParameterKind(String parameterType) {
   1.123 +            this.parameterType = parameterType;
   1.124 +        }
   1.125 +
   1.126 +        boolean explicit() {
   1.127 +            return this != IMPLICIT;
   1.128 +        }
   1.129 +
   1.130 +        boolean isVarargs() {
   1.131 +            return this == EXPLICIT_VARARGS ||
   1.132 +                    this == EXPLICIT_GENERIC2_VARARGS;
   1.133 +        }
   1.134 +    }
   1.135 +
   1.136 +    enum ModifierKind {
   1.137 +        NONE(""),
   1.138 +        FINAL("final"),
   1.139 +        PUBLIC("public");
   1.140 +
   1.141 +        String modifier;
   1.142 +
   1.143 +        ModifierKind(String modifier) {
   1.144 +            this.modifier = modifier;
   1.145 +        }
   1.146 +
   1.147 +        boolean compatibleWith(LambdaParameterKind pk) {
   1.148 +            switch (this) {
   1.149 +                case PUBLIC: return false;
   1.150 +                case FINAL: return pk != LambdaParameterKind.IMPLICIT;
   1.151 +                case NONE: return true;
   1.152 +                default: throw new AssertionError("Invalid modifier kind " + this);
   1.153 +            }
   1.154 +        }
   1.155 +    }
   1.156 +
   1.157 +    enum ExprKind {
   1.158 +        NONE("#L#S"),
   1.159 +        SINGLE_PAREN1("(#L#S)"),
   1.160 +        SINGLE_PAREN2("(#L)#S"),
   1.161 +        DOUBLE_PAREN1("((#L#S))"),
   1.162 +        DOUBLE_PAREN2("((#L)#S)"),
   1.163 +        DOUBLE_PAREN3("((#L))#S");
   1.164 +
   1.165 +        String expressionTemplate;
   1.166 +
   1.167 +        ExprKind(String expressionTemplate) {
   1.168 +            this.expressionTemplate = expressionTemplate;
   1.169 +        }
   1.170 +
   1.171 +        String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2,
   1.172 +                ModifierKind mk1, ModifierKind mk2, LambdaKind lk, LambdaParameterName pn, SubExprKind sk) {
   1.173 +            return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2, pn))
   1.174 +                    .replaceAll("#S", sk.subExpression);
   1.175 +        }
   1.176 +    }
   1.177 +
   1.178 +    enum SubExprKind {
   1.179 +        NONE(""),
   1.180 +        SELECT_FIELD(".f"),
   1.181 +        SELECT_METHOD(".f()"),
   1.182 +        SELECT_NEW(".new Foo()"),
   1.183 +        POSTINC("++"),
   1.184 +        POSTDEC("--");
   1.185 +
   1.186 +        String subExpression;
   1.187 +
   1.188 +        SubExprKind(String subExpression) {
   1.189 +            this.subExpression = subExpression;
   1.190 +        }
   1.191 +    }
   1.192 +
   1.193 +    public static void main(String... args) throws Exception {
   1.194 +        for (LambdaKind lk : LambdaKind.values()) {
   1.195 +            for (LambdaParameterName pn : LambdaParameterName.values()) {
   1.196 +                for (LambdaParameterKind pk1 : LambdaParameterKind.values()) {
   1.197 +                    if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT)
   1.198 +                        continue;
   1.199 +                    for (LambdaParameterKind pk2 : LambdaParameterKind.values()) {
   1.200 +                        if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT)
   1.201 +                            continue;
   1.202 +                        for (ModifierKind mk1 : ModifierKind.values()) {
   1.203 +                            if (mk1 != ModifierKind.NONE && lk.isShort())
   1.204 +                                continue;
   1.205 +                            if (lk.arity() < 1 && mk1 != ModifierKind.NONE)
   1.206 +                                continue;
   1.207 +                            for (ModifierKind mk2 : ModifierKind.values()) {
   1.208 +                                if (lk.arity() < 2 && mk2 != ModifierKind.NONE)
   1.209 +                                    continue;
   1.210 +                                for (SubExprKind sk : SubExprKind.values()) {
   1.211 +                                    for (ExprKind ek : ExprKind.values()) {
   1.212 +                                        pool.execute(
   1.213 +                                            new LambdaParserTest(pk1, pk2, mk1,
   1.214 +                                                                 mk2, lk, sk, ek, pn));
   1.215 +                                    }
   1.216 +                                }
   1.217 +                            }
   1.218 +                        }
   1.219 +                    }
   1.220 +                }
   1.221 +            }
   1.222 +        }
   1.223 +
   1.224 +        checkAfterExec();
   1.225 +    }
   1.226 +
   1.227 +    LambdaParameterKind pk1;
   1.228 +    LambdaParameterKind pk2;
   1.229 +    ModifierKind mk1;
   1.230 +    ModifierKind mk2;
   1.231 +    LambdaKind lk;
   1.232 +    LambdaParameterName pn;
   1.233 +    SubExprKind sk;
   1.234 +    ExprKind ek;
   1.235 +    JavaSource source;
   1.236 +    DiagnosticChecker diagChecker;
   1.237 +
   1.238 +    LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2,
   1.239 +            ModifierKind mk1, ModifierKind mk2, LambdaKind lk,
   1.240 +            SubExprKind sk, ExprKind ek, LambdaParameterName pn) {
   1.241 +        this.pk1 = pk1;
   1.242 +        this.pk2 = pk2;
   1.243 +        this.mk1 = mk1;
   1.244 +        this.mk2 = mk2;
   1.245 +        this.lk = lk;
   1.246 +        this.pn = pn;
   1.247 +        this.sk = sk;
   1.248 +        this.ek = ek;
   1.249 +        this.source = new JavaSource();
   1.250 +        this.diagChecker = new DiagnosticChecker();
   1.251 +    }
   1.252 +
   1.253 +    class JavaSource extends SimpleJavaFileObject {
   1.254 +
   1.255 +        String template = "class Test {\n" +
   1.256 +                          "   SAM s = #E;\n" +
   1.257 +                          "}";
   1.258 +
   1.259 +        String source;
   1.260 +
   1.261 +        public JavaSource() {
   1.262 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.263 +            source = template.replaceAll("#E",
   1.264 +                    ek.expressionString(pk1, pk2, mk1, mk2, lk, pn, sk));
   1.265 +        }
   1.266 +
   1.267 +        @Override
   1.268 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.269 +            return source;
   1.270 +        }
   1.271 +    }
   1.272 +
   1.273 +    public void run() {
   1.274 +        JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
   1.275 +                null, null, Arrays.asList(source));
   1.276 +        try {
   1.277 +            ct.parse();
   1.278 +        } catch (Throwable ex) {
   1.279 +            processException(ex);
   1.280 +            return;
   1.281 +        }
   1.282 +        check();
   1.283 +    }
   1.284 +
   1.285 +    void check() {
   1.286 +        checkCount.incrementAndGet();
   1.287 +
   1.288 +        boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) ||
   1.289 +                (lk.arity() > 1 && !mk2.compatibleWith(pk2));
   1.290 +
   1.291 +        if (lk.arity() == 2 &&
   1.292 +                (pk1.explicit() != pk2.explicit() ||
   1.293 +                pk1.isVarargs())) {
   1.294 +            errorExpected = true;
   1.295 +        }
   1.296 +
   1.297 +        errorExpected |= pn == LambdaParameterName.UNDERSCORE &&
   1.298 +                lk.arity() > 0;
   1.299 +
   1.300 +        if (errorExpected != diagChecker.errorFound) {
   1.301 +            throw new Error("invalid diagnostics for source:\n" +
   1.302 +                source.getCharContent(true) +
   1.303 +                "\nFound error: " + diagChecker.errorFound +
   1.304 +                "\nExpected error: " + errorExpected);
   1.305 +        }
   1.306 +    }
   1.307 +
   1.308 +    static class DiagnosticChecker
   1.309 +        implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.310 +
   1.311 +        boolean errorFound;
   1.312 +
   1.313 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.314 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.315 +                errorFound = true;
   1.316 +            }
   1.317 +        }
   1.318 +    }
   1.319 +
   1.320 +}

mercurial