test/tools/javac/multicatch/7030606/DisjunctiveTypeWellFormednessTest.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1520
5c956be64b9e
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial