test/tools/javac/lambda/LambdaParserTest.java

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

author
mcimadamore
date
Sat, 17 Nov 2012 19:01:03 +0000
changeset 1415
01c9d4161882
parent 1169
116f68a5e677
child 1436
f6f1fd261f57
permissions
-rw-r--r--

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

     1 /*
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 7115050
    27  * @bug 8003280
    28  * @summary Add lambda tests
    29  *  Add parser support for lambda expressions
    30  */
    32 import com.sun.source.util.JavacTask;
    33 import java.net.URI;
    34 import java.util.Arrays;
    35 import javax.tools.Diagnostic;
    36 import javax.tools.JavaCompiler;
    37 import javax.tools.JavaFileObject;
    38 import javax.tools.SimpleJavaFileObject;
    39 import javax.tools.StandardJavaFileManager;
    40 import javax.tools.ToolProvider;
    42 public class LambdaParserTest {
    44     static int checkCount = 0;
    46     enum LambdaKind {
    47         NILARY_EXPR("()->x"),
    48         NILARY_STMT("()->{ return x; }"),
    49         ONEARY_SHORT_EXPR("x->x"),
    50         ONEARY_SHORT_STMT("x->{ return x; }"),
    51         ONEARY_EXPR("(#M1 #T1 x)->x"),
    52         ONEARY_STMT("(#M1 #T1 x)->{ return x; }"),
    53         TWOARY_EXPR("(#M1 #T1 x, #M2 #T2 y)->x"),
    54         TWOARY_STMT("(#M1 #T1 x, #M2 #T2 y)->{ return x; }");
    56         String lambdaTemplate;
    58         LambdaKind(String lambdaTemplate) {
    59             this.lambdaTemplate = lambdaTemplate;
    60         }
    62         String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2,
    63                 ModifierKind mk1, ModifierKind mk2) {
    64             return lambdaTemplate.replaceAll("#M1", mk1.modifier)
    65                     .replaceAll("#M2", mk2.modifier)
    66                     .replaceAll("#T1", pk1.parameterType)
    67                     .replaceAll("#T2", pk2.parameterType);
    68         }
    70         int arity() {
    71             switch (this) {
    72                 case NILARY_EXPR:
    73                 case NILARY_STMT: return 0;
    74                 case ONEARY_SHORT_EXPR:
    75                 case ONEARY_SHORT_STMT:
    76                 case ONEARY_EXPR:
    77                 case ONEARY_STMT: return 1;
    78                 case TWOARY_EXPR:
    79                 case TWOARY_STMT: return 2;
    80                 default: throw new AssertionError("Invalid lambda kind " + this);
    81             }
    82         }
    84         boolean isShort() {
    85             return this == ONEARY_SHORT_EXPR ||
    86                     this == ONEARY_SHORT_STMT;
    87         }
    88     }
    90     enum LambdaParameterKind {
    91         IMPLICIT(""),
    92         EXPLIICT_SIMPLE("A"),
    93         EXPLICIT_VARARGS("A..."),
    94         EXPLICIT_GENERIC1("A<X>"),
    95         EXPLICIT_GENERIC3("A<? extends X, ? super Y>");
    97         String parameterType;
    99         LambdaParameterKind(String parameterType) {
   100             this.parameterType = parameterType;
   101         }
   103         boolean explicit() {
   104             return this != IMPLICIT;
   105         }
   106     }
   108     enum ModifierKind {
   109         NONE(""),
   110         FINAL("final"),
   111         PUBLIC("public");
   113         String modifier;
   115         ModifierKind(String modifier) {
   116             this.modifier = modifier;
   117         }
   119         boolean compatibleWith(LambdaParameterKind pk) {
   120             switch (this) {
   121                 case PUBLIC: return false;
   122                 case FINAL: return pk != LambdaParameterKind.IMPLICIT;
   123                 case NONE: return true;
   124                 default: throw new AssertionError("Invalid modifier kind " + this);
   125             }
   126         }
   127     }
   129     enum ExprKind {
   130         NONE("#L#S"),
   131         SINGLE_PAREN1("(#L#S)"),
   132         SINGLE_PAREN2("(#L)#S"),
   133         DOUBLE_PAREN1("((#L#S))"),
   134         DOUBLE_PAREN2("((#L)#S)"),
   135         DOUBLE_PAREN3("((#L))#S");
   137         String expressionTemplate;
   139         ExprKind(String expressionTemplate) {
   140             this.expressionTemplate = expressionTemplate;
   141         }
   143         String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2,
   144                 ModifierKind mk1, ModifierKind mk2, LambdaKind lk, SubExprKind sk) {
   145             return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2))
   146                     .replaceAll("#S", sk.subExpression);
   147         }
   148     }
   150     enum SubExprKind {
   151         NONE(""),
   152         SELECT_FIELD(".f"),
   153         SELECT_METHOD(".f()"),
   154         SELECT_NEW(".new Foo()"),
   155         POSTINC("++"),
   156         POSTDEC("--");
   158         String subExpression;
   160         SubExprKind(String subExpression) {
   161             this.subExpression = subExpression;
   162         }
   163     }
   165     public static void main(String... args) throws Exception {
   167         //create default shared JavaCompiler - reused across multiple compilations
   168         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   169         StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   171         for (LambdaKind lk : LambdaKind.values()) {
   172             for (LambdaParameterKind pk1 : LambdaParameterKind.values()) {
   173                 if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT) continue;
   174                 for (LambdaParameterKind pk2 : LambdaParameterKind.values()) {
   175                     if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT) continue;
   176                     for (ModifierKind mk1 : ModifierKind.values()) {
   177                         if (mk1 != ModifierKind.NONE && lk.isShort()) continue;
   178                         if (lk.arity() < 1 && mk1 != ModifierKind.NONE) continue;
   179                         for (ModifierKind mk2 : ModifierKind.values()) {
   180                             if (lk.arity() < 2 && mk2 != ModifierKind.NONE) continue;
   181                             for (SubExprKind sk : SubExprKind.values()) {
   182                                 for (ExprKind ek : ExprKind.values()) {
   183                                     new LambdaParserTest(pk1, pk2, mk1, mk2, lk, sk, ek)
   184                                             .run(comp, fm);
   185                                 }
   186                             }
   187                         }
   188                     }
   189                 }
   190             }
   191         }
   192         System.out.println("Total check executed: " + checkCount);
   193     }
   195     LambdaParameterKind pk1;
   196     LambdaParameterKind pk2;
   197     ModifierKind mk1;
   198     ModifierKind mk2;
   199     LambdaKind lk;
   200     SubExprKind sk;
   201     ExprKind ek;
   202     JavaSource source;
   203     DiagnosticChecker diagChecker;
   205     LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2, ModifierKind mk1,
   206             ModifierKind mk2, LambdaKind lk, SubExprKind sk, ExprKind ek) {
   207         this.pk1 = pk1;
   208         this.pk2 = pk2;
   209         this.mk1 = mk1;
   210         this.mk2 = mk2;
   211         this.lk = lk;
   212         this.sk = sk;
   213         this.ek = ek;
   214         this.source = new JavaSource();
   215         this.diagChecker = new DiagnosticChecker();
   216     }
   218     class JavaSource extends SimpleJavaFileObject {
   220         String template = "class Test {\n" +
   221                           "   SAM s = #E;\n" +
   222                           "}";
   224         String source;
   226         public JavaSource() {
   227             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   228             source = template.replaceAll("#E", ek.expressionString(pk1, pk2, mk1, mk2, lk, sk));
   229         }
   231         @Override
   232         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   233             return source;
   234         }
   235     }
   237     void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   238         JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   239                 null, null, Arrays.asList(source));
   240         try {
   241             ct.parse();
   242         } catch (Throwable ex) {
   243             throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
   244         }
   245         check();
   246     }
   248     void check() {
   249         checkCount++;
   251         boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) ||
   252                 (lk.arity() > 1 && !mk2.compatibleWith(pk2));
   254         if (lk.arity() == 2 &&
   255                 (pk1.explicit() != pk2.explicit() ||
   256                 pk1 == LambdaParameterKind.EXPLICIT_VARARGS)) {
   257             errorExpected = true;
   258         }
   260         if (errorExpected != diagChecker.errorFound) {
   261             throw new Error("invalid diagnostics for source:\n" +
   262                 source.getCharContent(true) +
   263                 "\nFound error: " + diagChecker.errorFound +
   264                 "\nExpected error: " + errorExpected);
   265         }
   266     }
   268     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   270         boolean errorFound;
   272         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   273             if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   274                 errorFound = true;
   275             }
   276         }
   277     }
   278 }

mercurial