test/tools/javac/T7093325.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/T7093325.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,269 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 7093325 8006694
    1.30 + * @summary Redundant entry in bytecode exception table
    1.31 + *  temporarily workaround combo tests are causing time out in several platforms
    1.32 + * @library lib
    1.33 + * @build JavacTestingAbstractThreadedTest
    1.34 + * @run main/othervm T7093325
    1.35 + */
    1.36 +
    1.37 +// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
    1.38 +// see JDK-8006746
    1.39 +
    1.40 +import java.io.File;
    1.41 +import java.net.URI;
    1.42 +import java.util.Arrays;
    1.43 +import javax.tools.JavaCompiler;
    1.44 +import javax.tools.JavaFileObject;
    1.45 +import javax.tools.SimpleJavaFileObject;
    1.46 +import javax.tools.ToolProvider;
    1.47 +
    1.48 +import com.sun.source.util.JavacTask;
    1.49 +import com.sun.tools.classfile.Attribute;
    1.50 +import com.sun.tools.classfile.ClassFile;
    1.51 +import com.sun.tools.classfile.Code_attribute;
    1.52 +import com.sun.tools.classfile.ConstantPool.*;
    1.53 +import com.sun.tools.classfile.Method;
    1.54 +
    1.55 +public class T7093325
    1.56 +    extends JavacTestingAbstractThreadedTest
    1.57 +    implements Runnable {
    1.58 +
    1.59 +    enum StatementKind {
    1.60 +        THROW("throw new RuntimeException();", false, false),
    1.61 +        RETURN_NONEMPTY("System.out.println(); return;", true, false),
    1.62 +        RETURN_EMPTY("return;", true, true),
    1.63 +        APPLY("System.out.println();", true, false);
    1.64 +
    1.65 +        String stmt;
    1.66 +        boolean canInline;
    1.67 +        boolean empty;
    1.68 +
    1.69 +        private StatementKind(String stmt, boolean canInline, boolean empty) {
    1.70 +            this.stmt = stmt;
    1.71 +            this.canInline = canInline;
    1.72 +            this.empty = empty;
    1.73 +        }
    1.74 +    }
    1.75 +
    1.76 +    enum CatchArity {
    1.77 +        NONE(""),
    1.78 +        ONE("catch (A a) { #S1 }"),
    1.79 +        TWO("catch (B b) { #S2 }"),
    1.80 +        THREE("catch (C c) { #S3 }"),
    1.81 +        FOUR("catch (D d) { #S4 }");
    1.82 +
    1.83 +        String catchStr;
    1.84 +
    1.85 +        private CatchArity(String catchStr) {
    1.86 +            this.catchStr = catchStr;
    1.87 +        }
    1.88 +
    1.89 +        String catchers() {
    1.90 +            if (this.ordinal() == 0) {
    1.91 +                return catchStr;
    1.92 +            } else {
    1.93 +                return CatchArity.values()[this.ordinal() - 1].catchers() +
    1.94 +                        catchStr;
    1.95 +            }
    1.96 +        }
    1.97 +    }
    1.98 +
    1.99 +    public static void main(String... args) throws Exception {
   1.100 +        for (CatchArity ca : CatchArity.values()) {
   1.101 +            for (StatementKind stmt0 : StatementKind.values()) {
   1.102 +                if (ca.ordinal() == 0) {
   1.103 +                    pool.execute(new T7093325(ca, stmt0));
   1.104 +                    continue;
   1.105 +                }
   1.106 +                for (StatementKind stmt1 : StatementKind.values()) {
   1.107 +                    if (ca.ordinal() == 1) {
   1.108 +                        pool.execute(new T7093325(ca, stmt0, stmt1));
   1.109 +                        continue;
   1.110 +                    }
   1.111 +                    for (StatementKind stmt2 : StatementKind.values()) {
   1.112 +                        if (ca.ordinal() == 2) {
   1.113 +                            pool.execute(new T7093325(ca, stmt0, stmt1, stmt2));
   1.114 +                            continue;
   1.115 +                        }
   1.116 +                        for (StatementKind stmt3 : StatementKind.values()) {
   1.117 +                            if (ca.ordinal() == 3) {
   1.118 +                                pool.execute(
   1.119 +                                    new T7093325(ca, stmt0, stmt1, stmt2, stmt3));
   1.120 +                                continue;
   1.121 +                            }
   1.122 +                            for (StatementKind stmt4 : StatementKind.values()) {
   1.123 +                                if (ca.ordinal() == 4) {
   1.124 +                                    pool.execute(
   1.125 +                                        new T7093325(ca, stmt0, stmt1,
   1.126 +                                                     stmt2, stmt3, stmt4));
   1.127 +                                    continue;
   1.128 +                                }
   1.129 +                                for (StatementKind stmt5 : StatementKind.values()) {
   1.130 +                                    pool.execute(
   1.131 +                                        new T7093325(ca, stmt0, stmt1, stmt2,
   1.132 +                                                     stmt3, stmt4, stmt5));
   1.133 +                                }
   1.134 +                            }
   1.135 +                        }
   1.136 +                    }
   1.137 +                }
   1.138 +            }
   1.139 +        }
   1.140 +
   1.141 +        checkAfterExec();
   1.142 +    }
   1.143 +
   1.144 +    /** instance decls **/
   1.145 +
   1.146 +    CatchArity ca;
   1.147 +    StatementKind[] stmts;
   1.148 +
   1.149 +    public T7093325(CatchArity ca, StatementKind... stmts) {
   1.150 +        this.ca = ca;
   1.151 +        this.stmts = stmts;
   1.152 +    }
   1.153 +
   1.154 +    @Override
   1.155 +    public void run() {
   1.156 +        int id = checkCount.incrementAndGet();
   1.157 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   1.158 +        JavaSource source = new JavaSource(id);
   1.159 +        JavacTask ct = (JavacTask)tool.getTask(null, fm.get(), null,
   1.160 +                null, null, Arrays.asList(source));
   1.161 +        ct.call();
   1.162 +        verifyBytecode(source, id);
   1.163 +    }
   1.164 +
   1.165 +    void verifyBytecode(JavaSource source, int id) {
   1.166 +        boolean lastInlined = false;
   1.167 +        boolean hasCode = false;
   1.168 +        int gapsCount = 0;
   1.169 +        for (int i = 0; i < stmts.length ; i++) {
   1.170 +            lastInlined = stmts[i].canInline;
   1.171 +            hasCode = hasCode || !stmts[i].empty;
   1.172 +            if (lastInlined && hasCode) {
   1.173 +                hasCode = false;
   1.174 +                gapsCount++;
   1.175 +            }
   1.176 +        }
   1.177 +        if (!lastInlined) {
   1.178 +            gapsCount++;
   1.179 +        }
   1.180 +
   1.181 +        File compiledTest = new File(String.format("Test%s.class", id));
   1.182 +        try {
   1.183 +            ClassFile cf = ClassFile.read(compiledTest);
   1.184 +            if (cf == null) {
   1.185 +                throw new Error("Classfile not found: " +
   1.186 +                                compiledTest.getName());
   1.187 +            }
   1.188 +
   1.189 +            Method test_method = null;
   1.190 +            for (Method m : cf.methods) {
   1.191 +                if (m.getName(cf.constant_pool).equals("test")) {
   1.192 +                    test_method = m;
   1.193 +                    break;
   1.194 +                }
   1.195 +            }
   1.196 +
   1.197 +            if (test_method == null) {
   1.198 +                throw new Error("Method test() not found in class Test");
   1.199 +            }
   1.200 +
   1.201 +            Code_attribute code = null;
   1.202 +            for (Attribute a : test_method.attributes) {
   1.203 +                if (a.getName(cf.constant_pool).equals(Attribute.Code)) {
   1.204 +                    code = (Code_attribute)a;
   1.205 +                    break;
   1.206 +                }
   1.207 +            }
   1.208 +
   1.209 +            if (code == null) {
   1.210 +                throw new Error("Code attribute not found in method test()");
   1.211 +            }
   1.212 +
   1.213 +            int actualGapsCount = 0;
   1.214 +            for (int i = 0; i < code.exception_table_length ; i++) {
   1.215 +                int catchType = code.exception_table[i].catch_type;
   1.216 +                if (catchType == 0) { //any
   1.217 +                    actualGapsCount++;
   1.218 +                }
   1.219 +            }
   1.220 +
   1.221 +            if (actualGapsCount != gapsCount) {
   1.222 +                throw new Error("Bad exception table for test()\n" +
   1.223 +                            "expected gaps: " + gapsCount + "\n" +
   1.224 +                            "found gaps: " + actualGapsCount + "\n" +
   1.225 +                            source);
   1.226 +            }
   1.227 +        } catch (Exception e) {
   1.228 +            e.printStackTrace();
   1.229 +            throw new Error("error reading " + compiledTest +": " + e);
   1.230 +        }
   1.231 +
   1.232 +    }
   1.233 +
   1.234 +    class JavaSource extends SimpleJavaFileObject {
   1.235 +
   1.236 +        static final String source_template =
   1.237 +                "class A extends RuntimeException {} \n" +
   1.238 +                "class B extends RuntimeException {} \n" +
   1.239 +                "class C extends RuntimeException {} \n" +
   1.240 +                "class D extends RuntimeException {} \n" +
   1.241 +                "class E extends RuntimeException {} \n" +
   1.242 +                "class Test#ID {\n" +
   1.243 +                "   void test() {\n" +
   1.244 +                "   try { #S0 } #C finally { System.out.println(); }\n" +
   1.245 +                "   }\n" +
   1.246 +                "}";
   1.247 +
   1.248 +        String source;
   1.249 +
   1.250 +        public JavaSource(int id) {
   1.251 +            super(URI.create(String.format("myfo:/Test%s.java", id)),
   1.252 +                  JavaFileObject.Kind.SOURCE);
   1.253 +            source = source_template.replace("#C", ca.catchers());
   1.254 +            source = source.replace("#S0", stmts[0].stmt);
   1.255 +            source = source.replace("#ID", String.valueOf(id));
   1.256 +            for (int i = 1; i < ca.ordinal() + 1; i++) {
   1.257 +                source = source.replace("#S" + i, stmts[i].stmt);
   1.258 +            }
   1.259 +        }
   1.260 +
   1.261 +        @Override
   1.262 +        public String toString() {
   1.263 +            return source;
   1.264 +        }
   1.265 +
   1.266 +        @Override
   1.267 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.268 +            return source;
   1.269 +        }
   1.270 +    }
   1.271 +
   1.272 +}

mercurial