langtools/test/tools/javac/SwitchMetricTest.java
changeset 27122 c14f94b48e30
equal deleted inserted replaced
27121:36889255488f 27122:c14f94b48e30
       
     1 /*
       
     2  * Copyright (c) 2014, 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8058243
       
    27  * @summary Reduce size of bytecode for large switch statements
       
    28  * @library /tools/lib
       
    29  * @build ToolBox
       
    30  * @run main SwitchMetricTest
       
    31  */
       
    32 
       
    33 import java.net.URL;
       
    34 import java.util.List;
       
    35 
       
    36 public class SwitchMetricTest {
       
    37     public static void main(String... args) throws Exception {
       
    38         new SwitchMetricTest().run();
       
    39     }
       
    40 
       
    41     // This code should produce a tableswitch
       
    42     class Test1 {
       
    43         int m(int x) {
       
    44             switch (x) {
       
    45             case 1:
       
    46             case 2:
       
    47             case 3:
       
    48             case 4:
       
    49             case 5: return 1;
       
    50             default: return 0;
       
    51             }
       
    52         }
       
    53     }
       
    54 
       
    55     // This code should produce a lookupswitch
       
    56     class Test2 {
       
    57         int m(int x) {
       
    58             switch (x) {
       
    59             case 1:
       
    60             case 2:
       
    61             case 3:
       
    62             case 4:
       
    63             case 50: return 1;
       
    64             default: return 0;
       
    65             }
       
    66         }
       
    67     }
       
    68 
       
    69     void check(String classfile, String bytecode) throws Exception {
       
    70         ToolBox tb = new ToolBox();
       
    71         URL url = SwitchMetricTest.class.getResource(classfile);
       
    72         List<String> result = tb.new JavapTask()
       
    73                 .options("-c")
       
    74                 .classes(url.getFile())
       
    75                 .run()
       
    76                 .write(ToolBox.OutputKind.DIRECT)
       
    77                 .getOutputLines(ToolBox.OutputKind.DIRECT);
       
    78 
       
    79         List<String> matches = tb.grep(bytecode, result);
       
    80         if (matches.isEmpty())
       
    81             throw new Exception(bytecode + " not found");
       
    82     }
       
    83 
       
    84     void run() throws Exception {
       
    85         check("SwitchMetricTest$Test1.class", "tableswitch");
       
    86         check("SwitchMetricTest$Test2.class", "lookupswitch");
       
    87     }
       
    88 }