test/tools/javac/lambda/LambdaParserTest.java

Thu, 17 Jan 2013 18:15:20 +0000

author
mcimadamore
date
Thu, 17 Jan 2013 18:15:20 +0000
changeset 1503
2d2b2be57c78
parent 1482
954541f13717
child 1520
5c956be64b9e
permissions
-rw-r--r--

8005852: Treatment of '_' as identifier
Summary: warn when '_' is found in an identifier position
Reviewed-by: jjg

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@1503 26 * @bug 7115050 8003280 8005852
mcimadamore@1415 27 * @summary Add lambda tests
mcimadamore@1415 28 * Add parser support for lambda expressions
vromero@1482 29 * @library ../lib
vromero@1482 30 * @build JavacTestingAbstractThreadedTest
vromero@1482 31 * @run main LambdaParserTest
mcimadamore@1144 32 */
mcimadamore@1144 33
mcimadamore@1144 34 import java.net.URI;
mcimadamore@1144 35 import java.util.Arrays;
mcimadamore@1144 36 import javax.tools.Diagnostic;
mcimadamore@1144 37 import javax.tools.JavaFileObject;
mcimadamore@1144 38 import javax.tools.SimpleJavaFileObject;
vromero@1482 39 import com.sun.source.util.JavacTask;
mcimadamore@1144 40
vromero@1482 41 public class LambdaParserTest
vromero@1482 42 extends JavacTestingAbstractThreadedTest
vromero@1482 43 implements Runnable {
mcimadamore@1144 44
mcimadamore@1144 45 enum LambdaKind {
mcimadamore@1144 46 NILARY_EXPR("()->x"),
mcimadamore@1144 47 NILARY_STMT("()->{ return x; }"),
mcimadamore@1503 48 ONEARY_SHORT_EXPR("#PN->x"),
mcimadamore@1503 49 ONEARY_SHORT_STMT("#PN->{ return x; }"),
mcimadamore@1503 50 ONEARY_EXPR("(#M1 #T1 #PN)->x"),
mcimadamore@1503 51 ONEARY_STMT("(#M1 #T1 #PN)->{ return x; }"),
mcimadamore@1503 52 TWOARY_EXPR("(#M1 #T1 #PN, #M2 #T2 y)->x"),
mcimadamore@1503 53 TWOARY_STMT("(#M1 #T1 #PN, #M2 #T2 y)->{ return x; }");
mcimadamore@1144 54
mcimadamore@1144 55 String lambdaTemplate;
mcimadamore@1144 56
mcimadamore@1144 57 LambdaKind(String lambdaTemplate) {
mcimadamore@1144 58 this.lambdaTemplate = lambdaTemplate;
mcimadamore@1144 59 }
mcimadamore@1144 60
mcimadamore@1144 61 String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2,
mcimadamore@1503 62 ModifierKind mk1, ModifierKind mk2, LambdaParameterName pn) {
mcimadamore@1144 63 return lambdaTemplate.replaceAll("#M1", mk1.modifier)
mcimadamore@1144 64 .replaceAll("#M2", mk2.modifier)
mcimadamore@1144 65 .replaceAll("#T1", pk1.parameterType)
mcimadamore@1503 66 .replaceAll("#T2", pk2.parameterType)
mcimadamore@1503 67 .replaceAll("#PN", pn.nameStr);
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@1503 90 enum LambdaParameterName {
mcimadamore@1503 91 IDENT("x"),
mcimadamore@1503 92 UNDERSCORE("_");
mcimadamore@1503 93
mcimadamore@1503 94 String nameStr;
mcimadamore@1503 95
mcimadamore@1503 96 LambdaParameterName(String nameStr) {
mcimadamore@1503 97 this.nameStr = nameStr;
mcimadamore@1503 98 }
mcimadamore@1503 99 }
mcimadamore@1503 100
mcimadamore@1144 101 enum LambdaParameterKind {
mcimadamore@1144 102 IMPLICIT(""),
mcimadamore@1144 103 EXPLIICT_SIMPLE("A"),
mcimadamore@1436 104 EXPLIICT_SIMPLE_ARR1("A[]"),
mcimadamore@1436 105 EXPLIICT_SIMPLE_ARR2("A[][]"),
mcimadamore@1144 106 EXPLICIT_VARARGS("A..."),
mcimadamore@1144 107 EXPLICIT_GENERIC1("A<X>"),
mcimadamore@1436 108 EXPLICIT_GENERIC2("A<? extends X, ? super Y>"),
mcimadamore@1436 109 EXPLICIT_GENERIC2_VARARGS("A<? extends X, ? super Y>..."),
mcimadamore@1436 110 EXPLICIT_GENERIC2_ARR1("A<? extends X, ? super Y>[]"),
mcimadamore@1436 111 EXPLICIT_GENERIC2_ARR2("A<? extends X, ? super Y>[][]");
mcimadamore@1144 112
mcimadamore@1144 113 String parameterType;
mcimadamore@1144 114
mcimadamore@1144 115 LambdaParameterKind(String parameterType) {
mcimadamore@1144 116 this.parameterType = parameterType;
mcimadamore@1144 117 }
mcimadamore@1144 118
mcimadamore@1144 119 boolean explicit() {
mcimadamore@1144 120 return this != IMPLICIT;
mcimadamore@1144 121 }
mcimadamore@1436 122
mcimadamore@1436 123 boolean isVarargs() {
mcimadamore@1436 124 return this == EXPLICIT_VARARGS ||
mcimadamore@1436 125 this == EXPLICIT_GENERIC2_VARARGS;
mcimadamore@1436 126 }
mcimadamore@1144 127 }
mcimadamore@1144 128
mcimadamore@1144 129 enum ModifierKind {
mcimadamore@1144 130 NONE(""),
mcimadamore@1144 131 FINAL("final"),
mcimadamore@1144 132 PUBLIC("public");
mcimadamore@1144 133
mcimadamore@1144 134 String modifier;
mcimadamore@1144 135
mcimadamore@1144 136 ModifierKind(String modifier) {
mcimadamore@1144 137 this.modifier = modifier;
mcimadamore@1144 138 }
mcimadamore@1144 139
mcimadamore@1144 140 boolean compatibleWith(LambdaParameterKind pk) {
mcimadamore@1144 141 switch (this) {
mcimadamore@1144 142 case PUBLIC: return false;
mcimadamore@1144 143 case FINAL: return pk != LambdaParameterKind.IMPLICIT;
mcimadamore@1144 144 case NONE: return true;
mcimadamore@1144 145 default: throw new AssertionError("Invalid modifier kind " + this);
mcimadamore@1144 146 }
mcimadamore@1144 147 }
mcimadamore@1144 148 }
mcimadamore@1144 149
mcimadamore@1144 150 enum ExprKind {
mcimadamore@1144 151 NONE("#L#S"),
mcimadamore@1144 152 SINGLE_PAREN1("(#L#S)"),
mcimadamore@1144 153 SINGLE_PAREN2("(#L)#S"),
mcimadamore@1144 154 DOUBLE_PAREN1("((#L#S))"),
mcimadamore@1144 155 DOUBLE_PAREN2("((#L)#S)"),
mcimadamore@1144 156 DOUBLE_PAREN3("((#L))#S");
mcimadamore@1144 157
mcimadamore@1144 158 String expressionTemplate;
mcimadamore@1144 159
mcimadamore@1144 160 ExprKind(String expressionTemplate) {
mcimadamore@1144 161 this.expressionTemplate = expressionTemplate;
mcimadamore@1144 162 }
mcimadamore@1144 163
mcimadamore@1144 164 String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2,
mcimadamore@1503 165 ModifierKind mk1, ModifierKind mk2, LambdaKind lk, LambdaParameterName pn, SubExprKind sk) {
mcimadamore@1503 166 return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2, pn))
mcimadamore@1144 167 .replaceAll("#S", sk.subExpression);
mcimadamore@1144 168 }
mcimadamore@1144 169 }
mcimadamore@1144 170
mcimadamore@1144 171 enum SubExprKind {
mcimadamore@1144 172 NONE(""),
mcimadamore@1144 173 SELECT_FIELD(".f"),
mcimadamore@1144 174 SELECT_METHOD(".f()"),
mcimadamore@1144 175 SELECT_NEW(".new Foo()"),
mcimadamore@1144 176 POSTINC("++"),
mcimadamore@1144 177 POSTDEC("--");
mcimadamore@1144 178
mcimadamore@1144 179 String subExpression;
mcimadamore@1144 180
mcimadamore@1144 181 SubExprKind(String subExpression) {
mcimadamore@1144 182 this.subExpression = subExpression;
mcimadamore@1144 183 }
mcimadamore@1144 184 }
mcimadamore@1144 185
mcimadamore@1144 186 public static void main(String... args) throws Exception {
mcimadamore@1144 187 for (LambdaKind lk : LambdaKind.values()) {
mcimadamore@1503 188 for (LambdaParameterName pn : LambdaParameterName.values()) {
mcimadamore@1503 189 for (LambdaParameterKind pk1 : LambdaParameterKind.values()) {
mcimadamore@1503 190 if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT)
vromero@1482 191 continue;
mcimadamore@1503 192 for (LambdaParameterKind pk2 : LambdaParameterKind.values()) {
mcimadamore@1503 193 if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT)
vromero@1482 194 continue;
mcimadamore@1503 195 for (ModifierKind mk1 : ModifierKind.values()) {
mcimadamore@1503 196 if (mk1 != ModifierKind.NONE && lk.isShort())
vromero@1482 197 continue;
mcimadamore@1503 198 if (lk.arity() < 1 && mk1 != ModifierKind.NONE)
mcimadamore@1503 199 continue;
mcimadamore@1503 200 for (ModifierKind mk2 : ModifierKind.values()) {
mcimadamore@1503 201 if (lk.arity() < 2 && mk2 != ModifierKind.NONE)
mcimadamore@1503 202 continue;
mcimadamore@1503 203 for (SubExprKind sk : SubExprKind.values()) {
mcimadamore@1503 204 for (ExprKind ek : ExprKind.values()) {
mcimadamore@1503 205 pool.execute(
mcimadamore@1503 206 new LambdaParserTest(pk1, pk2, mk1,
mcimadamore@1503 207 mk2, lk, sk, ek, pn));
mcimadamore@1503 208 }
mcimadamore@1144 209 }
mcimadamore@1144 210 }
mcimadamore@1144 211 }
mcimadamore@1144 212 }
mcimadamore@1144 213 }
mcimadamore@1144 214 }
mcimadamore@1144 215 }
vromero@1482 216
vromero@1482 217 checkAfterExec();
mcimadamore@1144 218 }
mcimadamore@1144 219
mcimadamore@1144 220 LambdaParameterKind pk1;
mcimadamore@1144 221 LambdaParameterKind pk2;
mcimadamore@1144 222 ModifierKind mk1;
mcimadamore@1144 223 ModifierKind mk2;
mcimadamore@1144 224 LambdaKind lk;
mcimadamore@1503 225 LambdaParameterName pn;
mcimadamore@1144 226 SubExprKind sk;
mcimadamore@1144 227 ExprKind ek;
mcimadamore@1144 228 JavaSource source;
mcimadamore@1144 229 DiagnosticChecker diagChecker;
mcimadamore@1144 230
vromero@1482 231 LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2,
vromero@1482 232 ModifierKind mk1, ModifierKind mk2, LambdaKind lk,
mcimadamore@1503 233 SubExprKind sk, ExprKind ek, LambdaParameterName pn) {
mcimadamore@1144 234 this.pk1 = pk1;
mcimadamore@1144 235 this.pk2 = pk2;
mcimadamore@1144 236 this.mk1 = mk1;
mcimadamore@1144 237 this.mk2 = mk2;
mcimadamore@1144 238 this.lk = lk;
mcimadamore@1503 239 this.pn = pn;
mcimadamore@1144 240 this.sk = sk;
mcimadamore@1144 241 this.ek = ek;
mcimadamore@1144 242 this.source = new JavaSource();
mcimadamore@1144 243 this.diagChecker = new DiagnosticChecker();
mcimadamore@1144 244 }
mcimadamore@1144 245
mcimadamore@1144 246 class JavaSource extends SimpleJavaFileObject {
mcimadamore@1144 247
mcimadamore@1144 248 String template = "class Test {\n" +
mcimadamore@1144 249 " SAM s = #E;\n" +
mcimadamore@1144 250 "}";
mcimadamore@1144 251
mcimadamore@1144 252 String source;
mcimadamore@1144 253
mcimadamore@1144 254 public JavaSource() {
mcimadamore@1144 255 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
vromero@1482 256 source = template.replaceAll("#E",
mcimadamore@1503 257 ek.expressionString(pk1, pk2, mk1, mk2, lk, pn, sk));
mcimadamore@1144 258 }
mcimadamore@1144 259
mcimadamore@1144 260 @Override
mcimadamore@1144 261 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1144 262 return source;
mcimadamore@1144 263 }
mcimadamore@1144 264 }
mcimadamore@1144 265
vromero@1482 266 public void run() {
vromero@1482 267 JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
mcimadamore@1415 268 null, null, Arrays.asList(source));
mcimadamore@1144 269 try {
mcimadamore@1144 270 ct.parse();
mcimadamore@1144 271 } catch (Throwable ex) {
vromero@1482 272 processException(ex);
vromero@1482 273 return;
mcimadamore@1144 274 }
mcimadamore@1144 275 check();
mcimadamore@1144 276 }
mcimadamore@1144 277
mcimadamore@1144 278 void check() {
vromero@1482 279 checkCount.incrementAndGet();
mcimadamore@1144 280
mcimadamore@1144 281 boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) ||
mcimadamore@1144 282 (lk.arity() > 1 && !mk2.compatibleWith(pk2));
mcimadamore@1144 283
mcimadamore@1144 284 if (lk.arity() == 2 &&
mcimadamore@1144 285 (pk1.explicit() != pk2.explicit() ||
mcimadamore@1436 286 pk1.isVarargs())) {
mcimadamore@1144 287 errorExpected = true;
mcimadamore@1144 288 }
mcimadamore@1144 289
mcimadamore@1503 290 errorExpected |= pn == LambdaParameterName.UNDERSCORE &&
mcimadamore@1503 291 lk.arity() > 0;
mcimadamore@1503 292
mcimadamore@1144 293 if (errorExpected != diagChecker.errorFound) {
mcimadamore@1144 294 throw new Error("invalid diagnostics for source:\n" +
mcimadamore@1144 295 source.getCharContent(true) +
mcimadamore@1144 296 "\nFound error: " + diagChecker.errorFound +
mcimadamore@1144 297 "\nExpected error: " + errorExpected);
mcimadamore@1144 298 }
mcimadamore@1144 299 }
mcimadamore@1144 300
vromero@1482 301 static class DiagnosticChecker
vromero@1482 302 implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@1144 303
mcimadamore@1144 304 boolean errorFound;
mcimadamore@1144 305
mcimadamore@1144 306 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@1144 307 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
mcimadamore@1144 308 errorFound = true;
mcimadamore@1144 309 }
mcimadamore@1144 310 }
mcimadamore@1144 311 }
vromero@1482 312
mcimadamore@1144 313 }

mercurial