mcimadamore@1366: /* mcimadamore@1366: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. mcimadamore@1366: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1366: * mcimadamore@1366: * This code is free software; you can redistribute it and/or modify it mcimadamore@1366: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1366: * published by the Free Software Foundation. mcimadamore@1366: * mcimadamore@1366: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1366: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1366: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1366: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1366: * accompanied this code). mcimadamore@1366: * mcimadamore@1366: * You should have received a copy of the GNU General Public License version mcimadamore@1366: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1366: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1366: * mcimadamore@1366: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1366: * or visit www.oracle.com if you need additional information or have any mcimadamore@1366: * questions. mcimadamore@1366: */ mcimadamore@1366: mcimadamore@1366: /* mcimadamore@1366: * @test mcimadamore@1513: * @bug 7192245 8005851 8005166 mcimadamore@1366: * @summary Automatic test for checking set of allowed modifiers on interface methods mcimadamore@1366: */ mcimadamore@1366: mcimadamore@1366: import com.sun.source.util.JavacTask; mcimadamore@1366: import java.net.URI; mcimadamore@1366: import java.util.Arrays; mcimadamore@1366: import java.util.List; mcimadamore@1366: import javax.tools.Diagnostic; mcimadamore@1366: import javax.tools.JavaCompiler; mcimadamore@1366: import javax.tools.JavaFileObject; mcimadamore@1366: import javax.tools.SimpleJavaFileObject; mcimadamore@1366: import javax.tools.StandardJavaFileManager; mcimadamore@1366: import javax.tools.ToolProvider; mcimadamore@1366: mcimadamore@1366: mcimadamore@1366: public class TestDefaultMethodsSyntax { mcimadamore@1366: mcimadamore@1366: static int checkCount = 0; mcimadamore@1366: mcimadamore@1366: enum VersionKind { mcimadamore@1366: PRE_LAMBDA("7"), mcimadamore@1366: LAMBDA("8"); mcimadamore@1366: mcimadamore@1366: String versionString; mcimadamore@1366: mcimadamore@1366: VersionKind(String versionString) { mcimadamore@1366: this.versionString = versionString; mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: List getOptions() { mcimadamore@1513: return Arrays.asList("-XDallowStaticInterfaceMethods", "-source", versionString); mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: enum ModifierKind { mcimadamore@1366: NONE(""), mcimadamore@1366: PUBLIC("public"), mcimadamore@1366: PROTECTED("protected"), mcimadamore@1366: PRIVATE("private"), mcimadamore@1366: ABSTRACT("abstract"), mcimadamore@1366: STATIC("static"), mcimadamore@1366: NATIVE("native"), mcimadamore@1366: SYNCHRONIZED("synchronized"), mcimadamore@1366: FINAL("final"), mcimadamore@1366: STRICTFP("strictfp"), mcimadamore@1366: DEFAULT("default"); mcimadamore@1366: mcimadamore@1366: String modStr; mcimadamore@1366: mcimadamore@1366: private ModifierKind(String modStr) { mcimadamore@1366: this.modStr = modStr; mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: static boolean intersect(ModifierKind mk, ModifierKind... mks) { mcimadamore@1366: for (ModifierKind mk2 : mks) { mcimadamore@1366: if (mk == mk2) return true; mcimadamore@1366: } mcimadamore@1366: return false; mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: static boolean compatible(MethodKind mk, ModifierKind mod1, ModifierKind mod2, EnclosingKind ek) { mcimadamore@1366: if (intersect(ABSTRACT, mod1, mod2) || intersect(NATIVE, mod1, mod2)) { mcimadamore@1366: return mk == MethodKind.NO_BODY; mcimadamore@1513: } else if (intersect(DEFAULT, mod1, mod2) || intersect(STATIC, mod1, mod2)) { mcimadamore@1366: return mk == MethodKind.BODY; mcimadamore@1366: } else { mcimadamore@1366: return ek == EnclosingKind.INTERFACE ? mcimadamore@1366: mk == MethodKind.NO_BODY : mk == MethodKind.BODY; mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: boolean compatible(EnclosingKind ek) { mcimadamore@1366: switch (this) { mcimadamore@1366: case PRIVATE: mcimadamore@1366: case PROTECTED: mcimadamore@1366: return ek != EnclosingKind.INTERFACE; mcimadamore@1366: default: mcimadamore@1366: return true; mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: static boolean compatible(ModifierKind m1, ModifierKind m2, EnclosingKind ek) { mcimadamore@1366: Result res1 = allowedModifierPairs[m1.ordinal()][m2.ordinal()]; mcimadamore@1366: Result res2 = allowedModifierPairs[m2.ordinal()][m1.ordinal()]; mcimadamore@1366: if (res1 != res2) { mcimadamore@1366: throw new AssertionError(String.format("Ill-formed table: [%s,%s] != [%s,%s]", m1, m2, m2, m1)); mcimadamore@1366: } else { mcimadamore@1366: return res1.compatible(ek, m1, m2); mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: interface Result { mcimadamore@1366: boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2); mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: static final Result T = new Result() { mcimadamore@1366: @Override mcimadamore@1366: public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) { mcimadamore@1366: return true; mcimadamore@1366: } mcimadamore@1366: }; mcimadamore@1366: mcimadamore@1366: static final Result F = new Result() { mcimadamore@1366: @Override mcimadamore@1366: public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) { mcimadamore@1366: return false; mcimadamore@1366: } mcimadamore@1366: }; mcimadamore@1366: mcimadamore@1366: static final Result C = new Result() { mcimadamore@1366: @Override mcimadamore@1366: public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) { mcimadamore@1366: return ek != EnclosingKind.INTERFACE; mcimadamore@1366: } mcimadamore@1366: }; mcimadamore@1366: mcimadamore@1366: static final Result I = new Result() { mcimadamore@1366: @Override mcimadamore@1366: public boolean compatible(EnclosingKind ek, ModifierKind m1, ModifierKind m2) { mcimadamore@1366: return ek == EnclosingKind.INTERFACE; mcimadamore@1366: } mcimadamore@1366: }; mcimadamore@1366: mcimadamore@1366: static Result[][] allowedModifierPairs = { mcimadamore@1366: /* NONE PUBLIC PROTECTED PRIVATE ABSTRACT STATIC NATIVE SYNCHRONIZED FINAL STRICTFP DEFAULT */ mcimadamore@1513: /* NONE */ { T , T , C , C , T , T , C , C , C , C , I }, mcimadamore@1513: /* PUBLIC */ { T , F , F , F , T , T , C , C , C , C , I }, mcimadamore@1366: /* PROTECTED */ { C , F , F , F , C , C , C , C , C , C , F }, mcimadamore@1366: /* PRIVATE */ { C , F , F , F , F , C , C , C , C , C , F }, mcimadamore@1366: /* ABSTRACT */ { T , T , C , F , F , F , F , F , F , F , F }, mcimadamore@1513: /* STATIC */ { T , T , C , C , F , F , C , C , C , T , F }, mcimadamore@1366: /* NATIVE */ { C , C , C , C , F , C , F , C , C , F , F }, mcimadamore@1512: /* SYNCHRONIZED */ { C , C , C , C , F , C , C , F , C , C , F }, mcimadamore@1366: /* FINAL */ { C , C , C , C , F , C , C , C , F , C , F }, mcimadamore@1513: /* STRICTFP */ { C , C , C , C , F , T , F , C , C , F , I }, mcimadamore@1512: /* DEFAULT */ { I , I , F , F , F , F , F , F , F , I , F }}; mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: enum MethodKind { mcimadamore@1366: NO_BODY("void m();"), mcimadamore@1366: BODY("void m() { }"); mcimadamore@1366: mcimadamore@1366: String methStr; mcimadamore@1366: mcimadamore@1366: private MethodKind(String methStr) { mcimadamore@1366: this.methStr = methStr; mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: enum EnclosingKind { mcimadamore@1366: ABSTRACT_CLASS("abstract class Test "), mcimadamore@1366: INTERFACE("interface Test "); mcimadamore@1366: mcimadamore@1366: String enclStr; mcimadamore@1366: mcimadamore@1366: EnclosingKind(String enclStr) { mcimadamore@1366: this.enclStr = enclStr; mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: public static void main(String... args) throws Exception { mcimadamore@1366: mcimadamore@1366: //create default shared JavaCompiler - reused across multiple compilations mcimadamore@1366: JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); mcimadamore@1366: StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); mcimadamore@1366: mcimadamore@1366: for (VersionKind vk : VersionKind.values()) { mcimadamore@1366: for (EnclosingKind ek : EnclosingKind.values()) { mcimadamore@1366: for (MethodKind mk : MethodKind.values()) { mcimadamore@1366: for (ModifierKind modk1 : ModifierKind.values()) { mcimadamore@1366: for (ModifierKind modk2 : ModifierKind.values()) { mcimadamore@1366: new TestDefaultMethodsSyntax(vk, ek, mk, modk1, modk2).run(comp, fm); mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: System.out.println("Total check executed: " + checkCount); mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: VersionKind vk; mcimadamore@1366: EnclosingKind ek; mcimadamore@1366: MethodKind mk; mcimadamore@1366: ModifierKind modk1, modk2; mcimadamore@1366: JavaSource source; mcimadamore@1366: DiagnosticChecker diagChecker; mcimadamore@1366: mcimadamore@1366: TestDefaultMethodsSyntax(VersionKind vk, EnclosingKind ek, MethodKind mk, ModifierKind modk1, ModifierKind modk2) { mcimadamore@1366: this.vk = vk; mcimadamore@1366: this.ek = ek; mcimadamore@1366: this.mk = mk; mcimadamore@1366: this.modk1 = modk1; mcimadamore@1366: this.modk2 = modk2; mcimadamore@1366: this.source = new JavaSource(); mcimadamore@1366: this.diagChecker = new DiagnosticChecker(); mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: class JavaSource extends SimpleJavaFileObject { mcimadamore@1366: mcimadamore@1366: String template = "#EK {\n" + mcimadamore@1366: " #MOD1 #MOD2 #METH\n" + mcimadamore@1366: "}\n"; mcimadamore@1366: mcimadamore@1366: String source; mcimadamore@1366: mcimadamore@1366: public JavaSource() { mcimadamore@1366: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); mcimadamore@1366: source = template.replaceAll("#EK", ek.enclStr) mcimadamore@1366: .replaceAll("#MOD1", modk1.modStr) mcimadamore@1366: .replaceAll("#MOD2", modk2.modStr) mcimadamore@1366: .replaceAll("#METH", mk.methStr); mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: @Override mcimadamore@1366: public CharSequence getCharContent(boolean ignoreEncodingErrors) { mcimadamore@1366: return source; mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { mcimadamore@1366: JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, mcimadamore@1366: vk.getOptions(), null, Arrays.asList(source)); mcimadamore@1366: try { mcimadamore@1366: ct.analyze(); mcimadamore@1366: } catch (Throwable ex) { mcimadamore@1366: throw new AssertionError("Error thrown when analyzing the following source:\n" + source.getCharContent(true)); mcimadamore@1366: } mcimadamore@1366: check(); mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: void check() { mcimadamore@1366: boolean errorExpected = !ModifierKind.compatible(modk1, modk2, ek); mcimadamore@1366: mcimadamore@1366: errorExpected |= !ModifierKind.compatible(mk, modk1, modk2, ek); mcimadamore@1366: mcimadamore@1366: errorExpected |= !modk1.compatible(ek) || !modk2.compatible(ek); mcimadamore@1366: mcimadamore@1366: errorExpected |= ModifierKind.intersect(ModifierKind.DEFAULT, modk1, modk2) && mcimadamore@1366: vk == VersionKind.PRE_LAMBDA; mcimadamore@1366: mcimadamore@1513: errorExpected |= ModifierKind.intersect(ModifierKind.STATIC, modk1, modk2) && mcimadamore@1513: ek == EnclosingKind.INTERFACE && vk == VersionKind.PRE_LAMBDA; mcimadamore@1513: mcimadamore@1366: checkCount++; mcimadamore@1366: if (diagChecker.errorFound != errorExpected) { mcimadamore@1366: throw new AssertionError("Problem when compiling source:\n" + source.getCharContent(true) + mcimadamore@1366: "\nfound error: " + diagChecker.errorFound); mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: mcimadamore@1366: static class DiagnosticChecker implements javax.tools.DiagnosticListener { mcimadamore@1366: mcimadamore@1366: boolean errorFound; mcimadamore@1366: mcimadamore@1366: public void report(Diagnostic diagnostic) { mcimadamore@1366: if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { mcimadamore@1366: errorFound = true; mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: } mcimadamore@1366: }