test/tools/javac/defaultMethods/syntax/TestDefaultMethodsSyntax.java

Wed, 17 Oct 2012 16:43:26 +0100

author
mcimadamore
date
Wed, 17 Oct 2012 16:43:26 +0100
changeset 1366
12cf6bfd8c05
child 1415
01c9d4161882
permissions
-rw-r--r--

7192245: Add parser support for default methods
Summary: Add support for 'default' keyword in modifier position
Reviewed-by: jjg

mcimadamore@1366 1 /*
mcimadamore@1366 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1366 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1366 4 *
mcimadamore@1366 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1366 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1366 7 * published by the Free Software Foundation.
mcimadamore@1366 8 *
mcimadamore@1366 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1366 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1366 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1366 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1366 13 * accompanied this code).
mcimadamore@1366 14 *
mcimadamore@1366 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1366 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1366 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1366 18 *
mcimadamore@1366 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1366 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1366 21 * questions.
mcimadamore@1366 22 */
mcimadamore@1366 23
mcimadamore@1366 24 /*
mcimadamore@1366 25 * @test
mcimadamore@1366 26 * @bug 7192245
mcimadamore@1366 27 * @summary Automatic test for checking set of allowed modifiers on interface methods
mcimadamore@1366 28 */
mcimadamore@1366 29
mcimadamore@1366 30 import com.sun.source.util.JavacTask;
mcimadamore@1366 31 import java.net.URI;
mcimadamore@1366 32 import java.util.Arrays;
mcimadamore@1366 33 import java.util.List;
mcimadamore@1366 34 import javax.tools.Diagnostic;
mcimadamore@1366 35 import javax.tools.JavaCompiler;
mcimadamore@1366 36 import javax.tools.JavaFileObject;
mcimadamore@1366 37 import javax.tools.SimpleJavaFileObject;
mcimadamore@1366 38 import javax.tools.StandardJavaFileManager;
mcimadamore@1366 39 import javax.tools.ToolProvider;
mcimadamore@1366 40
mcimadamore@1366 41
mcimadamore@1366 42 public class TestDefaultMethodsSyntax {
mcimadamore@1366 43
mcimadamore@1366 44 static int checkCount = 0;
mcimadamore@1366 45
mcimadamore@1366 46 enum VersionKind {
mcimadamore@1366 47 PRE_LAMBDA("7"),
mcimadamore@1366 48 LAMBDA("8");
mcimadamore@1366 49
mcimadamore@1366 50 String versionString;
mcimadamore@1366 51
mcimadamore@1366 52 VersionKind(String versionString) {
mcimadamore@1366 53 this.versionString = versionString;
mcimadamore@1366 54 }
mcimadamore@1366 55
mcimadamore@1366 56 List<String> getOptions() {
mcimadamore@1366 57 return Arrays.asList("-XDallowDefaultMethods", "-source", versionString);
mcimadamore@1366 58 }
mcimadamore@1366 59 }
mcimadamore@1366 60
mcimadamore@1366 61 enum ModifierKind {
mcimadamore@1366 62 NONE(""),
mcimadamore@1366 63 PUBLIC("public"),
mcimadamore@1366 64 PROTECTED("protected"),
mcimadamore@1366 65 PRIVATE("private"),
mcimadamore@1366 66 ABSTRACT("abstract"),
mcimadamore@1366 67 STATIC("static"),
mcimadamore@1366 68 NATIVE("native"),
mcimadamore@1366 69 SYNCHRONIZED("synchronized"),
mcimadamore@1366 70 FINAL("final"),
mcimadamore@1366 71 STRICTFP("strictfp"),
mcimadamore@1366 72 DEFAULT("default");
mcimadamore@1366 73
mcimadamore@1366 74 String modStr;
mcimadamore@1366 75
mcimadamore@1366 76 private ModifierKind(String modStr) {
mcimadamore@1366 77 this.modStr = modStr;
mcimadamore@1366 78 }
mcimadamore@1366 79
mcimadamore@1366 80 boolean isAllowed(EnclosingKind ek, ModifierKind otherMod) {
mcimadamore@1366 81 if (this == otherMod) return false;
mcimadamore@1366 82 switch (this) {
mcimadamore@1366 83 case NONE:
mcimadamore@1366 84 return true;
mcimadamore@1366 85 case ABSTRACT:
mcimadamore@1366 86 return otherMod != PRIVATE;
mcimadamore@1366 87 case NATIVE:
mcimadamore@1366 88 return otherMod != ABSTRACT &&
mcimadamore@1366 89 otherMod != STRICTFP;
mcimadamore@1366 90 case FINAL:
mcimadamore@1366 91 case STATIC:
mcimadamore@1366 92 case SYNCHRONIZED:
mcimadamore@1366 93 case STRICTFP:
mcimadamore@1366 94 return otherMod != ABSTRACT;
mcimadamore@1366 95 case PUBLIC:
mcimadamore@1366 96 return true;
mcimadamore@1366 97 case PROTECTED:
mcimadamore@1366 98 return ek == EnclosingKind.ABSTRACT_CLASS;
mcimadamore@1366 99 case DEFAULT:
mcimadamore@1366 100 return otherMod != ABSTRACT;
mcimadamore@1366 101 default:
mcimadamore@1366 102 return true;
mcimadamore@1366 103 }
mcimadamore@1366 104 }
mcimadamore@1366 105
mcimadamore@1366 106 static boolean intersect(ModifierKind mk, ModifierKind... mks) {
mcimadamore@1366 107 for (ModifierKind mk2 : mks) {
mcimadamore@1366 108 if (mk == mk2) return true;
mcimadamore@1366 109 }
mcimadamore@1366 110 return false;
mcimadamore@1366 111 }
mcimadamore@1366 112
mcimadamore@1366 113 static boolean compatible(MethodKind mk, ModifierKind mod1, ModifierKind mod2, EnclosingKind ek) {
mcimadamore@1366 114 if (intersect(ABSTRACT, mod1, mod2) || intersect(NATIVE, mod1, mod2)) {
mcimadamore@1366 115 return mk == MethodKind.NO_BODY;
mcimadamore@1366 116 } else if (intersect(DEFAULT, mod1, mod2)) {
mcimadamore@1366 117 return mk == MethodKind.BODY;
mcimadamore@1366 118 } else {
mcimadamore@1366 119 return ek == EnclosingKind.INTERFACE ?
mcimadamore@1366 120 mk == MethodKind.NO_BODY : mk == MethodKind.BODY;
mcimadamore@1366 121 }
mcimadamore@1366 122 }
mcimadamore@1366 123
mcimadamore@1366 124 boolean compatible(EnclosingKind ek) {
mcimadamore@1366 125 switch (this) {
mcimadamore@1366 126 case STATIC:
mcimadamore@1366 127 case PRIVATE:
mcimadamore@1366 128 case PROTECTED:
mcimadamore@1366 129 return ek != EnclosingKind.INTERFACE;
mcimadamore@1366 130 default:
mcimadamore@1366 131 return true;
mcimadamore@1366 132 }
mcimadamore@1366 133 }
mcimadamore@1366 134
mcimadamore@1366 135 static boolean compatible(ModifierKind m1, ModifierKind m2, EnclosingKind ek) {
mcimadamore@1366 136 Result res1 = allowedModifierPairs[m1.ordinal()][m2.ordinal()];
mcimadamore@1366 137 Result res2 = allowedModifierPairs[m2.ordinal()][m1.ordinal()];
mcimadamore@1366 138 if (res1 != res2) {
mcimadamore@1366 139 throw new AssertionError(String.format("Ill-formed table: [%s,%s] != [%s,%s]", m1, m2, m2, m1));
mcimadamore@1366 140 } else {
mcimadamore@1366 141 return res1.compatible(ek, m1, m2);
mcimadamore@1366 142 }
mcimadamore@1366 143 }
mcimadamore@1366 144
mcimadamore@1366 145 interface Result {
mcimadamore@1366 146 boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2);
mcimadamore@1366 147 }
mcimadamore@1366 148
mcimadamore@1366 149 static final Result T = new Result() {
mcimadamore@1366 150 @Override
mcimadamore@1366 151 public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) {
mcimadamore@1366 152 return true;
mcimadamore@1366 153 }
mcimadamore@1366 154 };
mcimadamore@1366 155
mcimadamore@1366 156 static final Result F = new Result() {
mcimadamore@1366 157 @Override
mcimadamore@1366 158 public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) {
mcimadamore@1366 159 return false;
mcimadamore@1366 160 }
mcimadamore@1366 161 };
mcimadamore@1366 162
mcimadamore@1366 163 static final Result C = new Result() {
mcimadamore@1366 164 @Override
mcimadamore@1366 165 public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) {
mcimadamore@1366 166 return ek != EnclosingKind.INTERFACE;
mcimadamore@1366 167 }
mcimadamore@1366 168 };
mcimadamore@1366 169
mcimadamore@1366 170 static final Result I = new Result() {
mcimadamore@1366 171 @Override
mcimadamore@1366 172 public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) {
mcimadamore@1366 173 return ek == EnclosingKind.INTERFACE;
mcimadamore@1366 174 }
mcimadamore@1366 175 };
mcimadamore@1366 176
mcimadamore@1366 177 static Result[][] allowedModifierPairs = {
mcimadamore@1366 178 /* NONE PUBLIC PROTECTED PRIVATE ABSTRACT STATIC NATIVE SYNCHRONIZED FINAL STRICTFP DEFAULT */
mcimadamore@1366 179 /* NONE */ { T , T , C , C , T , C , C , C , C , C , I },
mcimadamore@1366 180 /* PUBLIC */ { T , F , F , F , T , C , C , C , C , C , I },
mcimadamore@1366 181 /* PROTECTED */ { C , F , F , F , C , C , C , C , C , C , F },
mcimadamore@1366 182 /* PRIVATE */ { C , F , F , F , F , C , C , C , C , C , F },
mcimadamore@1366 183 /* ABSTRACT */ { T , T , C , F , F , F , F , F , F , F , F },
mcimadamore@1366 184 /* STATIC */ { C , C , C , C , F , F , C , C , C , C , F },
mcimadamore@1366 185 /* NATIVE */ { C , C , C , C , F , C , F , C , C , F , F },
mcimadamore@1366 186 /* SYNCHRONIZED */ { C , C , C , C , F , C , C , F , C , C , I },
mcimadamore@1366 187 /* FINAL */ { C , C , C , C , F , C , C , C , F , C , F },
mcimadamore@1366 188 /* STRICTFP */ { C , C , C , C , F , C , F , C , C , F , I },
mcimadamore@1366 189 /* DEFAULT */ { I , I , F , F , F , F , F , I , F , I , F }};
mcimadamore@1366 190 }
mcimadamore@1366 191
mcimadamore@1366 192 enum MethodKind {
mcimadamore@1366 193 NO_BODY("void m();"),
mcimadamore@1366 194 BODY("void m() { }");
mcimadamore@1366 195
mcimadamore@1366 196 String methStr;
mcimadamore@1366 197
mcimadamore@1366 198 private MethodKind(String methStr) {
mcimadamore@1366 199 this.methStr = methStr;
mcimadamore@1366 200 }
mcimadamore@1366 201 }
mcimadamore@1366 202
mcimadamore@1366 203 enum EnclosingKind {
mcimadamore@1366 204 ABSTRACT_CLASS("abstract class Test "),
mcimadamore@1366 205 INTERFACE("interface Test ");
mcimadamore@1366 206
mcimadamore@1366 207 String enclStr;
mcimadamore@1366 208
mcimadamore@1366 209 EnclosingKind(String enclStr) {
mcimadamore@1366 210 this.enclStr = enclStr;
mcimadamore@1366 211 }
mcimadamore@1366 212 }
mcimadamore@1366 213
mcimadamore@1366 214 public static void main(String... args) throws Exception {
mcimadamore@1366 215
mcimadamore@1366 216 //create default shared JavaCompiler - reused across multiple compilations
mcimadamore@1366 217 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
mcimadamore@1366 218 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
mcimadamore@1366 219
mcimadamore@1366 220 for (VersionKind vk : VersionKind.values()) {
mcimadamore@1366 221 for (EnclosingKind ek : EnclosingKind.values()) {
mcimadamore@1366 222 for (MethodKind mk : MethodKind.values()) {
mcimadamore@1366 223 for (ModifierKind modk1 : ModifierKind.values()) {
mcimadamore@1366 224 for (ModifierKind modk2 : ModifierKind.values()) {
mcimadamore@1366 225 new TestDefaultMethodsSyntax(vk, ek, mk, modk1, modk2).run(comp, fm);
mcimadamore@1366 226 }
mcimadamore@1366 227 }
mcimadamore@1366 228 }
mcimadamore@1366 229 }
mcimadamore@1366 230 }
mcimadamore@1366 231 System.out.println("Total check executed: " + checkCount);
mcimadamore@1366 232 }
mcimadamore@1366 233
mcimadamore@1366 234 VersionKind vk;
mcimadamore@1366 235 EnclosingKind ek;
mcimadamore@1366 236 MethodKind mk;
mcimadamore@1366 237 ModifierKind modk1, modk2;
mcimadamore@1366 238 JavaSource source;
mcimadamore@1366 239 DiagnosticChecker diagChecker;
mcimadamore@1366 240
mcimadamore@1366 241 TestDefaultMethodsSyntax(VersionKind vk, EnclosingKind ek, MethodKind mk, ModifierKind modk1, ModifierKind modk2) {
mcimadamore@1366 242 this.vk = vk;
mcimadamore@1366 243 this.ek = ek;
mcimadamore@1366 244 this.mk = mk;
mcimadamore@1366 245 this.modk1 = modk1;
mcimadamore@1366 246 this.modk2 = modk2;
mcimadamore@1366 247 this.source = new JavaSource();
mcimadamore@1366 248 this.diagChecker = new DiagnosticChecker();
mcimadamore@1366 249 }
mcimadamore@1366 250
mcimadamore@1366 251 class JavaSource extends SimpleJavaFileObject {
mcimadamore@1366 252
mcimadamore@1366 253 String template = "#EK {\n" +
mcimadamore@1366 254 " #MOD1 #MOD2 #METH\n" +
mcimadamore@1366 255 "}\n";
mcimadamore@1366 256
mcimadamore@1366 257 String source;
mcimadamore@1366 258
mcimadamore@1366 259 public JavaSource() {
mcimadamore@1366 260 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
mcimadamore@1366 261 source = template.replaceAll("#EK", ek.enclStr)
mcimadamore@1366 262 .replaceAll("#MOD1", modk1.modStr)
mcimadamore@1366 263 .replaceAll("#MOD2", modk2.modStr)
mcimadamore@1366 264 .replaceAll("#METH", mk.methStr);
mcimadamore@1366 265 }
mcimadamore@1366 266
mcimadamore@1366 267 @Override
mcimadamore@1366 268 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1366 269 return source;
mcimadamore@1366 270 }
mcimadamore@1366 271 }
mcimadamore@1366 272
mcimadamore@1366 273 void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
mcimadamore@1366 274 JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
mcimadamore@1366 275 vk.getOptions(), null, Arrays.asList(source));
mcimadamore@1366 276 try {
mcimadamore@1366 277 ct.analyze();
mcimadamore@1366 278 } catch (Throwable ex) {
mcimadamore@1366 279 throw new AssertionError("Error thrown when analyzing the following source:\n" + source.getCharContent(true));
mcimadamore@1366 280 }
mcimadamore@1366 281 check();
mcimadamore@1366 282 }
mcimadamore@1366 283
mcimadamore@1366 284 void check() {
mcimadamore@1366 285 boolean errorExpected = !ModifierKind.compatible(modk1, modk2, ek);
mcimadamore@1366 286
mcimadamore@1366 287 errorExpected |= !ModifierKind.compatible(mk, modk1, modk2, ek);
mcimadamore@1366 288
mcimadamore@1366 289 errorExpected |= !modk1.compatible(ek) || !modk2.compatible(ek);
mcimadamore@1366 290
mcimadamore@1366 291 errorExpected |= ModifierKind.intersect(ModifierKind.DEFAULT, modk1, modk2) &&
mcimadamore@1366 292 vk == VersionKind.PRE_LAMBDA;
mcimadamore@1366 293
mcimadamore@1366 294 checkCount++;
mcimadamore@1366 295 if (diagChecker.errorFound != errorExpected) {
mcimadamore@1366 296 throw new AssertionError("Problem when compiling source:\n" + source.getCharContent(true) +
mcimadamore@1366 297 "\nfound error: " + diagChecker.errorFound);
mcimadamore@1366 298 }
mcimadamore@1366 299 }
mcimadamore@1366 300
mcimadamore@1366 301 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@1366 302
mcimadamore@1366 303 boolean errorFound;
mcimadamore@1366 304
mcimadamore@1366 305 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@1366 306 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
mcimadamore@1366 307 errorFound = true;
mcimadamore@1366 308 }
mcimadamore@1366 309 }
mcimadamore@1366 310 }
mcimadamore@1366 311 }

mercurial