1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambda/LambdaParserTest.java Mon Nov 28 15:56:42 2011 +0000 1.3 @@ -0,0 +1,276 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 7115050 1.30 + * @summary Add parser support for lambda expressions 1.31 + */ 1.32 + 1.33 +import com.sun.source.util.JavacTask; 1.34 +import java.net.URI; 1.35 +import java.util.Arrays; 1.36 +import javax.tools.Diagnostic; 1.37 +import javax.tools.JavaCompiler; 1.38 +import javax.tools.JavaFileObject; 1.39 +import javax.tools.SimpleJavaFileObject; 1.40 +import javax.tools.StandardJavaFileManager; 1.41 +import javax.tools.ToolProvider; 1.42 + 1.43 +public class LambdaParserTest { 1.44 + 1.45 + static int checkCount = 0; 1.46 + 1.47 + enum LambdaKind { 1.48 + NILARY_EXPR("()->x"), 1.49 + NILARY_STMT("()->{ return x; }"), 1.50 + ONEARY_SHORT_EXPR("x->x"), 1.51 + ONEARY_SHORT_STMT("x->{ return x; }"), 1.52 + ONEARY_EXPR("(#M1 #T1 x)->x"), 1.53 + ONEARY_STMT("(#M1 #T1 x)->{ return x; }"), 1.54 + TWOARY_EXPR("(#M1 #T1 x, #M2 #T2 y)->x"), 1.55 + TWOARY_STMT("(#M1 #T1 x, #M2 #T2 y)->{ return x; }"); 1.56 + 1.57 + String lambdaTemplate; 1.58 + 1.59 + LambdaKind(String lambdaTemplate) { 1.60 + this.lambdaTemplate = lambdaTemplate; 1.61 + } 1.62 + 1.63 + String getLambdaString(LambdaParameterKind pk1, LambdaParameterKind pk2, 1.64 + ModifierKind mk1, ModifierKind mk2) { 1.65 + return lambdaTemplate.replaceAll("#M1", mk1.modifier) 1.66 + .replaceAll("#M2", mk2.modifier) 1.67 + .replaceAll("#T1", pk1.parameterType) 1.68 + .replaceAll("#T2", pk2.parameterType); 1.69 + } 1.70 + 1.71 + int arity() { 1.72 + switch (this) { 1.73 + case NILARY_EXPR: 1.74 + case NILARY_STMT: return 0; 1.75 + case ONEARY_SHORT_EXPR: 1.76 + case ONEARY_SHORT_STMT: 1.77 + case ONEARY_EXPR: 1.78 + case ONEARY_STMT: return 1; 1.79 + case TWOARY_EXPR: 1.80 + case TWOARY_STMT: return 2; 1.81 + default: throw new AssertionError("Invalid lambda kind " + this); 1.82 + } 1.83 + } 1.84 + 1.85 + boolean isShort() { 1.86 + return this == ONEARY_SHORT_EXPR || 1.87 + this == ONEARY_SHORT_STMT; 1.88 + } 1.89 + } 1.90 + 1.91 + enum LambdaParameterKind { 1.92 + IMPLICIT(""), 1.93 + EXPLIICT_SIMPLE("A"), 1.94 + EXPLICIT_VARARGS("A..."), 1.95 + EXPLICIT_GENERIC1("A<X>"), 1.96 + EXPLICIT_GENERIC3("A<? extends X, ? super Y>"); 1.97 + 1.98 + String parameterType; 1.99 + 1.100 + LambdaParameterKind(String parameterType) { 1.101 + this.parameterType = parameterType; 1.102 + } 1.103 + 1.104 + boolean explicit() { 1.105 + return this != IMPLICIT; 1.106 + } 1.107 + } 1.108 + 1.109 + enum ModifierKind { 1.110 + NONE(""), 1.111 + FINAL("final"), 1.112 + PUBLIC("public"); 1.113 + 1.114 + String modifier; 1.115 + 1.116 + ModifierKind(String modifier) { 1.117 + this.modifier = modifier; 1.118 + } 1.119 + 1.120 + boolean compatibleWith(LambdaParameterKind pk) { 1.121 + switch (this) { 1.122 + case PUBLIC: return false; 1.123 + case FINAL: return pk != LambdaParameterKind.IMPLICIT; 1.124 + case NONE: return true; 1.125 + default: throw new AssertionError("Invalid modifier kind " + this); 1.126 + } 1.127 + } 1.128 + } 1.129 + 1.130 + enum ExprKind { 1.131 + NONE("#L#S"), 1.132 + SINGLE_PAREN1("(#L#S)"), 1.133 + SINGLE_PAREN2("(#L)#S"), 1.134 + DOUBLE_PAREN1("((#L#S))"), 1.135 + DOUBLE_PAREN2("((#L)#S)"), 1.136 + DOUBLE_PAREN3("((#L))#S"); 1.137 + 1.138 + String expressionTemplate; 1.139 + 1.140 + ExprKind(String expressionTemplate) { 1.141 + this.expressionTemplate = expressionTemplate; 1.142 + } 1.143 + 1.144 + String expressionString(LambdaParameterKind pk1, LambdaParameterKind pk2, 1.145 + ModifierKind mk1, ModifierKind mk2, LambdaKind lk, SubExprKind sk) { 1.146 + return expressionTemplate.replaceAll("#L", lk.getLambdaString(pk1, pk2, mk1, mk2)) 1.147 + .replaceAll("#S", sk.subExpression); 1.148 + } 1.149 + } 1.150 + 1.151 + enum SubExprKind { 1.152 + NONE(""), 1.153 + SELECT_FIELD(".f"), 1.154 + SELECT_METHOD(".f()"), 1.155 + SELECT_NEW(".new Foo()"), 1.156 + POSTINC("++"), 1.157 + POSTDEC("--"); 1.158 + 1.159 + String subExpression; 1.160 + 1.161 + SubExprKind(String subExpression) { 1.162 + this.subExpression = subExpression; 1.163 + } 1.164 + } 1.165 + 1.166 + public static void main(String... args) throws Exception { 1.167 + 1.168 + //create default shared JavaCompiler - reused across multiple compilations 1.169 + JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); 1.170 + StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); 1.171 + 1.172 + for (LambdaKind lk : LambdaKind.values()) { 1.173 + for (LambdaParameterKind pk1 : LambdaParameterKind.values()) { 1.174 + if (lk.arity() < 1 && pk1 != LambdaParameterKind.IMPLICIT) continue; 1.175 + for (LambdaParameterKind pk2 : LambdaParameterKind.values()) { 1.176 + if (lk.arity() < 2 && pk2 != LambdaParameterKind.IMPLICIT) continue; 1.177 + for (ModifierKind mk1 : ModifierKind.values()) { 1.178 + if (mk1 != ModifierKind.NONE && lk.isShort()) continue; 1.179 + if (lk.arity() < 1 && mk1 != ModifierKind.NONE) continue; 1.180 + for (ModifierKind mk2 : ModifierKind.values()) { 1.181 + if (lk.arity() < 2 && mk2 != ModifierKind.NONE) continue; 1.182 + for (SubExprKind sk : SubExprKind.values()) { 1.183 + for (ExprKind ek : ExprKind.values()) { 1.184 + new LambdaParserTest(pk1, pk2, mk1, mk2, lk, sk, ek) 1.185 + .run(comp, fm); 1.186 + } 1.187 + } 1.188 + } 1.189 + } 1.190 + } 1.191 + } 1.192 + } 1.193 + System.out.println("Total check executed: " + checkCount); 1.194 + } 1.195 + 1.196 + LambdaParameterKind pk1; 1.197 + LambdaParameterKind pk2; 1.198 + ModifierKind mk1; 1.199 + ModifierKind mk2; 1.200 + LambdaKind lk; 1.201 + SubExprKind sk; 1.202 + ExprKind ek; 1.203 + JavaSource source; 1.204 + DiagnosticChecker diagChecker; 1.205 + 1.206 + LambdaParserTest(LambdaParameterKind pk1, LambdaParameterKind pk2, ModifierKind mk1, 1.207 + ModifierKind mk2, LambdaKind lk, SubExprKind sk, ExprKind ek) { 1.208 + this.pk1 = pk1; 1.209 + this.pk2 = pk2; 1.210 + this.mk1 = mk1; 1.211 + this.mk2 = mk2; 1.212 + this.lk = lk; 1.213 + this.sk = sk; 1.214 + this.ek = ek; 1.215 + this.source = new JavaSource(); 1.216 + this.diagChecker = new DiagnosticChecker(); 1.217 + } 1.218 + 1.219 + class JavaSource extends SimpleJavaFileObject { 1.220 + 1.221 + String template = "class Test {\n" + 1.222 + " SAM s = #E;\n" + 1.223 + "}"; 1.224 + 1.225 + String source; 1.226 + 1.227 + public JavaSource() { 1.228 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 1.229 + source = template.replaceAll("#E", ek.expressionString(pk1, pk2, mk1, mk2, lk, sk)); 1.230 + } 1.231 + 1.232 + @Override 1.233 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 1.234 + return source; 1.235 + } 1.236 + } 1.237 + 1.238 + void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { 1.239 + JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, 1.240 + Arrays.asList("-XDallowLambda"), null, Arrays.asList(source)); 1.241 + try { 1.242 + ct.parse(); 1.243 + } catch (Throwable ex) { 1.244 + throw new AssertionError("Error thron when parsing the following source:\n" + source.getCharContent(true)); 1.245 + } 1.246 + check(); 1.247 + } 1.248 + 1.249 + void check() { 1.250 + checkCount++; 1.251 + 1.252 + boolean errorExpected = (lk.arity() > 0 && !mk1.compatibleWith(pk1)) || 1.253 + (lk.arity() > 1 && !mk2.compatibleWith(pk2)); 1.254 + 1.255 + if (lk.arity() == 2 && 1.256 + (pk1.explicit() != pk2.explicit() || 1.257 + pk1 == LambdaParameterKind.EXPLICIT_VARARGS)) { 1.258 + errorExpected = true; 1.259 + } 1.260 + 1.261 + if (errorExpected != diagChecker.errorFound) { 1.262 + throw new Error("invalid diagnostics for source:\n" + 1.263 + source.getCharContent(true) + 1.264 + "\nFound error: " + diagChecker.errorFound + 1.265 + "\nExpected error: " + errorExpected); 1.266 + } 1.267 + } 1.268 + 1.269 + static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { 1.270 + 1.271 + boolean errorFound; 1.272 + 1.273 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 1.274 + if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { 1.275 + errorFound = true; 1.276 + } 1.277 + } 1.278 + } 1.279 +}