langtools/test/tools/javac/lambda/LambdaParserTest.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 15384 5a8d00abf076
child 32454 b0ac04e0fefe
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 15384
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     4
 *
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     8
 *
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    14
 *
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    18
 *
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    21
 * questions.
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    22
 */
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    23
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    24
/*
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    25
 * @test
15384
5a8d00abf076 8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents: 15367
diff changeset
    26
 * @bug 7115050 8003280 8005852 8006694
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 11381
diff changeset
    27
 * @summary Add lambda tests
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 11381
diff changeset
    28
 *  Add parser support for lambda expressions
15384
5a8d00abf076 8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents: 15367
diff changeset
    29
 *  temporarily workaround combo tests are causing time out in several platforms
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
    30
 * @library ../lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 15384
diff changeset
    31
 * @modules jdk.compiler
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
    32
 * @build JavacTestingAbstractThreadedTest
15384
5a8d00abf076 8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents: 15367
diff changeset
    33
 * @run main/othervm LambdaParserTest
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    34
 */
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    35
15384
5a8d00abf076 8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents: 15367
diff changeset
    36
// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
5a8d00abf076 8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents: 15367
diff changeset
    37
// see JDK-8006746
5a8d00abf076 8006694: temporarily workaround combo tests are causing time out in several platforms
vromero
parents: 15367
diff changeset
    38
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    39
import java.net.URI;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    40
import java.util.Arrays;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    41
import javax.tools.Diagnostic;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    42
import javax.tools.JavaFileObject;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    43
import javax.tools.SimpleJavaFileObject;
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
    44
import com.sun.source.util.JavacTask;
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    45
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
    46
public class LambdaParserTest
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
    47
    extends JavacTestingAbstractThreadedTest
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
    48
    implements Runnable {
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    49
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    50
    enum LambdaKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    51
        NILARY_EXPR("()->x"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    52
        NILARY_STMT("()->{ return x; }"),
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    53
        ONEARY_SHORT_EXPR("#PN->x"),
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    54
        ONEARY_SHORT_STMT("#PN->{ return x; }"),
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    55
        ONEARY_EXPR("(#M1 #T1 #PN)->x"),
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    56
        ONEARY_STMT("(#M1 #T1 #PN)->{ return x; }"),
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    57
        TWOARY_EXPR("(#M1 #T1 #PN, #M2 #T2 y)->x"),
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    58
        TWOARY_STMT("(#M1 #T1 #PN, #M2 #T2 y)->{ return x; }");
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    59
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    60
        String lambdaTemplate;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    61
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    62
        LambdaKind(String lambdaTemplate) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    63
            this.lambdaTemplate = lambdaTemplate;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    64
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    65
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    66
        String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2,
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    67
                ModifierKind mk1, ModifierKind mk2, LambdaParameterName pn) {
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    68
            return lambdaTemplate.replaceAll("#M1", mk1.modifier)
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    69
                    .replaceAll("#M2", mk2.modifier)
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    70
                    .replaceAll("#T1", pk1.parameterType)
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    71
                    .replaceAll("#T2", pk2.parameterType)
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    72
                    .replaceAll("#PN", pn.nameStr);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    73
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    74
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    75
        int arity() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    76
            switch (this) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    77
                case NILARY_EXPR:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    78
                case NILARY_STMT: return 0;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    79
                case ONEARY_SHORT_EXPR:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    80
                case ONEARY_SHORT_STMT:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    81
                case ONEARY_EXPR:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    82
                case ONEARY_STMT: return 1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    83
                case TWOARY_EXPR:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    84
                case TWOARY_STMT: return 2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    85
                default: throw new AssertionError("Invalid lambda kind " + this);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    86
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    87
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    88
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    89
        boolean isShort() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    90
            return this == ONEARY_SHORT_EXPR ||
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    91
                    this == ONEARY_SHORT_STMT;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    92
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    93
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    94
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    95
    enum LambdaParameterName {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    96
        IDENT("x"),
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    97
        UNDERSCORE("_");
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    98
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
    99
        String nameStr;
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   100
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   101
        LambdaParameterName(String nameStr) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   102
            this.nameStr = nameStr;
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   103
        }
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   104
    }
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   105
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   106
    enum LambdaParameterKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   107
        IMPLICIT(""),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   108
        EXPLIICT_SIMPLE("A"),
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   109
        EXPLIICT_SIMPLE_ARR1("A[]"),
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   110
        EXPLIICT_SIMPLE_ARR2("A[][]"),
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   111
        EXPLICIT_VARARGS("A..."),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   112
        EXPLICIT_GENERIC1("A<X>"),
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   113
        EXPLICIT_GENERIC2("A<? extends X, ? super Y>"),
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   114
        EXPLICIT_GENERIC2_VARARGS("A<? extends X, ? super Y>..."),
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   115
        EXPLICIT_GENERIC2_ARR1("A<? extends X, ? super Y>[]"),
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   116
        EXPLICIT_GENERIC2_ARR2("A<? extends X, ? super Y>[][]");
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   117
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   118
        String parameterType;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   119
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   120
        LambdaParameterKind(String parameterType) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   121
            this.parameterType = parameterType;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   122
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   123
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   124
        boolean explicit() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   125
            return this != IMPLICIT;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   126
        }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   127
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   128
        boolean isVarargs() {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   129
            return this == EXPLICIT_VARARGS ||
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   130
                    this == EXPLICIT_GENERIC2_VARARGS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   131
        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   132
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   133
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   134
    enum ModifierKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   135
        NONE(""),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   136
        FINAL("final"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   137
        PUBLIC("public");
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   138
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   139
        String modifier;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   140
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   141
        ModifierKind(String modifier) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   142
            this.modifier = modifier;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   143
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   144
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   145
        boolean compatibleWith(LambdaParameterKind pk) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   146
            switch (this) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   147
                case PUBLIC: return false;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   148
                case FINAL: return pk != LambdaParameterKind.IMPLICIT;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   149
                case NONE: return true;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   150
                default: throw new AssertionError("Invalid modifier kind " + this);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   151
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   152
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   153
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   154
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   155
    enum ExprKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   156
        NONE("#L#S"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   157
        SINGLE_PAREN1("(#L#S)"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   158
        SINGLE_PAREN2("(#L)#S"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   159
        DOUBLE_PAREN1("((#L#S))"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   160
        DOUBLE_PAREN2("((#L)#S)"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   161
        DOUBLE_PAREN3("((#L))#S");
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   162
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   163
        String expressionTemplate;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   164
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   165
        ExprKind(String expressionTemplate) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   166
            this.expressionTemplate = expressionTemplate;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   167
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   168
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   169
        String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2,
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   170
                ModifierKind mk1, ModifierKind mk2, LambdaKind lk, LambdaParameterName pn, SubExprKind sk) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   171
            return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2, pn))
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   172
                    .replaceAll("#S", sk.subExpression);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   173
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   174
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   175
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   176
    enum SubExprKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   177
        NONE(""),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   178
        SELECT_FIELD(".f"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   179
        SELECT_METHOD(".f()"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   180
        SELECT_NEW(".new Foo()"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   181
        POSTINC("++"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   182
        POSTDEC("--");
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   183
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   184
        String subExpression;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   185
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   186
        SubExprKind(String subExpression) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   187
            this.subExpression = subExpression;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   188
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   189
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   190
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   191
    public static void main(String... args) throws Exception {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   192
        for (LambdaKind lk : LambdaKind.values()) {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   193
            for (LambdaParameterName pn : LambdaParameterName.values()) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   194
                for (LambdaParameterKind pk1 : LambdaParameterKind.values()) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   195
                    if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT)
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   196
                        continue;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   197
                    for (LambdaParameterKind pk2 : LambdaParameterKind.values()) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   198
                        if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT)
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   199
                            continue;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   200
                        for (ModifierKind mk1 : ModifierKind.values()) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   201
                            if (mk1 != ModifierKind.NONE && lk.isShort())
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   202
                                continue;
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   203
                            if (lk.arity() < 1 && mk1 != ModifierKind.NONE)
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   204
                                continue;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   205
                            for (ModifierKind mk2 : ModifierKind.values()) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   206
                                if (lk.arity() < 2 && mk2 != ModifierKind.NONE)
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   207
                                    continue;
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   208
                                for (SubExprKind sk : SubExprKind.values()) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   209
                                    for (ExprKind ek : ExprKind.values()) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   210
                                        pool.execute(
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   211
                                            new LambdaParserTest(pk1, pk2, mk1,
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   212
                                                                 mk2, lk, sk, ek, pn));
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   213
                                    }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   214
                                }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   215
                            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   216
                        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   217
                    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   218
                }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   219
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   220
        }
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   221
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   222
        checkAfterExec();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   223
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   224
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   225
    LambdaParameterKind pk1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   226
    LambdaParameterKind pk2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   227
    ModifierKind mk1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   228
    ModifierKind mk2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   229
    LambdaKind lk;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   230
    LambdaParameterName pn;
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   231
    SubExprKind sk;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   232
    ExprKind ek;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   233
    JavaSource source;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   234
    DiagnosticChecker diagChecker;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   235
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   236
    LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2,
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   237
            ModifierKind mk1, ModifierKind mk2, LambdaKind lk,
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   238
            SubExprKind sk, ExprKind ek, LambdaParameterName pn) {
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   239
        this.pk1 = pk1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   240
        this.pk2 = pk2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   241
        this.mk1 = mk1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   242
        this.mk2 = mk2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   243
        this.lk = lk;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   244
        this.pn = pn;
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   245
        this.sk = sk;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   246
        this.ek = ek;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   247
        this.source = new JavaSource();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   248
        this.diagChecker = new DiagnosticChecker();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   249
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   250
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   251
    class JavaSource extends SimpleJavaFileObject {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   252
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   253
        String template = "class Test {\n" +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   254
                          "   SAM s = #E;\n" +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   255
                          "}";
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   256
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   257
        String source;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   258
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   259
        public JavaSource() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   260
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   261
            source = template.replaceAll("#E",
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   262
                    ek.expressionString(pk1, pk2, mk1, mk2, lk, pn, sk));
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   263
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   264
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   265
        @Override
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   266
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   267
            return source;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   268
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   269
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   270
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   271
    public void run() {
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   272
        JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 11381
diff changeset
   273
                null, null, Arrays.asList(source));
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   274
        try {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   275
            ct.parse();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   276
        } catch (Throwable ex) {
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   277
            processException(ex);
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   278
            return;
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   279
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   280
        check();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   281
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   282
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   283
    void check() {
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   284
        checkCount.incrementAndGet();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   285
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   286
        boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) ||
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   287
                (lk.arity() > 1 && !mk2.compatibleWith(pk2));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   288
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   289
        if (lk.arity() == 2 &&
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   290
                (pk1.explicit() != pk2.explicit() ||
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14547
diff changeset
   291
                pk1.isVarargs())) {
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   292
            errorExpected = true;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   293
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   294
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   295
        errorExpected |= pn == LambdaParameterName.UNDERSCORE &&
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   296
                lk.arity() > 0;
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 15040
diff changeset
   297
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   298
        if (errorExpected != diagChecker.errorFound) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   299
            throw new Error("invalid diagnostics for source:\n" +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   300
                source.getCharContent(true) +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   301
                "\nFound error: " + diagChecker.errorFound +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   302
                "\nExpected error: " + errorExpected);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   303
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   304
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   305
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   306
    static class DiagnosticChecker
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   307
        implements javax.tools.DiagnosticListener<JavaFileObject> {
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   308
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   309
        boolean errorFound;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   310
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   311
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   312
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   313
                errorFound = true;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   314
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   315
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   316
    }
15040
99fd9483d3f0 8005167: execution time of combo tests in javac should be improved
vromero
parents: 14725
diff changeset
   317
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   318
}