1 /*
  2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8005166 8129962
 27  * @summary Add support for static interface methods
 28  *          Smoke test for static interface method hiding
 29  * @library /tools/javac/lib
 30  * @modules jdk.compiler/com.sun.tools.javac.api
 31  *          jdk.compiler/com.sun.tools.javac.file
 32  *          jdk.compiler/com.sun.tools.javac.util
 33  * @build combo.ComboTestHelper
 34  * @run main InterfaceMethodHidingTest
 35  */
 36 
 37 import java.io.IOException;
 38 
 39 import combo.ComboInstance;
 40 import combo.ComboParameter;
 41 import combo.ComboTask.Result;
 42 import combo.ComboTestHelper;
 43 
 44 public class InterfaceMethodHidingTest extends ComboInstance<InterfaceMethodHidingTest> {
 45 
 46     enum SignatureKind implements ComboParameter {
 47         VOID_INTEGER("void m(Integer s)", false),
 48         STRING_INTEGER("String m(Integer s)", true),
 49         VOID_STRING("void m(String s)", false),
 50         STRING_STRING("String m(String s)", true);
 51 
 52         String sigStr;
 53         boolean needsReturn;
 54 
 55         SignatureKind(String sigStr, boolean needsReturn) {
 56             this.sigStr = sigStr;
 57             this.needsReturn = needsReturn;
 58         }
 59 
 60         boolean overrideEquivalentWith(SignatureKind s2) {
 61             switch (this) {
 62                 case VOID_INTEGER:
 63                 case STRING_INTEGER:
 64                     return s2 == VOID_INTEGER || s2 == STRING_INTEGER;
 65                 case VOID_STRING:
 66                 case STRING_STRING:
 67                     return s2 == VOID_STRING || s2 == STRING_STRING;
 68                 default:
 69                     throw new AssertionError("bad signature kind");
 70             }
 71         }
 72 
 73         @Override
 74         public String expand(String optParameter) {
 75             return sigStr;
 76         }
 77     }
 78 
 79     enum MethodKind implements ComboParameter {
 80         VIRTUAL("#{SIG[#IDX]};"),
 81         STATIC("static #{SIG[#IDX]} { #{BODY[#IDX]}; #{RET.#IDX} }"),
 82         DEFAULT("default #{SIG[#IDX]} { #{BODY[#IDX]}; #{RET.#IDX} }");
 83 
 84         String methTemplate;
 85 
 86         MethodKind(String methTemplate) {
 87             this.methTemplate = methTemplate;
 88         }
 89 
 90         boolean inherithed() {
 91             return this != STATIC;
 92         }
 93 
 94         static boolean overrides(MethodKind mk1, SignatureKind sk1, MethodKind mk2, SignatureKind sk2) {
 95             return sk1 == sk2 &&
 96                     mk2.inherithed() &&
 97                     mk1 != STATIC;
 98         }
 99 
100         @Override
101         public String expand(String optParameter) {
102             return methTemplate.replaceAll("#IDX", optParameter);
103         }
104     }
105 
106     enum BodyExpr implements ComboParameter {
107         NONE(""),
108         THIS("Object o = this");
109 
110         String bodyExprStr;
111 
112         BodyExpr(String bodyExprStr) {
113             this.bodyExprStr = bodyExprStr;
114         }
115 
116         boolean allowed(MethodKind mk) {
117             return this == NONE ||
118                     mk != MethodKind.STATIC;
119         }
120 
121         @Override
122         public String expand(String optParameter) {
123             return bodyExprStr;
124         }
125     }
126 
127     public static void main(String... args) throws Exception {
128         new ComboTestHelper<InterfaceMethodHidingTest>()
129                 .withArrayDimension("SIG", (x, sig, idx) -> x.signatureKinds[idx] = sig, 3, SignatureKind.values())
130                 .withArrayDimension("BODY", (x, body, idx) -> x.bodyExprs[idx] = body, 3, BodyExpr.values())
131                 .withArrayDimension("MET", (x, meth, idx) -> x.methodKinds[idx] = meth, 3, MethodKind.values())
132                 .run(InterfaceMethodHidingTest::new);
133     }
134 
135     MethodKind[] methodKinds = new MethodKind[3];
136     SignatureKind[] signatureKinds = new SignatureKind[3];
137     BodyExpr[] bodyExprs = new BodyExpr[3];
138 
139     String template = "interface Sup {\n" +
140                           "   default void sup() { }\n" +
141                           "}\n" +
142                           "interface A extends Sup {\n" +
143                           "   #{MET[0].0}\n" +
144                           "}\n" +
145                           "interface B extends A, Sup {\n" +
146                           "   #{MET[1].1}\n" +
147                           "}\n" +
148                           "interface C extends B, Sup {\n" +
149                           "   #{MET[2].2}\n" +
150                           "}\n";
151 
152     @Override
153     public void doWork() throws IOException {
154         newCompilationTask()
155                 .withOption("-XDallowStaticInterfaceMethods")
156                 .withSourceFromTemplate(template, this::returnExpr)
157                 .analyze(this::check);
158     }
159 
160     ComboParameter returnExpr(String name) {
161         switch (name) {
162             case "RET":
163                 return optParameter -> {
164                     int idx = new Integer(optParameter);
165                     return signatureKinds[idx].needsReturn ? "return null;" : "return;";
166                 };
167             default:
168                 return null;
169         }
170     }
171 
172     void check(Result<?> res) {
173         boolean errorExpected = !bodyExprs[0].allowed(methodKinds[0]) ||
174                 !bodyExprs[1].allowed(methodKinds[1]) ||
175                 !bodyExprs[2].allowed(methodKinds[2]);
176 
177         if (methodKinds[0].inherithed()) {
178             errorExpected |= signatureKinds[1].overrideEquivalentWith(signatureKinds[0]) &&
179                     !MethodKind.overrides(methodKinds[1], signatureKinds[1], methodKinds[0], signatureKinds[0]) ||
180                     signatureKinds[2].overrideEquivalentWith(signatureKinds[0]) &&
181                     !MethodKind.overrides(methodKinds[2], signatureKinds[2], methodKinds[0], signatureKinds[0]);
182         }
183 
184         if (methodKinds[1].inherithed()) {
185             errorExpected |= signatureKinds[2].overrideEquivalentWith(signatureKinds[1]) &&
186                     !MethodKind.overrides(methodKinds[2], signatureKinds[2], methodKinds[1], signatureKinds[1]);
187         }
188 
189         if (res.hasErrors() != errorExpected) {
190             fail("Problem when compiling source:\n" + res.compilationInfo() +
191                     "\nfound error: " + res.hasErrors());
192         }
193     }
194 }