Mon, 17 Nov 2014 12:40:43 -0800
Added tag jdk8u31-b09 for changeset 26c46688ce4a
mcimadamore@949 | 1 | /* |
vromero@1482 | 2 | * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@949 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@949 | 4 | * |
mcimadamore@949 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@949 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@949 | 7 | * published by the Free Software Foundation. |
mcimadamore@949 | 8 | * |
mcimadamore@949 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@949 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@949 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@949 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@949 | 13 | * accompanied this code). |
mcimadamore@949 | 14 | * |
mcimadamore@949 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@949 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@949 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@949 | 18 | * |
mcimadamore@949 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@949 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@949 | 21 | * questions. |
mcimadamore@949 | 22 | */ |
mcimadamore@949 | 23 | |
mcimadamore@949 | 24 | /* |
mcimadamore@949 | 25 | * @test |
vromero@1520 | 26 | * @bug 7030606 8006694 |
mcimadamore@949 | 27 | * @summary Project-coin: multi-catch types should be pairwise disjoint |
vromero@1520 | 28 | * temporarily workaround combo tests are causing time out in several platforms |
vromero@1482 | 29 | * @library ../../lib |
vromero@1482 | 30 | * @build JavacTestingAbstractThreadedTest |
vromero@1520 | 31 | * @run main/othervm DisjunctiveTypeWellFormednessTest |
mcimadamore@949 | 32 | */ |
mcimadamore@949 | 33 | |
vromero@1520 | 34 | // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) |
vromero@1520 | 35 | // see JDK-8006746 |
vromero@1520 | 36 | |
mcimadamore@949 | 37 | import java.net.URI; |
mcimadamore@949 | 38 | import java.util.Arrays; |
mcimadamore@949 | 39 | import javax.tools.Diagnostic; |
mcimadamore@949 | 40 | import javax.tools.JavaFileObject; |
mcimadamore@949 | 41 | import javax.tools.SimpleJavaFileObject; |
vromero@1482 | 42 | import com.sun.source.util.JavacTask; |
mcimadamore@949 | 43 | |
vromero@1482 | 44 | public class DisjunctiveTypeWellFormednessTest |
vromero@1482 | 45 | extends JavacTestingAbstractThreadedTest |
vromero@1482 | 46 | implements Runnable { |
mcimadamore@949 | 47 | |
mcimadamore@949 | 48 | enum Alternative { |
mcimadamore@949 | 49 | EXCEPTION("Exception"), |
mcimadamore@949 | 50 | RUNTIME_EXCEPTION("RuntimeException"), |
mcimadamore@949 | 51 | IO_EXCEPTION("java.io.IOException"), |
mcimadamore@949 | 52 | FILE_NOT_FOUND_EXCEPTION("java.io.FileNotFoundException"), |
mcimadamore@949 | 53 | ILLEGAL_ARGUMENT_EXCEPTION("IllegalArgumentException"); |
mcimadamore@949 | 54 | |
mcimadamore@949 | 55 | String exceptionStr; |
mcimadamore@949 | 56 | |
mcimadamore@949 | 57 | private Alternative(String exceptionStr) { |
mcimadamore@949 | 58 | this.exceptionStr = exceptionStr; |
mcimadamore@949 | 59 | } |
mcimadamore@949 | 60 | |
mcimadamore@949 | 61 | static String makeDisjunctiveType(Alternative... alternatives) { |
mcimadamore@949 | 62 | StringBuilder buf = new StringBuilder(); |
mcimadamore@949 | 63 | String sep = ""; |
mcimadamore@949 | 64 | for (Alternative alternative : alternatives) { |
mcimadamore@949 | 65 | buf.append(sep); |
mcimadamore@949 | 66 | buf.append(alternative.exceptionStr); |
mcimadamore@949 | 67 | sep = "|"; |
mcimadamore@949 | 68 | } |
mcimadamore@949 | 69 | return buf.toString(); |
mcimadamore@949 | 70 | } |
mcimadamore@949 | 71 | |
mcimadamore@949 | 72 | boolean disjoint(Alternative that) { |
mcimadamore@949 | 73 | return disjoint[this.ordinal()][that.ordinal()]; |
mcimadamore@949 | 74 | } |
mcimadamore@949 | 75 | |
mcimadamore@949 | 76 | static boolean[][] disjoint = { |
mcimadamore@949 | 77 | // Exception RuntimeException IOException FileNotFoundException IllegalArgumentException |
mcimadamore@949 | 78 | /*Exception*/ { false, false, false, false, false }, |
mcimadamore@949 | 79 | /*RuntimeException*/ { false, false, true, true, false }, |
mcimadamore@949 | 80 | /*IOException*/ { false, true, false, false, true }, |
mcimadamore@949 | 81 | /*FileNotFoundException*/ { false, true, false, false, true }, |
mcimadamore@949 | 82 | /*IllegalArgumentException*/ { false, false, true, true, false } |
mcimadamore@949 | 83 | }; |
mcimadamore@949 | 84 | } |
mcimadamore@949 | 85 | |
mcimadamore@949 | 86 | enum Arity { |
mcimadamore@949 | 87 | ONE(1), |
mcimadamore@949 | 88 | TWO(2), |
mcimadamore@949 | 89 | THREE(3), |
mcimadamore@949 | 90 | FOUR(4), |
mcimadamore@949 | 91 | FIVE(5); |
mcimadamore@949 | 92 | |
mcimadamore@949 | 93 | int n; |
mcimadamore@949 | 94 | |
mcimadamore@949 | 95 | private Arity(int n) { |
mcimadamore@949 | 96 | this.n = n; |
mcimadamore@949 | 97 | } |
mcimadamore@949 | 98 | } |
mcimadamore@949 | 99 | |
mcimadamore@949 | 100 | public static void main(String... args) throws Exception { |
mcimadamore@949 | 101 | for (Arity arity : Arity.values()) { |
mcimadamore@949 | 102 | for (Alternative a1 : Alternative.values()) { |
mcimadamore@949 | 103 | if (arity == Arity.ONE) { |
vromero@1482 | 104 | pool.execute(new DisjunctiveTypeWellFormednessTest(a1)); |
mcimadamore@949 | 105 | continue; |
mcimadamore@949 | 106 | } |
mcimadamore@949 | 107 | for (Alternative a2 : Alternative.values()) { |
mcimadamore@949 | 108 | if (arity == Arity.TWO) { |
vromero@1482 | 109 | pool.execute(new DisjunctiveTypeWellFormednessTest(a1, a2)); |
mcimadamore@949 | 110 | continue; |
mcimadamore@949 | 111 | } |
mcimadamore@949 | 112 | for (Alternative a3 : Alternative.values()) { |
mcimadamore@949 | 113 | if (arity == Arity.THREE) { |
vromero@1482 | 114 | pool.execute(new DisjunctiveTypeWellFormednessTest(a1, a2, a3)); |
mcimadamore@949 | 115 | continue; |
mcimadamore@949 | 116 | } |
mcimadamore@949 | 117 | for (Alternative a4 : Alternative.values()) { |
mcimadamore@949 | 118 | if (arity == Arity.FOUR) { |
vromero@1482 | 119 | pool.execute(new DisjunctiveTypeWellFormednessTest(a1, a2, a3, a4)); |
mcimadamore@949 | 120 | continue; |
mcimadamore@949 | 121 | } |
mcimadamore@949 | 122 | for (Alternative a5 : Alternative.values()) { |
vromero@1482 | 123 | pool.execute(new DisjunctiveTypeWellFormednessTest(a1, a2, a3, a4, a5)); |
mcimadamore@949 | 124 | } |
mcimadamore@949 | 125 | } |
mcimadamore@949 | 126 | } |
mcimadamore@949 | 127 | } |
mcimadamore@949 | 128 | } |
mcimadamore@949 | 129 | } |
vromero@1482 | 130 | |
vromero@1482 | 131 | checkAfterExec(false); |
mcimadamore@949 | 132 | } |
mcimadamore@949 | 133 | |
mcimadamore@949 | 134 | Alternative[] alternatives; |
mcimadamore@949 | 135 | JavaSource source; |
mcimadamore@949 | 136 | DiagnosticChecker diagChecker; |
mcimadamore@949 | 137 | |
mcimadamore@949 | 138 | DisjunctiveTypeWellFormednessTest(Alternative... alternatives) { |
mcimadamore@949 | 139 | this.alternatives = alternatives; |
mcimadamore@949 | 140 | this.source = new JavaSource(); |
mcimadamore@949 | 141 | this.diagChecker = new DiagnosticChecker(); |
mcimadamore@949 | 142 | } |
mcimadamore@949 | 143 | |
mcimadamore@949 | 144 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@949 | 145 | |
mcimadamore@949 | 146 | String template = "class Test {\n" + |
mcimadamore@949 | 147 | "void test() {\n" + |
mcimadamore@949 | 148 | "try {} catch (#T e) {}\n" + |
mcimadamore@949 | 149 | "}\n" + |
mcimadamore@949 | 150 | "}\n"; |
mcimadamore@949 | 151 | |
mcimadamore@949 | 152 | String source; |
mcimadamore@949 | 153 | |
mcimadamore@949 | 154 | public JavaSource() { |
mcimadamore@949 | 155 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@949 | 156 | source = template.replace("#T", Alternative.makeDisjunctiveType(alternatives)); |
mcimadamore@949 | 157 | } |
mcimadamore@949 | 158 | |
mcimadamore@949 | 159 | @Override |
mcimadamore@949 | 160 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@949 | 161 | return source; |
mcimadamore@949 | 162 | } |
mcimadamore@949 | 163 | } |
mcimadamore@949 | 164 | |
vromero@1482 | 165 | @Override |
vromero@1482 | 166 | public void run() { |
vromero@1482 | 167 | JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker, |
mcimadamore@949 | 168 | null, null, Arrays.asList(source)); |
vromero@1482 | 169 | try { |
vromero@1482 | 170 | ct.analyze(); |
vromero@1482 | 171 | } catch (Throwable t) { |
vromero@1482 | 172 | processException(t); |
vromero@1482 | 173 | return; |
vromero@1482 | 174 | } |
mcimadamore@949 | 175 | check(); |
mcimadamore@949 | 176 | } |
mcimadamore@949 | 177 | |
mcimadamore@949 | 178 | void check() { |
mcimadamore@949 | 179 | |
mcimadamore@949 | 180 | int non_disjoint = 0; |
mcimadamore@949 | 181 | int i = 0; |
mcimadamore@949 | 182 | for (Alternative a1 : alternatives) { |
mcimadamore@949 | 183 | int j = 0; |
mcimadamore@949 | 184 | for (Alternative a2 : alternatives) { |
mcimadamore@949 | 185 | if (i == j) continue; |
mcimadamore@949 | 186 | if (!a1.disjoint(a2)) { |
mcimadamore@949 | 187 | non_disjoint++; |
mcimadamore@949 | 188 | break; |
mcimadamore@949 | 189 | } |
mcimadamore@949 | 190 | j++; |
mcimadamore@949 | 191 | } |
mcimadamore@949 | 192 | i++; |
mcimadamore@949 | 193 | } |
mcimadamore@949 | 194 | |
mcimadamore@949 | 195 | if (non_disjoint != diagChecker.errorsFound) { |
mcimadamore@949 | 196 | throw new Error("invalid diagnostics for source:\n" + |
mcimadamore@949 | 197 | source.getCharContent(true) + |
mcimadamore@949 | 198 | "\nFound errors: " + diagChecker.errorsFound + |
mcimadamore@949 | 199 | "\nExpected errors: " + non_disjoint); |
mcimadamore@949 | 200 | } |
mcimadamore@949 | 201 | } |
mcimadamore@949 | 202 | |
mcimadamore@949 | 203 | static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@949 | 204 | |
mcimadamore@949 | 205 | int errorsFound; |
mcimadamore@949 | 206 | |
mcimadamore@949 | 207 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@949 | 208 | if (diagnostic.getKind() == Diagnostic.Kind.ERROR && |
mcimadamore@949 | 209 | diagnostic.getCode().startsWith("compiler.err.multicatch.types.must.be.disjoint")) { |
mcimadamore@949 | 210 | errorsFound++; |
mcimadamore@949 | 211 | } |
mcimadamore@949 | 212 | } |
mcimadamore@949 | 213 | } |
vromero@1482 | 214 | |
mcimadamore@949 | 215 | } |