langtools/test/tools/javac/T8024039/NoDeadCodeGenerationOnTrySmtTest.java
changeset 19923 4895f15b3845
child 20236 32da2c913ac4
equal deleted inserted replaced
19922:52810e1bbb4f 19923:4895f15b3845
       
     1 /*
       
     2  * Copyright (c) 2013, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 /*
       
    27  * @test
       
    28  * @bug 8024039
       
    29  * @summary javac, previous solution for JDK-8022186 was incorrect
       
    30  * @library /tools/javac/lib
       
    31  * @build ToolBox
       
    32  * @run main NoDeadCodeGenerationOnTrySmtTest
       
    33  */
       
    34 
       
    35 import java.io.File;
       
    36 import java.nio.file.Paths;
       
    37 
       
    38 import com.sun.tools.classfile.ClassFile;
       
    39 import com.sun.tools.classfile.Code_attribute;
       
    40 import com.sun.tools.classfile.Code_attribute.Exception_data;
       
    41 import com.sun.tools.classfile.Method;
       
    42 import com.sun.tools.javac.util.Assert;
       
    43 
       
    44 public class NoDeadCodeGenerationOnTrySmtTest {
       
    45 
       
    46     static final String testSource =
       
    47         "public class Test {\n" +
       
    48         "    void m1(int arg) {\n" +
       
    49         "        synchronized (new Integer(arg)) {\n" +
       
    50         "            {\n" +
       
    51         "                label0:\n" +
       
    52         "                do {\n" +
       
    53         "                    break label0;\n" +
       
    54         "                } while (arg != 0);\n" +
       
    55         "            }\n" +
       
    56         "        }\n" +
       
    57         "    }\n" +
       
    58 
       
    59         "    void m2(int arg) {\n" +
       
    60         "        synchronized (new Integer(arg)) {\n" +
       
    61         "            {\n" +
       
    62         "                label0:\n" +
       
    63         "                {\n" +
       
    64         "                    break label0;\n" +
       
    65         "                }\n" +
       
    66         "            }\n" +
       
    67         "        }\n" +
       
    68         "    }\n" +
       
    69         "}";
       
    70 
       
    71     static final int[][] expectedExceptionTable = {
       
    72     //  {from,         to,         target,      type},
       
    73         {11,           13,         16,          0},
       
    74         {16,           19,         16,          0}
       
    75     };
       
    76 
       
    77     static final String[] methodsToLookFor = {"m1", "m2"};
       
    78 
       
    79     public static void main(String[] args) throws Exception {
       
    80         new NoDeadCodeGenerationOnTrySmtTest().run();
       
    81     }
       
    82 
       
    83     void run() throws Exception {
       
    84         compileTestClass();
       
    85         checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
       
    86                 "Test.class").toUri()), methodsToLookFor);
       
    87     }
       
    88 
       
    89     void compileTestClass() throws Exception {
       
    90         ToolBox.JavaToolArgs javacSuccessArgs =
       
    91                 new ToolBox.JavaToolArgs().setSources(testSource);
       
    92         ToolBox.javac(javacSuccessArgs);
       
    93     }
       
    94 
       
    95     void checkClassFile(final File cfile, String[] methodsToFind) throws Exception {
       
    96         ClassFile classFile = ClassFile.read(cfile);
       
    97         int numberOfmethodsFound = 0;
       
    98         for (String methodToFind : methodsToFind) {
       
    99             for (Method method : classFile.methods) {
       
   100                 if (method.getName(classFile.constant_pool).equals(methodToFind)) {
       
   101                     numberOfmethodsFound++;
       
   102                     Code_attribute code = (Code_attribute) method.attributes.get("Code");
       
   103                     Assert.check(code.exception_table_langth == expectedExceptionTable.length,
       
   104                             "The ExceptionTable found has a length different to the expected one");
       
   105                     int i = 0;
       
   106                     for (Exception_data entry: code.exception_table) {
       
   107                         Assert.check(entry.start_pc == expectedExceptionTable[i][0] &&
       
   108                                 entry.end_pc == expectedExceptionTable[i][1] &&
       
   109                                 entry.handler_pc == expectedExceptionTable[i][2] &&
       
   110                                 entry.catch_type == expectedExceptionTable[i][3],
       
   111                                 "Exception table entry at pos " + i + " differ from expected.");
       
   112                         i++;
       
   113                     }
       
   114                 }
       
   115             }
       
   116         }
       
   117         Assert.check(numberOfmethodsFound == 2, "Some seek methods were not found");
       
   118     }
       
   119 
       
   120     void error(String msg) {
       
   121         throw new AssertionError(msg);
       
   122     }
       
   123 
       
   124 }