Mon, 02 Sep 2013 22:38:36 +0100
8016177: structural most specific and stuckness
Reviewed-by: jjg, vromero
Contributed-by: maurizio.cimadamore@oracle.com
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@1513 | 26 | * @bug 7192245 8005851 8005166 |
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@1513 | 57 | return Arrays.asList("-XDallowStaticInterfaceMethods", "-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 | static boolean intersect(ModifierKind mk, ModifierKind... mks) { |
mcimadamore@1366 | 81 | for (ModifierKind mk2 : mks) { |
mcimadamore@1366 | 82 | if (mk == mk2) return true; |
mcimadamore@1366 | 83 | } |
mcimadamore@1366 | 84 | return false; |
mcimadamore@1366 | 85 | } |
mcimadamore@1366 | 86 | |
mcimadamore@1366 | 87 | static boolean compatible(MethodKind mk, ModifierKind mod1, ModifierKind mod2, EnclosingKind ek) { |
mcimadamore@1366 | 88 | if (intersect(ABSTRACT, mod1, mod2) || intersect(NATIVE, mod1, mod2)) { |
mcimadamore@1366 | 89 | return mk == MethodKind.NO_BODY; |
mcimadamore@1513 | 90 | } else if (intersect(DEFAULT, mod1, mod2) || intersect(STATIC, mod1, mod2)) { |
mcimadamore@1366 | 91 | return mk == MethodKind.BODY; |
mcimadamore@1366 | 92 | } else { |
mcimadamore@1366 | 93 | return ek == EnclosingKind.INTERFACE ? |
mcimadamore@1366 | 94 | mk == MethodKind.NO_BODY : mk == MethodKind.BODY; |
mcimadamore@1366 | 95 | } |
mcimadamore@1366 | 96 | } |
mcimadamore@1366 | 97 | |
mcimadamore@1366 | 98 | boolean compatible(EnclosingKind ek) { |
mcimadamore@1366 | 99 | switch (this) { |
mcimadamore@1366 | 100 | case PRIVATE: |
mcimadamore@1366 | 101 | case PROTECTED: |
mcimadamore@1366 | 102 | return ek != EnclosingKind.INTERFACE; |
mcimadamore@1366 | 103 | default: |
mcimadamore@1366 | 104 | return true; |
mcimadamore@1366 | 105 | } |
mcimadamore@1366 | 106 | } |
mcimadamore@1366 | 107 | |
mcimadamore@1366 | 108 | static boolean compatible(ModifierKind m1, ModifierKind m2, EnclosingKind ek) { |
mcimadamore@1366 | 109 | Result res1 = allowedModifierPairs[m1.ordinal()][m2.ordinal()]; |
mcimadamore@1366 | 110 | Result res2 = allowedModifierPairs[m2.ordinal()][m1.ordinal()]; |
mcimadamore@1366 | 111 | if (res1 != res2) { |
mcimadamore@1366 | 112 | throw new AssertionError(String.format("Ill-formed table: [%s,%s] != [%s,%s]", m1, m2, m2, m1)); |
mcimadamore@1366 | 113 | } else { |
mcimadamore@1366 | 114 | return res1.compatible(ek, m1, m2); |
mcimadamore@1366 | 115 | } |
mcimadamore@1366 | 116 | } |
mcimadamore@1366 | 117 | |
mcimadamore@1366 | 118 | interface Result { |
mcimadamore@1366 | 119 | boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2); |
mcimadamore@1366 | 120 | } |
mcimadamore@1366 | 121 | |
mcimadamore@1366 | 122 | static final Result T = new Result() { |
mcimadamore@1366 | 123 | @Override |
mcimadamore@1366 | 124 | public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) { |
mcimadamore@1366 | 125 | return true; |
mcimadamore@1366 | 126 | } |
mcimadamore@1366 | 127 | }; |
mcimadamore@1366 | 128 | |
mcimadamore@1366 | 129 | static final Result F = new Result() { |
mcimadamore@1366 | 130 | @Override |
mcimadamore@1366 | 131 | public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) { |
mcimadamore@1366 | 132 | return false; |
mcimadamore@1366 | 133 | } |
mcimadamore@1366 | 134 | }; |
mcimadamore@1366 | 135 | |
mcimadamore@1366 | 136 | static final Result C = new Result() { |
mcimadamore@1366 | 137 | @Override |
mcimadamore@1366 | 138 | public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) { |
mcimadamore@1366 | 139 | return ek != EnclosingKind.INTERFACE; |
mcimadamore@1366 | 140 | } |
mcimadamore@1366 | 141 | }; |
mcimadamore@1366 | 142 | |
mcimadamore@1366 | 143 | static final Result I = new Result() { |
mcimadamore@1366 | 144 | @Override |
mcimadamore@1366 | 145 | public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) { |
mcimadamore@1366 | 146 | return ek == EnclosingKind.INTERFACE; |
mcimadamore@1366 | 147 | } |
mcimadamore@1366 | 148 | }; |
mcimadamore@1366 | 149 | |
mcimadamore@1366 | 150 | static Result[][] allowedModifierPairs = { |
mcimadamore@1366 | 151 | /* NONE PUBLIC PROTECTED PRIVATE ABSTRACT STATIC NATIVE SYNCHRONIZED FINAL STRICTFP DEFAULT */ |
mcimadamore@1513 | 152 | /* NONE */ { T , T , C , C , T , T , C , C , C , C , I }, |
mcimadamore@1513 | 153 | /* PUBLIC */ { T , F , F , F , T , T , C , C , C , C , I }, |
mcimadamore@1366 | 154 | /* PROTECTED */ { C , F , F , F , C , C , C , C , C , C , F }, |
mcimadamore@1366 | 155 | /* PRIVATE */ { C , F , F , F , F , C , C , C , C , C , F }, |
mcimadamore@1366 | 156 | /* ABSTRACT */ { T , T , C , F , F , F , F , F , F , F , F }, |
mcimadamore@1513 | 157 | /* STATIC */ { T , T , C , C , F , F , C , C , C , T , F }, |
mcimadamore@1366 | 158 | /* NATIVE */ { C , C , C , C , F , C , F , C , C , F , F }, |
mcimadamore@1512 | 159 | /* SYNCHRONIZED */ { C , C , C , C , F , C , C , F , C , C , F }, |
mcimadamore@1366 | 160 | /* FINAL */ { C , C , C , C , F , C , C , C , F , C , F }, |
mcimadamore@1513 | 161 | /* STRICTFP */ { C , C , C , C , F , T , F , C , C , F , I }, |
mcimadamore@1512 | 162 | /* DEFAULT */ { I , I , F , F , F , F , F , F , F , I , F }}; |
mcimadamore@1366 | 163 | } |
mcimadamore@1366 | 164 | |
mcimadamore@1366 | 165 | enum MethodKind { |
mcimadamore@1366 | 166 | NO_BODY("void m();"), |
mcimadamore@1366 | 167 | BODY("void m() { }"); |
mcimadamore@1366 | 168 | |
mcimadamore@1366 | 169 | String methStr; |
mcimadamore@1366 | 170 | |
mcimadamore@1366 | 171 | private MethodKind(String methStr) { |
mcimadamore@1366 | 172 | this.methStr = methStr; |
mcimadamore@1366 | 173 | } |
mcimadamore@1366 | 174 | } |
mcimadamore@1366 | 175 | |
mcimadamore@1366 | 176 | enum EnclosingKind { |
mcimadamore@1366 | 177 | ABSTRACT_CLASS("abstract class Test "), |
mcimadamore@1366 | 178 | INTERFACE("interface Test "); |
mcimadamore@1366 | 179 | |
mcimadamore@1366 | 180 | String enclStr; |
mcimadamore@1366 | 181 | |
mcimadamore@1366 | 182 | EnclosingKind(String enclStr) { |
mcimadamore@1366 | 183 | this.enclStr = enclStr; |
mcimadamore@1366 | 184 | } |
mcimadamore@1366 | 185 | } |
mcimadamore@1366 | 186 | |
mcimadamore@1366 | 187 | public static void main(String... args) throws Exception { |
mcimadamore@1366 | 188 | |
mcimadamore@1366 | 189 | //create default shared JavaCompiler - reused across multiple compilations |
mcimadamore@1366 | 190 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1366 | 191 | StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1366 | 192 | |
mcimadamore@1366 | 193 | for (VersionKind vk : VersionKind.values()) { |
mcimadamore@1366 | 194 | for (EnclosingKind ek : EnclosingKind.values()) { |
mcimadamore@1366 | 195 | for (MethodKind mk : MethodKind.values()) { |
mcimadamore@1366 | 196 | for (ModifierKind modk1 : ModifierKind.values()) { |
mcimadamore@1366 | 197 | for (ModifierKind modk2 : ModifierKind.values()) { |
mcimadamore@1366 | 198 | new TestDefaultMethodsSyntax(vk, ek, mk, modk1, modk2).run(comp, fm); |
mcimadamore@1366 | 199 | } |
mcimadamore@1366 | 200 | } |
mcimadamore@1366 | 201 | } |
mcimadamore@1366 | 202 | } |
mcimadamore@1366 | 203 | } |
mcimadamore@1366 | 204 | System.out.println("Total check executed: " + checkCount); |
mcimadamore@1366 | 205 | } |
mcimadamore@1366 | 206 | |
mcimadamore@1366 | 207 | VersionKind vk; |
mcimadamore@1366 | 208 | EnclosingKind ek; |
mcimadamore@1366 | 209 | MethodKind mk; |
mcimadamore@1366 | 210 | ModifierKind modk1, modk2; |
mcimadamore@1366 | 211 | JavaSource source; |
mcimadamore@1366 | 212 | DiagnosticChecker diagChecker; |
mcimadamore@1366 | 213 | |
mcimadamore@1366 | 214 | TestDefaultMethodsSyntax(VersionKind vk, EnclosingKind ek, MethodKind mk, ModifierKind modk1, ModifierKind modk2) { |
mcimadamore@1366 | 215 | this.vk = vk; |
mcimadamore@1366 | 216 | this.ek = ek; |
mcimadamore@1366 | 217 | this.mk = mk; |
mcimadamore@1366 | 218 | this.modk1 = modk1; |
mcimadamore@1366 | 219 | this.modk2 = modk2; |
mcimadamore@1366 | 220 | this.source = new JavaSource(); |
mcimadamore@1366 | 221 | this.diagChecker = new DiagnosticChecker(); |
mcimadamore@1366 | 222 | } |
mcimadamore@1366 | 223 | |
mcimadamore@1366 | 224 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@1366 | 225 | |
mcimadamore@1366 | 226 | String template = "#EK {\n" + |
mcimadamore@1366 | 227 | " #MOD1 #MOD2 #METH\n" + |
mcimadamore@1366 | 228 | "}\n"; |
mcimadamore@1366 | 229 | |
mcimadamore@1366 | 230 | String source; |
mcimadamore@1366 | 231 | |
mcimadamore@1366 | 232 | public JavaSource() { |
mcimadamore@1366 | 233 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@1366 | 234 | source = template.replaceAll("#EK", ek.enclStr) |
mcimadamore@1366 | 235 | .replaceAll("#MOD1", modk1.modStr) |
mcimadamore@1366 | 236 | .replaceAll("#MOD2", modk2.modStr) |
mcimadamore@1366 | 237 | .replaceAll("#METH", mk.methStr); |
mcimadamore@1366 | 238 | } |
mcimadamore@1366 | 239 | |
mcimadamore@1366 | 240 | @Override |
mcimadamore@1366 | 241 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@1366 | 242 | return source; |
mcimadamore@1366 | 243 | } |
mcimadamore@1366 | 244 | } |
mcimadamore@1366 | 245 | |
mcimadamore@1366 | 246 | void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { |
mcimadamore@1366 | 247 | JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, |
mcimadamore@1366 | 248 | vk.getOptions(), null, Arrays.asList(source)); |
mcimadamore@1366 | 249 | try { |
mcimadamore@1366 | 250 | ct.analyze(); |
mcimadamore@1366 | 251 | } catch (Throwable ex) { |
mcimadamore@1366 | 252 | throw new AssertionError("Error thrown when analyzing the following source:\n" + source.getCharContent(true)); |
mcimadamore@1366 | 253 | } |
mcimadamore@1366 | 254 | check(); |
mcimadamore@1366 | 255 | } |
mcimadamore@1366 | 256 | |
mcimadamore@1366 | 257 | void check() { |
mcimadamore@1366 | 258 | boolean errorExpected = !ModifierKind.compatible(modk1, modk2, ek); |
mcimadamore@1366 | 259 | |
mcimadamore@1366 | 260 | errorExpected |= !ModifierKind.compatible(mk, modk1, modk2, ek); |
mcimadamore@1366 | 261 | |
mcimadamore@1366 | 262 | errorExpected |= !modk1.compatible(ek) || !modk2.compatible(ek); |
mcimadamore@1366 | 263 | |
mcimadamore@1366 | 264 | errorExpected |= ModifierKind.intersect(ModifierKind.DEFAULT, modk1, modk2) && |
mcimadamore@1366 | 265 | vk == VersionKind.PRE_LAMBDA; |
mcimadamore@1366 | 266 | |
mcimadamore@1513 | 267 | errorExpected |= ModifierKind.intersect(ModifierKind.STATIC, modk1, modk2) && |
mcimadamore@1513 | 268 | ek == EnclosingKind.INTERFACE && vk == VersionKind.PRE_LAMBDA; |
mcimadamore@1513 | 269 | |
mcimadamore@1366 | 270 | checkCount++; |
mcimadamore@1366 | 271 | if (diagChecker.errorFound != errorExpected) { |
mcimadamore@1366 | 272 | throw new AssertionError("Problem when compiling source:\n" + source.getCharContent(true) + |
mcimadamore@1366 | 273 | "\nfound error: " + diagChecker.errorFound); |
mcimadamore@1366 | 274 | } |
mcimadamore@1366 | 275 | } |
mcimadamore@1366 | 276 | |
mcimadamore@1366 | 277 | static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@1366 | 278 | |
mcimadamore@1366 | 279 | boolean errorFound; |
mcimadamore@1366 | 280 | |
mcimadamore@1366 | 281 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@1366 | 282 | if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
mcimadamore@1366 | 283 | errorFound = true; |
mcimadamore@1366 | 284 | } |
mcimadamore@1366 | 285 | } |
mcimadamore@1366 | 286 | } |
mcimadamore@1366 | 287 | } |