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

mcimadamore@1144 1 /*
mcimadamore@1144 2 * Copyright (c) 2011, 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
mcimadamore@1144 30 */
mcimadamore@1144 31
mcimadamore@1144 32 import com.sun.source.util.JavacTask;
mcimadamore@1144 33 import java.net.URI;
mcimadamore@1144 34 import java.util.Arrays;
mcimadamore@1144 35 import javax.tools.Diagnostic;
mcimadamore@1144 36 import javax.tools.JavaCompiler;
mcimadamore@1144 37 import javax.tools.JavaFileObject;
mcimadamore@1144 38 import javax.tools.SimpleJavaFileObject;
mcimadamore@1144 39 import javax.tools.StandardJavaFileManager;
mcimadamore@1144 40 import javax.tools.ToolProvider;
mcimadamore@1144 41
mcimadamore@1144 42 public class LambdaParserTest {
mcimadamore@1144 43
mcimadamore@1144 44 static int checkCount = 0;
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@1144 93 EXPLICIT_VARARGS("A..."),
mcimadamore@1144 94 EXPLICIT_GENERIC1("A<X>"),
mcimadamore@1144 95 EXPLICIT_GENERIC3("A<? extends X, ? super Y>");
mcimadamore@1144 96
mcimadamore@1144 97 String parameterType;
mcimadamore@1144 98
mcimadamore@1144 99 LambdaParameterKind(String parameterType) {
mcimadamore@1144 100 this.parameterType = parameterType;
mcimadamore@1144 101 }
mcimadamore@1144 102
mcimadamore@1144 103 boolean explicit() {
mcimadamore@1144 104 return this != IMPLICIT;
mcimadamore@1144 105 }
mcimadamore@1144 106 }
mcimadamore@1144 107
mcimadamore@1144 108 enum ModifierKind {
mcimadamore@1144 109 NONE(""),
mcimadamore@1144 110 FINAL("final"),
mcimadamore@1144 111 PUBLIC("public");
mcimadamore@1144 112
mcimadamore@1144 113 String modifier;
mcimadamore@1144 114
mcimadamore@1144 115 ModifierKind(String modifier) {
mcimadamore@1144 116 this.modifier = modifier;
mcimadamore@1144 117 }
mcimadamore@1144 118
mcimadamore@1144 119 boolean compatibleWith(LambdaParameterKind pk) {
mcimadamore@1144 120 switch (this) {
mcimadamore@1144 121 case PUBLIC: return false;
mcimadamore@1144 122 case FINAL: return pk != LambdaParameterKind.IMPLICIT;
mcimadamore@1144 123 case NONE: return true;
mcimadamore@1144 124 default: throw new AssertionError("Invalid modifier kind " + this);
mcimadamore@1144 125 }
mcimadamore@1144 126 }
mcimadamore@1144 127 }
mcimadamore@1144 128
mcimadamore@1144 129 enum ExprKind {
mcimadamore@1144 130 NONE("#L#S"),
mcimadamore@1144 131 SINGLE_PAREN1("(#L#S)"),
mcimadamore@1144 132 SINGLE_PAREN2("(#L)#S"),
mcimadamore@1144 133 DOUBLE_PAREN1("((#L#S))"),
mcimadamore@1144 134 DOUBLE_PAREN2("((#L)#S)"),
mcimadamore@1144 135 DOUBLE_PAREN3("((#L))#S");
mcimadamore@1144 136
mcimadamore@1144 137 String expressionTemplate;
mcimadamore@1144 138
mcimadamore@1144 139 ExprKind(String expressionTemplate) {
mcimadamore@1144 140 this.expressionTemplate = expressionTemplate;
mcimadamore@1144 141 }
mcimadamore@1144 142
mcimadamore@1144 143 String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2,
mcimadamore@1144 144 ModifierKind mk1, ModifierKind mk2, LambdaKind lk, SubExprKind sk) {
mcimadamore@1144 145 return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2))
mcimadamore@1144 146 .replaceAll("#S", sk.subExpression);
mcimadamore@1144 147 }
mcimadamore@1144 148 }
mcimadamore@1144 149
mcimadamore@1144 150 enum SubExprKind {
mcimadamore@1144 151 NONE(""),
mcimadamore@1144 152 SELECT_FIELD(".f"),
mcimadamore@1144 153 SELECT_METHOD(".f()"),
mcimadamore@1144 154 SELECT_NEW(".new Foo()"),
mcimadamore@1144 155 POSTINC("++"),
mcimadamore@1144 156 POSTDEC("--");
mcimadamore@1144 157
mcimadamore@1144 158 String subExpression;
mcimadamore@1144 159
mcimadamore@1144 160 SubExprKind(String subExpression) {
mcimadamore@1144 161 this.subExpression = subExpression;
mcimadamore@1144 162 }
mcimadamore@1144 163 }
mcimadamore@1144 164
mcimadamore@1144 165 public static void main(String... args) throws Exception {
mcimadamore@1144 166
mcimadamore@1144 167 //create default shared JavaCompiler - reused across multiple compilations
mcimadamore@1144 168 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
mcimadamore@1144 169 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
mcimadamore@1144 170
mcimadamore@1144 171 for (LambdaKind lk : LambdaKind.values()) {
mcimadamore@1144 172 for (LambdaParameterKind pk1 : LambdaParameterKind.values()) {
mcimadamore@1144 173 if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT) continue;
mcimadamore@1144 174 for (LambdaParameterKind pk2 : LambdaParameterKind.values()) {
mcimadamore@1144 175 if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT) continue;
mcimadamore@1144 176 for (ModifierKind mk1 : ModifierKind.values()) {
mcimadamore@1144 177 if (mk1 != ModifierKind.NONE && lk.isShort()) continue;
mcimadamore@1144 178 if (lk.arity() < 1 && mk1 != ModifierKind.NONE) continue;
mcimadamore@1144 179 for (ModifierKind mk2 : ModifierKind.values()) {
mcimadamore@1144 180 if (lk.arity() < 2 && mk2 != ModifierKind.NONE) continue;
mcimadamore@1144 181 for (SubExprKind sk : SubExprKind.values()) {
mcimadamore@1144 182 for (ExprKind ek : ExprKind.values()) {
mcimadamore@1144 183 new LambdaParserTest(pk1, pk2, mk1, mk2, lk, sk, ek)
mcimadamore@1144 184 .run(comp, fm);
mcimadamore@1144 185 }
mcimadamore@1144 186 }
mcimadamore@1144 187 }
mcimadamore@1144 188 }
mcimadamore@1144 189 }
mcimadamore@1144 190 }
mcimadamore@1144 191 }
mcimadamore@1144 192 System.out.println("Total check executed: " + checkCount);
mcimadamore@1144 193 }
mcimadamore@1144 194
mcimadamore@1144 195 LambdaParameterKind pk1;
mcimadamore@1144 196 LambdaParameterKind pk2;
mcimadamore@1144 197 ModifierKind mk1;
mcimadamore@1144 198 ModifierKind mk2;
mcimadamore@1144 199 LambdaKind lk;
mcimadamore@1144 200 SubExprKind sk;
mcimadamore@1144 201 ExprKind ek;
mcimadamore@1144 202 JavaSource source;
mcimadamore@1144 203 DiagnosticChecker diagChecker;
mcimadamore@1144 204
mcimadamore@1144 205 LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2, ModifierKind mk1,
mcimadamore@1144 206 ModifierKind mk2, LambdaKind lk, SubExprKind sk, ExprKind ek) {
mcimadamore@1144 207 this.pk1 = pk1;
mcimadamore@1144 208 this.pk2 = pk2;
mcimadamore@1144 209 this.mk1 = mk1;
mcimadamore@1144 210 this.mk2 = mk2;
mcimadamore@1144 211 this.lk = lk;
mcimadamore@1144 212 this.sk = sk;
mcimadamore@1144 213 this.ek = ek;
mcimadamore@1144 214 this.source = new JavaSource();
mcimadamore@1144 215 this.diagChecker = new DiagnosticChecker();
mcimadamore@1144 216 }
mcimadamore@1144 217
mcimadamore@1144 218 class JavaSource extends SimpleJavaFileObject {
mcimadamore@1144 219
mcimadamore@1144 220 String template = "class Test {\n" +
mcimadamore@1144 221 " SAM s = #E;\n" +
mcimadamore@1144 222 "}";
mcimadamore@1144 223
mcimadamore@1144 224 String source;
mcimadamore@1144 225
mcimadamore@1144 226 public JavaSource() {
mcimadamore@1144 227 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
mcimadamore@1144 228 source = template.replaceAll("#E", ek.expressionString(pk1, pk2, mk1, mk2, lk, sk));
mcimadamore@1144 229 }
mcimadamore@1144 230
mcimadamore@1144 231 @Override
mcimadamore@1144 232 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1144 233 return source;
mcimadamore@1144 234 }
mcimadamore@1144 235 }
mcimadamore@1144 236
mcimadamore@1144 237 void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
mcimadamore@1144 238 JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
mcimadamore@1415 239 null, null, Arrays.asList(source));
mcimadamore@1144 240 try {
mcimadamore@1144 241 ct.parse();
mcimadamore@1144 242 } catch (Throwable ex) {
jjg@1169 243 throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
mcimadamore@1144 244 }
mcimadamore@1144 245 check();
mcimadamore@1144 246 }
mcimadamore@1144 247
mcimadamore@1144 248 void check() {
mcimadamore@1144 249 checkCount++;
mcimadamore@1144 250
mcimadamore@1144 251 boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) ||
mcimadamore@1144 252 (lk.arity() > 1 && !mk2.compatibleWith(pk2));
mcimadamore@1144 253
mcimadamore@1144 254 if (lk.arity() == 2 &&
mcimadamore@1144 255 (pk1.explicit() != pk2.explicit() ||
mcimadamore@1144 256 pk1 == LambdaParameterKind.EXPLICIT_VARARGS)) {
mcimadamore@1144 257 errorExpected = true;
mcimadamore@1144 258 }
mcimadamore@1144 259
mcimadamore@1144 260 if (errorExpected != diagChecker.errorFound) {
mcimadamore@1144 261 throw new Error("invalid diagnostics for source:\n" +
mcimadamore@1144 262 source.getCharContent(true) +
mcimadamore@1144 263 "\nFound error: " + diagChecker.errorFound +
mcimadamore@1144 264 "\nExpected error: " + errorExpected);
mcimadamore@1144 265 }
mcimadamore@1144 266 }
mcimadamore@1144 267
mcimadamore@1144 268 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@1144 269
mcimadamore@1144 270 boolean errorFound;
mcimadamore@1144 271
mcimadamore@1144 272 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@1144 273 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
mcimadamore@1144 274 errorFound = true;
mcimadamore@1144 275 }
mcimadamore@1144 276 }
mcimadamore@1144 277 }
mcimadamore@1144 278 }

mercurial