test/tools/javac/T8024039/NoDeadCodeGenerationOnTrySmtTest.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/T8024039/NoDeadCodeGenerationOnTrySmtTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,124 @@
     1.4 +/*
     1.5 + * Copyright (c) 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.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * @test
    1.31 + * @bug 8024039
    1.32 + * @summary javac, previous solution for JDK-8022186 was incorrect
    1.33 + * @library /tools/javac/lib
    1.34 + * @build ToolBox
    1.35 + * @run main NoDeadCodeGenerationOnTrySmtTest
    1.36 + */
    1.37 +
    1.38 +import java.io.File;
    1.39 +import java.nio.file.Paths;
    1.40 +
    1.41 +import com.sun.tools.classfile.ClassFile;
    1.42 +import com.sun.tools.classfile.Code_attribute;
    1.43 +import com.sun.tools.classfile.Code_attribute.Exception_data;
    1.44 +import com.sun.tools.classfile.Method;
    1.45 +import com.sun.tools.javac.util.Assert;
    1.46 +
    1.47 +public class NoDeadCodeGenerationOnTrySmtTest {
    1.48 +
    1.49 +    static final String testSource =
    1.50 +        "public class Test {\n" +
    1.51 +        "    void m1(int arg) {\n" +
    1.52 +        "        synchronized (new Integer(arg)) {\n" +
    1.53 +        "            {\n" +
    1.54 +        "                label0:\n" +
    1.55 +        "                do {\n" +
    1.56 +        "                    break label0;\n" +
    1.57 +        "                } while (arg != 0);\n" +
    1.58 +        "            }\n" +
    1.59 +        "        }\n" +
    1.60 +        "    }\n" +
    1.61 +
    1.62 +        "    void m2(int arg) {\n" +
    1.63 +        "        synchronized (new Integer(arg)) {\n" +
    1.64 +        "            {\n" +
    1.65 +        "                label0:\n" +
    1.66 +        "                {\n" +
    1.67 +        "                    break label0;\n" +
    1.68 +        "                }\n" +
    1.69 +        "            }\n" +
    1.70 +        "        }\n" +
    1.71 +        "    }\n" +
    1.72 +        "}";
    1.73 +
    1.74 +    static final int[][] expectedExceptionTable = {
    1.75 +    //  {from,         to,         target,      type},
    1.76 +        {11,           13,         16,          0},
    1.77 +        {16,           19,         16,          0}
    1.78 +    };
    1.79 +
    1.80 +    static final String[] methodsToLookFor = {"m1", "m2"};
    1.81 +
    1.82 +    public static void main(String[] args) throws Exception {
    1.83 +        new NoDeadCodeGenerationOnTrySmtTest().run();
    1.84 +    }
    1.85 +
    1.86 +    void run() throws Exception {
    1.87 +        compileTestClass();
    1.88 +        checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
    1.89 +                "Test.class").toUri()), methodsToLookFor);
    1.90 +    }
    1.91 +
    1.92 +    void compileTestClass() throws Exception {
    1.93 +        ToolBox.JavaToolArgs javacSuccessArgs =
    1.94 +                new ToolBox.JavaToolArgs().setSources(testSource);
    1.95 +        ToolBox.javac(javacSuccessArgs);
    1.96 +    }
    1.97 +
    1.98 +    void checkClassFile(final File cfile, String[] methodsToFind) throws Exception {
    1.99 +        ClassFile classFile = ClassFile.read(cfile);
   1.100 +        int numberOfmethodsFound = 0;
   1.101 +        for (String methodToFind : methodsToFind) {
   1.102 +            for (Method method : classFile.methods) {
   1.103 +                if (method.getName(classFile.constant_pool).equals(methodToFind)) {
   1.104 +                    numberOfmethodsFound++;
   1.105 +                    Code_attribute code = (Code_attribute) method.attributes.get("Code");
   1.106 +                    Assert.check(code.exception_table_length == expectedExceptionTable.length,
   1.107 +                            "The ExceptionTable found has a length different to the expected one");
   1.108 +                    int i = 0;
   1.109 +                    for (Exception_data entry: code.exception_table) {
   1.110 +                        Assert.check(entry.start_pc == expectedExceptionTable[i][0] &&
   1.111 +                                entry.end_pc == expectedExceptionTable[i][1] &&
   1.112 +                                entry.handler_pc == expectedExceptionTable[i][2] &&
   1.113 +                                entry.catch_type == expectedExceptionTable[i][3],
   1.114 +                                "Exception table entry at pos " + i + " differ from expected.");
   1.115 +                        i++;
   1.116 +                    }
   1.117 +                }
   1.118 +            }
   1.119 +        }
   1.120 +        Assert.check(numberOfmethodsFound == 2, "Some seek methods were not found");
   1.121 +    }
   1.122 +
   1.123 +    void error(String msg) {
   1.124 +        throw new AssertionError(msg);
   1.125 +    }
   1.126 +
   1.127 +}

mercurial