test/hotspot/jtreg/vmTestbase/nsk/sysdict/share/GenClassesBuilder.java
author jlahoda
Wed, 27 Nov 2019 09:00:01 +0100
changeset 59285 7799a51dbe30
parent 50154 d2d6bc39ea88
permissions -rw-r--r--
8231826: Implement javac changes for pattern matching for instanceof Reviewed-by: mcimadamore Contributed-by: brian.goetz@oracle.com, gavin.bierman@oracle.com, maurizio.cimadamore@oracle.com, srikanth.adayapalam@oracle.com, vicente.romero@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50154
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
     1
/*
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
     4
 *
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
     8
 *
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    13
 * accompanied this code).
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    14
 *
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    18
 *
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    21
 * questions.
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    22
 */
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    23
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    24
package nsk.sysdict.share;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    25
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    26
import jdk.test.lib.JDKToolLauncher;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    27
import jdk.test.lib.Utils;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    28
import jdk.test.lib.process.ProcessTools;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    29
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    30
import java.io.IOException;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    31
import java.nio.file.Files;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    32
import java.nio.file.Path;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    33
import java.nio.file.Paths;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    34
import java.util.Arrays;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    35
import java.util.stream.Stream;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    36
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    37
public class GenClassesBuilder {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    38
    public static void main(String[] args) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    39
        if (args == null || args.length == 0) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    40
            throw new Error("args can't be empty");
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    41
        }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    42
        for (String arg : args) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    43
            switch (arg) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    44
                case "btree":
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    45
                    build("btree", "BTree",
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    46
                            () -> BTreeGen.main(new String[] {"default"}));
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    47
                    break;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    48
                case "leans":
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    49
                    build("leans", "Leans",
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    50
                            () -> ChainGen.main(new String[] {"leans"}));
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    51
                    break;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    52
                case "fats":
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    53
                    build("fats", "Fats",
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    54
                            () -> ChainGen.main(new String[] {"fats"}));
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    55
                    break;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    56
                default:
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    57
                    throw new Error("unkown target " + arg);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    58
            }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    59
        }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    60
    }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    61
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    62
    private static void build(String name, String prefix, Runnable generator) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    63
        generator.run();
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    64
        Path genSrcDir = Paths.get(name, "genSrc", "nsk", "sysdict", "share");
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    65
        Path classesDir = Paths.get(name,"classes").toAbsolutePath();
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    66
        try {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    67
            Files.createDirectories(genSrcDir);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    68
        } catch (IOException e) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    69
            throw new Error("can't create dir " + genSrcDir, e);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    70
        }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    71
        moveJavaFiles(genSrcDir, prefix);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    72
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    73
        JDKToolLauncher javac = JDKToolLauncher.create("javac")
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    74
                                               .addToolArg("-d")
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    75
                                               .addToolArg(classesDir.toString())
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    76
                                               .addToolArg("-cp")
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    77
                                               .addToolArg(Utils.TEST_CLASS_PATH);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    78
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    79
        try (Stream<Path> stream = Files.walk(genSrcDir)) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    80
            stream.map(Path::toAbsolutePath)
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    81
                  .map(Path::toString)
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    82
                  .filter(s -> s.endsWith(".java"))
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    83
                  .forEach(javac::addToolArg);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    84
        } catch (IOException e) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    85
            throw new Error("traverse dir " + genSrcDir, e);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    86
        }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    87
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    88
        executeTool(javac);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    89
        buildJar(name + ".jar", classesDir);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    90
    }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    91
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    92
    private static void executeTool(JDKToolLauncher tool) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    93
        String[] command = tool.getCommand();
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    94
        try {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    95
            ProcessTools.executeCommand(command)
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    96
                        .shouldHaveExitValue(0);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    97
        } catch (Error | RuntimeException e) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    98
            throw e;
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
    99
        } catch (Throwable e) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   100
            throw new Error("execution of " + Arrays.toString(command) + " failed", e);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   101
        }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   102
    }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   103
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   104
    private static void buildJar(String name, Path dir) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   105
        JDKToolLauncher jar = JDKToolLauncher.create("jar")
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   106
                                             .addToolArg("cf")
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   107
                                             .addToolArg(name)
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   108
                                             .addToolArg("-C")
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   109
                                             .addToolArg(dir.toString())
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   110
                                             .addToolArg(".");
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   111
        executeTool(jar);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   112
    }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   113
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   114
    private static void moveJavaFiles(Path dir, String prefix) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   115
        try (Stream<Path> stream = Files.list(Paths.get("."))) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   116
            stream.filter(Files::isRegularFile)
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   117
                  .filter(p -> {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   118
                      String s = p.getFileName().toString();
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   119
                      return s.startsWith(prefix) && s.endsWith(".java");})
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   120
                  .forEach(p -> move(p, dir));
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   121
        } catch (IOException e) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   122
            throw new Error("can't traverse current dir", e);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   123
        }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   124
    }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   125
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   126
    private static void move(Path src, Path dstDir) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   127
        if (Files.notExists(src)) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   128
            throw new Error("file " + src + " doesn't exit");
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   129
        }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   130
        try {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   131
            Files.move(src, dstDir.resolve(src.getFileName()));
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   132
        } catch (IOException e) {
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   133
            throw new Error("can't move " + src + " to " + dstDir, e);
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   134
        }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   135
    }
d2d6bc39ea88 8199252: [TESTBUG] Open source VM testbase system dictionary tests
mseledtsov
parents:
diff changeset
   136
}