langtools/test/tools/javac/lambda/LambdaParserTest.java
author mcimadamore
Sat, 17 Nov 2012 19:01:03 +0000
changeset 14547 86d8d242b0c4
parent 11381 890ea587d133
child 14725 65836e833f59
permissions -rw-r--r--
8003280: Add lambda tests Summary: Turn on lambda expression, method reference and default method support Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     1
/*
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
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
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    26
 * @bug 7115050
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 11381
diff changeset
    27
 * @bug 8003280
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 11381
diff changeset
    28
 * @summary Add lambda tests
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 11381
diff changeset
    29
 *  Add parser support for lambda expressions
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    30
 */
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    31
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    32
import com.sun.source.util.JavacTask;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    33
import java.net.URI;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    34
import java.util.Arrays;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    35
import javax.tools.Diagnostic;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    36
import javax.tools.JavaCompiler;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    37
import javax.tools.JavaFileObject;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    38
import javax.tools.SimpleJavaFileObject;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    39
import javax.tools.StandardJavaFileManager;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    40
import javax.tools.ToolProvider;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    41
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    42
public class LambdaParserTest {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    43
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    44
    static int checkCount = 0;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    45
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    46
    enum LambdaKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    47
        NILARY_EXPR("()->x"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    48
        NILARY_STMT("()->{ return x; }"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    49
        ONEARY_SHORT_EXPR("x->x"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    50
        ONEARY_SHORT_STMT("x->{ return x; }"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    51
        ONEARY_EXPR("(#M1 #T1 x)->x"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    52
        ONEARY_STMT("(#M1 #T1 x)->{ return x; }"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    53
        TWOARY_EXPR("(#M1 #T1 x, #M2 #T2 y)->x"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    54
        TWOARY_STMT("(#M1 #T1 x, #M2 #T2 y)->{ return x; }");
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    55
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    56
        String lambdaTemplate;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    57
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    58
        LambdaKind(String lambdaTemplate) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    59
            this.lambdaTemplate = lambdaTemplate;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    60
        }
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
        String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2,
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    63
                ModifierKind mk1, ModifierKind mk2) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    64
            return lambdaTemplate.replaceAll("#M1", mk1.modifier)
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    65
                    .replaceAll("#M2", mk2.modifier)
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    66
                    .replaceAll("#T1", pk1.parameterType)
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    67
                    .replaceAll("#T2", pk2.parameterType);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    68
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    69
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    70
        int arity() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    71
            switch (this) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    72
                case NILARY_EXPR:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    73
                case NILARY_STMT: return 0;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    74
                case ONEARY_SHORT_EXPR:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    75
                case ONEARY_SHORT_STMT:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    76
                case ONEARY_EXPR:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    77
                case ONEARY_STMT: return 1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    78
                case TWOARY_EXPR:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    79
                case TWOARY_STMT: return 2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    80
                default: throw new AssertionError("Invalid lambda kind " + this);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    81
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    82
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    83
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    84
        boolean isShort() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    85
            return this == ONEARY_SHORT_EXPR ||
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    86
                    this == ONEARY_SHORT_STMT;
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
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    90
    enum LambdaParameterKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    91
        IMPLICIT(""),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    92
        EXPLIICT_SIMPLE("A"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    93
        EXPLICIT_VARARGS("A..."),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    94
        EXPLICIT_GENERIC1("A<X>"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    95
        EXPLICIT_GENERIC3("A<? extends X, ? super Y>");
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    96
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    97
        String parameterType;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    98
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
    99
        LambdaParameterKind(String parameterType) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   100
            this.parameterType = parameterType;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   101
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   102
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   103
        boolean explicit() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   104
            return this != IMPLICIT;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   105
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   106
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   107
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   108
    enum ModifierKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   109
        NONE(""),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   110
        FINAL("final"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   111
        PUBLIC("public");
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   112
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   113
        String modifier;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   114
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   115
        ModifierKind(String modifier) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   116
            this.modifier = modifier;
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
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   119
        boolean compatibleWith(LambdaParameterKind pk) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   120
            switch (this) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   121
                case PUBLIC: return false;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   122
                case FINAL: return pk != LambdaParameterKind.IMPLICIT;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   123
                case NONE: return true;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   124
                default: throw new AssertionError("Invalid modifier kind " + this);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   125
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   126
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   127
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   128
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   129
    enum ExprKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   130
        NONE("#L#S"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   131
        SINGLE_PAREN1("(#L#S)"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   132
        SINGLE_PAREN2("(#L)#S"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   133
        DOUBLE_PAREN1("((#L#S))"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   134
        DOUBLE_PAREN2("((#L)#S)"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   135
        DOUBLE_PAREN3("((#L))#S");
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   136
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   137
        String expressionTemplate;
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
        ExprKind(String expressionTemplate) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   140
            this.expressionTemplate = expressionTemplate;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   141
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   142
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   143
        String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2,
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   144
                ModifierKind mk1, ModifierKind mk2, LambdaKind lk, SubExprKind sk) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   145
            return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2))
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   146
                    .replaceAll("#S", sk.subExpression);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   147
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   148
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   149
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   150
    enum SubExprKind {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   151
        NONE(""),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   152
        SELECT_FIELD(".f"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   153
        SELECT_METHOD(".f()"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   154
        SELECT_NEW(".new Foo()"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   155
        POSTINC("++"),
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   156
        POSTDEC("--");
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   157
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   158
        String subExpression;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   159
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   160
        SubExprKind(String subExpression) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   161
            this.subExpression = subExpression;
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
    }
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
    public static void main(String... args) throws Exception {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   166
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   167
        //create default shared JavaCompiler - reused across multiple compilations
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   168
        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   169
        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   170
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   171
        for (LambdaKind lk : LambdaKind.values()) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   172
            for (LambdaParameterKind pk1 : LambdaParameterKind.values()) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   173
                if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT) continue;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   174
                for (LambdaParameterKind pk2 : LambdaParameterKind.values()) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   175
                    if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT) continue;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   176
                    for (ModifierKind mk1 : ModifierKind.values()) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   177
                        if (mk1 != ModifierKind.NONE && lk.isShort()) continue;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   178
                        if (lk.arity() < 1 && mk1 != ModifierKind.NONE) continue;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   179
                        for (ModifierKind mk2 : ModifierKind.values()) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   180
                            if (lk.arity() < 2 && mk2 != ModifierKind.NONE) continue;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   181
                            for (SubExprKind sk : SubExprKind.values()) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   182
                                for (ExprKind ek : ExprKind.values()) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   183
                                    new LambdaParserTest(pk1, pk2, mk1, mk2, lk, sk, ek)
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   184
                                            .run(comp, fm);
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
                            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   187
                        }
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
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   192
        System.out.println("Total check executed: " + checkCount);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   193
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   194
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   195
    LambdaParameterKind pk1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   196
    LambdaParameterKind pk2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   197
    ModifierKind mk1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   198
    ModifierKind mk2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   199
    LambdaKind lk;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   200
    SubExprKind sk;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   201
    ExprKind ek;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   202
    JavaSource source;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   203
    DiagnosticChecker diagChecker;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   204
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   205
    LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2, ModifierKind mk1,
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   206
            ModifierKind mk2, LambdaKind lk, SubExprKind sk, ExprKind ek) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   207
        this.pk1 = pk1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   208
        this.pk2 = pk2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   209
        this.mk1 = mk1;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   210
        this.mk2 = mk2;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   211
        this.lk = lk;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   212
        this.sk = sk;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   213
        this.ek = ek;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   214
        this.source = new JavaSource();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   215
        this.diagChecker = new DiagnosticChecker();
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
    class JavaSource extends SimpleJavaFileObject {
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
        String template = "class Test {\n" +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   221
                          "   SAM s = #E;\n" +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   222
                          "}";
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
        String source;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   225
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   226
        public JavaSource() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   227
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   228
            source = template.replaceAll("#E", ek.expressionString(pk1, pk2, mk1, mk2, lk, sk));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   229
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   230
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   231
        @Override
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   232
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   233
            return source;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   234
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   235
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   236
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   237
    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   238
        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 11381
diff changeset
   239
                null, null, Arrays.asList(source));
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   240
        try {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   241
            ct.parse();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   242
        } catch (Throwable ex) {
11381
890ea587d133 7124605: typos in javac comments
jjg
parents: 11143
diff changeset
   243
            throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   244
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   245
        check();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   246
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   247
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   248
    void check() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   249
        checkCount++;
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
        boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) ||
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   252
                (lk.arity() > 1 && !mk2.compatibleWith(pk2));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   253
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   254
        if (lk.arity() == 2 &&
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   255
                (pk1.explicit() != pk2.explicit() ||
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   256
                pk1 == LambdaParameterKind.EXPLICIT_VARARGS)) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   257
            errorExpected = true;
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
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   260
        if (errorExpected != diagChecker.errorFound) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   261
            throw new Error("invalid diagnostics for source:\n" +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   262
                source.getCharContent(true) +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   263
                "\nFound error: " + diagChecker.errorFound +
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   264
                "\nExpected error: " + errorExpected);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   265
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   266
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   267
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   268
    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
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
        boolean errorFound;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   271
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   272
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   273
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   274
                errorFound = true;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   275
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   276
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   277
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents:
diff changeset
   278
}