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