mcimadamore@1513: /* mcimadamore@1513: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. mcimadamore@1513: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1513: * mcimadamore@1513: * This code is free software; you can redistribute it and/or modify it mcimadamore@1513: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1513: * published by the Free Software Foundation. mcimadamore@1513: * mcimadamore@1513: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1513: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1513: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1513: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1513: * accompanied this code). mcimadamore@1513: * mcimadamore@1513: * You should have received a copy of the GNU General Public License version mcimadamore@1513: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1513: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1513: * mcimadamore@1513: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1513: * or visit www.oracle.com if you need additional information or have any mcimadamore@1513: * questions. mcimadamore@1513: */ mcimadamore@1513: mcimadamore@1513: /* mcimadamore@1513: * @test mcimadamore@1513: * @bug 8005166 mcimadamore@1513: * @summary Add support for static interface methods mcimadamore@1513: * Smoke test for static interface method hiding mcimadamore@1513: */ mcimadamore@1513: mcimadamore@1513: import com.sun.source.util.JavacTask; mcimadamore@1513: import java.net.URI; mcimadamore@1513: import java.util.Arrays; mcimadamore@1513: import javax.tools.Diagnostic; mcimadamore@1513: import javax.tools.JavaCompiler; mcimadamore@1513: import javax.tools.JavaFileObject; mcimadamore@1513: import javax.tools.SimpleJavaFileObject; mcimadamore@1513: import javax.tools.StandardJavaFileManager; mcimadamore@1513: import javax.tools.ToolProvider; mcimadamore@1513: mcimadamore@1513: mcimadamore@1513: public class InterfaceMethodHidingTest { mcimadamore@1513: mcimadamore@1513: static int checkCount = 0; mcimadamore@1513: mcimadamore@1513: enum SignatureKind { mcimadamore@1513: VOID_INTEGER("void m(Integer s)", "return;"), mcimadamore@1513: STRING_INTEGER("String m(Integer s)", "return null;"), mcimadamore@1513: VOID_STRING("void m(String s)", "return;"), mcimadamore@1513: STRING_STRING("String m(String s)", "return null;"); mcimadamore@1513: mcimadamore@1513: String sigStr; mcimadamore@1513: String retStr; mcimadamore@1513: mcimadamore@1513: SignatureKind(String sigStr, String retStr) { mcimadamore@1513: this.sigStr = sigStr; mcimadamore@1513: this.retStr = retStr; mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: boolean overrideEquivalentWith(SignatureKind s2) { mcimadamore@1513: switch (this) { mcimadamore@1513: case VOID_INTEGER: mcimadamore@1513: case STRING_INTEGER: mcimadamore@1513: return s2 == VOID_INTEGER || s2 == STRING_INTEGER; mcimadamore@1513: case VOID_STRING: mcimadamore@1513: case STRING_STRING: mcimadamore@1513: return s2 == VOID_STRING || s2 == STRING_STRING; mcimadamore@1513: default: mcimadamore@1513: throw new AssertionError("bad signature kind"); mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: enum MethodKind { mcimadamore@1513: VIRTUAL("", "#M #S;"), mcimadamore@1513: STATIC("static", "#M #S { #BE; #R }"), mcimadamore@1513: DEFAULT("default", "#M #S { #BE; #R }"); mcimadamore@1513: mcimadamore@1513: String modStr; mcimadamore@1513: String methTemplate; mcimadamore@1513: mcimadamore@1513: MethodKind(String modStr, String methTemplate) { mcimadamore@1513: this.modStr = modStr; mcimadamore@1513: this.methTemplate = methTemplate; mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: boolean inherithed() { mcimadamore@1513: return this != STATIC; mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: static boolean overrides(MethodKind mk1, SignatureKind sk1, MethodKind mk2, SignatureKind sk2) { mcimadamore@1513: return sk1 == sk2 && mcimadamore@1513: mk2.inherithed() && mcimadamore@1513: mk1 != STATIC; mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: String getBody(BodyExpr be, SignatureKind sk) { mcimadamore@1513: return methTemplate.replaceAll("#BE", be.bodyExprStr) mcimadamore@1513: .replaceAll("#R", sk.retStr) mcimadamore@1513: .replaceAll("#M", modStr) mcimadamore@1513: .replaceAll("#S", sk.sigStr); mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: enum BodyExpr { mcimadamore@1513: NONE(""), mcimadamore@1513: THIS("Object o = this"); mcimadamore@1513: mcimadamore@1513: String bodyExprStr; mcimadamore@1513: mcimadamore@1513: BodyExpr(String bodyExprStr) { mcimadamore@1513: this.bodyExprStr = bodyExprStr; mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: boolean allowed(MethodKind mk) { mcimadamore@1513: return this == NONE || mcimadamore@1513: mk != MethodKind.STATIC; mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: public static void main(String... args) throws Exception { mcimadamore@1513: mcimadamore@1513: //create default shared JavaCompiler - reused across multiple compilations mcimadamore@1513: JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); mcimadamore@1513: StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); mcimadamore@1513: mcimadamore@1513: for (MethodKind mk1 : MethodKind.values()) { mcimadamore@1513: for (SignatureKind sk1 : SignatureKind.values()) { mcimadamore@1513: for (BodyExpr be1 : BodyExpr.values()) { mcimadamore@1513: for (MethodKind mk2 : MethodKind.values()) { mcimadamore@1513: for (SignatureKind sk2 : SignatureKind.values()) { mcimadamore@1513: for (BodyExpr be2 : BodyExpr.values()) { mcimadamore@1513: for (MethodKind mk3 : MethodKind.values()) { mcimadamore@1513: for (SignatureKind sk3 : SignatureKind.values()) { mcimadamore@1513: for (BodyExpr be3 : BodyExpr.values()) { mcimadamore@1513: new InterfaceMethodHidingTest(mk1, mk2, mk3, sk1, sk2, sk3, be1, be2, be3).run(comp, fm); mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: System.out.println("Total check executed: " + checkCount); mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: MethodKind mk1, mk2, mk3; mcimadamore@1513: SignatureKind sk1, sk2, sk3; mcimadamore@1513: BodyExpr be1, be2, be3; mcimadamore@1513: JavaSource source; mcimadamore@1513: DiagnosticChecker diagChecker; mcimadamore@1513: mcimadamore@1513: InterfaceMethodHidingTest(MethodKind mk1, MethodKind mk2, MethodKind mk3, mcimadamore@1513: SignatureKind sk1, SignatureKind sk2, SignatureKind sk3, BodyExpr be1, BodyExpr be2, BodyExpr be3) { mcimadamore@1513: this.mk1 = mk1; mcimadamore@1513: this.mk2 = mk2; mcimadamore@1513: this.mk3 = mk3; mcimadamore@1513: this.sk1 = sk1; mcimadamore@1513: this.sk2 = sk2; mcimadamore@1513: this.sk3 = sk3; mcimadamore@1513: this.be1 = be1; mcimadamore@1513: this.be2 = be2; mcimadamore@1513: this.be3 = be3; mcimadamore@1513: this.source = new JavaSource(); mcimadamore@1513: this.diagChecker = new DiagnosticChecker(); mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: class JavaSource extends SimpleJavaFileObject { mcimadamore@1513: mcimadamore@1513: String template = "interface Sup {\n" + mcimadamore@1513: " default void sup() { }\n" + mcimadamore@1513: "}\n" + mcimadamore@1513: "interface A extends Sup {\n" + mcimadamore@1513: " #M1\n" + mcimadamore@1513: "}\n" + mcimadamore@1513: "interface B extends A, Sup {\n" + mcimadamore@1513: " #M2\n" + mcimadamore@1513: "}\n" + mcimadamore@1513: "interface C extends B, Sup {\n" + mcimadamore@1513: " #M3\n" + mcimadamore@1513: "}\n"; mcimadamore@1513: mcimadamore@1513: String source; mcimadamore@1513: mcimadamore@1513: public JavaSource() { mcimadamore@1513: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); mcimadamore@1513: source = template.replaceAll("#M1", mk1.getBody(be1, sk1)) mcimadamore@1513: .replaceAll("#M2", mk2.getBody(be2, sk2)) mcimadamore@1513: .replaceAll("#M3", mk3.getBody(be3, sk3)); mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: @Override mcimadamore@1513: public CharSequence getCharContent(boolean ignoreEncodingErrors) { mcimadamore@1513: return source; mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { mcimadamore@1513: JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, mcimadamore@1513: Arrays.asList("-XDallowStaticInterfaceMethods"), null, Arrays.asList(source)); mcimadamore@1513: try { mcimadamore@1513: ct.analyze(); mcimadamore@1513: } catch (Throwable ex) { mcimadamore@1513: throw new AssertionError("Error thrown when analyzing the following source:\n" + source.getCharContent(true)); mcimadamore@1513: } mcimadamore@1513: check(); mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: void check() { mcimadamore@1513: boolean errorExpected = mcimadamore@1513: !be1.allowed(mk1) || !be2.allowed(mk2) || !be3.allowed(mk3); mcimadamore@1513: mcimadamore@1513: if (mk1.inherithed()) { mcimadamore@1513: errorExpected |= mcimadamore@1513: sk2.overrideEquivalentWith(sk1) && !MethodKind.overrides(mk2, sk2, mk1, sk1) || mcimadamore@1513: sk3.overrideEquivalentWith(sk1) && !MethodKind.overrides(mk3, sk3, mk1, sk1); mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: if (mk2.inherithed()) { mcimadamore@1513: errorExpected |= mcimadamore@1513: sk3.overrideEquivalentWith(sk2) && !MethodKind.overrides(mk3, sk3, mk2, sk2); mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: checkCount++; mcimadamore@1513: if (diagChecker.errorFound != errorExpected) { mcimadamore@1513: throw new AssertionError("Problem when compiling source:\n" + source.getCharContent(true) + mcimadamore@1513: "\nfound error: " + diagChecker.errorFound); mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: mcimadamore@1513: static class DiagnosticChecker implements javax.tools.DiagnosticListener { mcimadamore@1513: mcimadamore@1513: boolean errorFound; mcimadamore@1513: mcimadamore@1513: public void report(Diagnostic diagnostic) { mcimadamore@1513: if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { mcimadamore@1513: errorFound = true; mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: } mcimadamore@1513: }