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 8002099 8006694 8129962
 27  * @summary Add support for intersection types in cast expression
 28  *  temporarily workaround combo tests are causing time out in several platforms
 29  * @enablePreview
 30  * @library /tools/javac/lib
 31  * @modules jdk.compiler/com.sun.tools.javac.api
 32  *          jdk.compiler/com.sun.tools.javac.file
 33  *          jdk.compiler/com.sun.tools.javac.util
 34  * @build combo.ComboTestHelper
 35 
 36  * @run main IntersectionTypeCastTest
 37  */
 38 
 39 import com.sun.tools.javac.util.List;
 40 
 41 import combo.ComboInstance;
 42 import combo.ComboParameter;
 43 import combo.ComboTask.Result;
 44 import combo.ComboTestHelper;
 45 
 46 import java.io.IOException;
 47 
 48 public class IntersectionTypeCastTest extends ComboInstance<IntersectionTypeCastTest> {
 49 
 50     interface Type extends ComboParameter {
 51         boolean subtypeOf(Type that);
 52         boolean isClass();
 53         boolean isInterface();
 54     }
 55 
 56     enum InterfaceKind implements Type {
 57         A("A", null),
 58         B("B", null),
 59         C("C", A);
 60 
 61         String typeStr;
 62         InterfaceKind superInterface;
 63 
 64         InterfaceKind(String typeStr, InterfaceKind superInterface) {
 65             this.typeStr = typeStr;
 66             this.superInterface = superInterface;
 67         }
 68 
 69         @Override
 70         public boolean subtypeOf(Type that) {
 71             return this == that || superInterface == that ||
 72                    that == ClassKind.OBJECT;
 73         }
 74 
 75         @Override
 76         public boolean isClass() {
 77             return false;
 78         }
 79 
 80         @Override
 81         public boolean isInterface() {
 82             return true;
 83         }
 84 
 85         @Override
 86         public String expand(String optParameter) {
 87             return typeStr;
 88         }
 89     }
 90 
 91     enum ClassKind implements Type {
 92         OBJECT("Object"),
 93         CA("CA", InterfaceKind.A),
 94         CB("CB", InterfaceKind.B),
 95         CAB("CAB", InterfaceKind.A, InterfaceKind.B),
 96         CC("CC", InterfaceKind.C, InterfaceKind.A),
 97         CCA("CCA", InterfaceKind.C, InterfaceKind.A),
 98         CCB("CCB", InterfaceKind.C, InterfaceKind.A, InterfaceKind.B),
 99         CCAB("CCAB", InterfaceKind.C, InterfaceKind.A, InterfaceKind.B);
100 
101         String typeStr;
102         List<InterfaceKind> superInterfaces;
103 
104         ClassKind(String typeStr, InterfaceKind... superInterfaces) {
105             this.typeStr = typeStr;
106             this.superInterfaces = List.from(superInterfaces);
107         }
108 
109         @Override
110         public boolean subtypeOf(Type that) {
111             return this == that || superInterfaces.contains(that) ||
112                     that == OBJECT;
113         }
114 
115         @Override
116         public boolean isClass() {
117             return true;
118         }
119 
120         @Override
121         public boolean isInterface() {
122             return false;
123         }
124 
125         @Override
126         public String expand(String optParameter) {
127             return typeStr;
128         }
129     }
130 
131     enum ModifierKind implements ComboParameter {
132         NONE(""),
133         FINAL("final");
134 
135         String modStr;
136 
137         ModifierKind(String modStr) {
138             this.modStr = modStr;
139         }
140 
141         @Override
142         public String expand(String optParameter) {
143             return modStr;
144         }
145     }
146 
147     enum CastKind implements ComboParameter {
148         CLASS("(#{CLAZZ#IDX})", 0),
149         INTERFACE("(#{INTF1#IDX})", 1),
150         INTERSECTION2("(#{CLAZZ#IDX} & #{INTF1#IDX})", 1),
151         INTERSECTION3("(#{CLAZZ#IDX} & #{INTF1#IDX} & #{INTF2#IDX})", 2);
152 
153         String castTemplate;
154         int interfaceBounds;
155 
156         CastKind(String castTemplate, int interfaceBounds) {
157             this.castTemplate = castTemplate;
158             this.interfaceBounds = interfaceBounds;
159         }
160 
161         @Override
162         public String expand(String optParameter) {
163             return castTemplate.replaceAll("#IDX", optParameter);
164         }
165     }
166 
167     static class CastInfo {
168         CastKind kind;
169         Type[] types;
170 
171         CastInfo(CastKind kind, Type... types) {
172             this.kind = kind;
173             this.types = types;
174         }
175 
176         boolean hasDuplicateTypes() {
177             for (int i = 0 ; i < arity() ; i++) {
178                 for (int j = 0 ; j < arity() ; j++) {
179                     if (i != j && types[i] == types[j]) {
180                         return true;
181                     }
182                 }
183             }
184             return false;
185         }
186 
187         boolean compatibleWith(ModifierKind mod, CastInfo that) {
188             for (int i = 0 ; i < arity() ; i++) {
189                 Type t1 = types[i];
190                 for (int j = 0 ; j < that.arity() ; j++) {
191                     Type t2 = that.types[j];
192                     boolean compat =
193                             t1.subtypeOf(t2) ||
194                             t2.subtypeOf(t1) ||
195                             (t1.isInterface() && t2.isInterface()) || //side-cast (1)
196                             (mod == ModifierKind.NONE &&
197                             (t1.isInterface() != t2.isInterface())); //side-cast (2)
198                     if (!compat) return false;
199                 }
200             }
201             return true;
202         }
203 
204         private int arity() {
205             return kind.interfaceBounds + 1;
206         }
207     }
208 
209     public static void main(String... args) throws Exception {
210         new ComboTestHelper<IntersectionTypeCastTest>()
211                 .withFilter(IntersectionTypeCastTest::isRedundantCast)
212                 .withFilter(IntersectionTypeCastTest::arityFilter)
213                 .withArrayDimension("CAST", (x, ck, idx) -> x.castKinds[idx] = ck, 2, CastKind.values())
214                 .withDimension("CLAZZ1", (x, ty) -> x.types1[0] = ty, ClassKind.values())
215                 .withDimension("INTF11", (x, ty) -> x.types1[1] = ty, InterfaceKind.values())
216                 .withDimension("INTF21", (x, ty) -> x.types1[2] = ty, InterfaceKind.values())
217                 .withDimension("CLAZZ2", (x, ty) -> x.types2[0] = ty, ClassKind.values())
218                 .withDimension("INTF12", (x, ty) -> x.types2[1] = ty, InterfaceKind.values())
219                 .withDimension("INTF22", (x, ty) -> x.types2[2] = ty, InterfaceKind.values())
220                 .withDimension("MOD", (x, mod) -> x.mod = mod, ModifierKind.values())
221                 .run(IntersectionTypeCastTest::new);
222     }
223 
224     boolean isRedundantCast() {
225         for (int i = 0 ; i < 2 ; i++) {
226             Type[] types = i == 0 ? types1 : types2;
227             if (castKinds[i] == CastKind.INTERFACE && types[0] != ClassKind.OBJECT) {
228                 return false;
229             }
230         }
231         return true;
232     }
233 
234     boolean arityFilter() {
235         for (int i = 0 ; i < 2 ; i++) {
236             int lastPos = castKinds[i].interfaceBounds + 1;
237             Type[] types = i == 0 ? types1 : types2;
238             for (int j = 1; j < types.length; j++) {
239                 boolean shouldBeSet = j < lastPos;
240                 if (!shouldBeSet && (types[j] != InterfaceKind.A)) {
241                     return false;
242                 }
243             }
244         }
245         return true;
246     }
247 
248     ModifierKind mod;
249     CastKind[] castKinds = new CastKind[2];
250     Type[] types1 = new Type[3];
251     Type[] types2 = new Type[3];
252 
253     @Override
254     public void doWork() throws IOException {
255         newCompilationTask()
256                 .withSourceFromTemplate(bodyTemplate)
257                 .analyze(this::check);
258     }
259 
260     String bodyTemplate = "class Test {\n" +
261                           "   void test() {\n" +
262                           "      Object o = #{CAST[0].1}#{CAST[1].2}null;\n" +
263                           "   } }\n" +
264                           "interface A { }\n" +
265                           "interface B { }\n" +
266                           "interface C extends A { }\n" +
267                           "#{MOD} class CA implements A { }\n" +
268                           "#{MOD} class CB implements B { }\n" +
269                           "#{MOD} class CAB implements A, B { }\n" +
270                           "#{MOD} class CC implements C { }\n" +
271                           "#{MOD} class CCA implements C, A { }\n" +
272                           "#{MOD} class CCB implements C, B { }\n" +
273                           "#{MOD} class CCAB implements C, A, B { }";
274 
275     void check(Result<?> res) {
276         CastInfo cast1 = new CastInfo(castKinds[0], types1);
277         CastInfo cast2 = new CastInfo(castKinds[1], types2);
278         boolean errorExpected = cast1.hasDuplicateTypes() ||
279                 cast2.hasDuplicateTypes();
280 
281         errorExpected |= !cast2.compatibleWith(mod, cast1);
282 
283         boolean errorsFound = res.hasErrors();
284         if (errorExpected != errorsFound) {
285             fail("invalid diagnostics for source:\n" +
286                 res.compilationInfo() +
287                 "\nFound error: " + errorsFound +
288                 "\nExpected error: " + errorExpected);
289         }
290     }
291 }