test/tools/javac/lambda/LambdaParserTest.java

Tue, 08 Jan 2013 13:47:57 +0000

author
vromero
date
Tue, 08 Jan 2013 13:47:57 +0000
changeset 1482
954541f13717
parent 1436
f6f1fd261f57
child 1503
2d2b2be57c78
permissions
-rw-r--r--

8005167: execution time of combo tests in javac should be improved
Reviewed-by: jjg, jjh

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

mercurial