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