1 /*
  2  * Copyright (c) 2011, 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 7030606 8006694 8129962
 27  * @summary Project-coin: multi-catch types should be pairwise disjoint
 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  * @run main DisjunctiveTypeWellFormednessTest
 36  */
 37 
 38 import java.io.IOException;
 39 
 40 import combo.ComboInstance;
 41 import combo.ComboParameter;
 42 import combo.ComboTask.Result;
 43 import combo.ComboTestHelper;
 44 
 45 
 46 public class DisjunctiveTypeWellFormednessTest extends ComboInstance<DisjunctiveTypeWellFormednessTest> {
 47 
 48     enum Alternative implements ComboParameter {
 49         EXCEPTION("Exception"),
 50         RUNTIME_EXCEPTION("RuntimeException"),
 51         IO_EXCEPTION("java.io.IOException"),
 52         FILE_NOT_FOUND_EXCEPTION("java.io.FileNotFoundException"),
 53         ILLEGAL_ARGUMENT_EXCEPTION("IllegalArgumentException");
 54 
 55         String exceptionStr;
 56 
 57         Alternative(String exceptionStr) {
 58             this.exceptionStr = exceptionStr;
 59         }
 60 
 61         boolean disjoint(Alternative that) {
 62             return disjoint[this.ordinal()][that.ordinal()];
 63         }
 64 
 65         static boolean[][] disjoint = {
 66             //                              Exception    RuntimeException    IOException    FileNotFoundException    IllegalArgumentException
 67             /*Exception*/                {  false,       false,              false,         false,                   false },
 68             /*RuntimeException*/         {  false,       false,              true,          true,                    false },
 69             /*IOException*/              {  false,       true,               false,         false,                   true },
 70             /*FileNotFoundException*/    {  false,       true,               false,         false,                   true },
 71             /*IllegalArgumentException*/ {  false,       false,              true,          true,                    false }
 72         };
 73 
 74         @Override
 75         public String expand(String optParameter) {
 76             return exceptionStr;
 77         }
 78     }
 79 
 80     enum Arity implements ComboParameter {
 81         ONE(1, "#{TYPE[0]}"),
 82         TWO(2, "#{TYPE[0]} | #{TYPE[1]}"),
 83         THREE(3, "#{TYPE[0]} | #{TYPE[1]} | #{TYPE[2]}"),
 84         FOUR(4, "#{TYPE[0]} | #{TYPE[1]} | #{TYPE[2]} | #{TYPE[3]}"),
 85         FIVE(5, "#{TYPE[0]} | #{TYPE[1]} | #{TYPE[2]} | #{TYPE[3]} | #{TYPE[4]}");
 86 
 87         int n;
 88         String arityTemplate;
 89 
 90         Arity(int n, String arityTemplate) {
 91             this.n = n;
 92             this.arityTemplate = arityTemplate;
 93         }
 94 
 95         @Override
 96         public String expand(String optParameter) {
 97             return arityTemplate;
 98         }
 99     }
100 
101     public static void main(String... args) throws Exception {
102         new ComboTestHelper<DisjunctiveTypeWellFormednessTest>()
103                 .withFilter(DisjunctiveTypeWellFormednessTest::arityFilter)
104                 .withDimension("CTYPE", (x, arity) -> x.arity = arity, Arity.values())
105                 .withArrayDimension("TYPE", (x, type, idx) -> x.alternatives[idx] = type, 5, Alternative.values())
106                 .run(DisjunctiveTypeWellFormednessTest::new);
107     }
108 
109     Arity arity;
110     Alternative[] alternatives = new Alternative[5];
111 
112     boolean arityFilter() {
113         for (int i = arity.n; i < alternatives.length ; i++) {
114             if (alternatives[i].ordinal() != 0) {
115                 return false;
116             }
117         }
118         return true;
119     }
120 
121     String template = "class Test {\n" +
122                       "void test() {\n" +
123                       "try {} catch (#{CTYPE} e) {}\n" +
124                       "}\n" +
125                       "}\n";
126 
127     @Override
128     public void doWork() throws IOException {
129         newCompilationTask()
130                 .withSourceFromTemplate(template)
131                 .analyze(this::check);
132     }
133 
134     void check(Result<?> res) {
135 
136         int non_disjoint = 0;
137         for (int i = 0 ; i < arity.n ; i++) {
138             for (int j = 0 ; j < i ; j++) {
139                 if (!alternatives[i].disjoint(alternatives[j])) {
140                     non_disjoint++;
141                     break;
142                 }
143             }
144         }
145 
146         int foundErrs = res.diagnosticsForKey("compiler.err.multicatch.types.must.be.disjoint").size();
147         if (non_disjoint != foundErrs) {
148             fail("invalid diagnostics for source:\n" +
149                     res.compilationInfo() +
150                     "\nFound errors: " + foundErrs +
151                     "\nExpected errors: " + non_disjoint);
152         }
153     }
154 }
--- EOF ---