test/jdk/tools/lib/tests/JImageGenerator.java
author lancea
Thu, 14 Nov 2019 10:54:32 -0500
changeset 59083 3e4d8b5856f3
parent 48223 962b4324320c
permissions -rw-r--r--
8234080: jdk/nio/zipfs/CRCWriteTest.java fails Reviewed-by: clanger, amlu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
package tests;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.io.ByteArrayInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.io.ByteArrayOutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.FileOutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.OutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.io.PrintStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.io.PrintWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.io.StringWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.StandardCopyOption;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.jar.JarEntry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.jar.JarInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.jar.JarOutputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.util.zip.ZipEntry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import javax.tools.JavaCompiler;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import javax.tools.StandardJavaFileManager;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import javax.tools.StandardLocation;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import javax.tools.ToolProvider;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 * A generator for jmods, jars and images.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
public class JImageGenerator {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    public static final String LOAD_ALL_CLASSES_TEMPLATE = "package PACKAGE;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            + "\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            + "import java.net.URI;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            + "import java.nio.file.FileSystems;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            + "import java.nio.file.Files;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            + "import java.nio.file.Path;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            + "import java.util.function.Function;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            + "\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            + "public class CLASS {\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            + "    private static long total_time;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            + "    private static long num_classes;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
            + "    public static void main(String[] args) throws Exception {\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            + "        Function<Path, String> formatter = (path) -> {\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            + "            String clazz = path.toString().substring(\"modules/\".length()+1, path.toString().lastIndexOf(\".\"));\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            + "            clazz = clazz.substring(clazz.indexOf(\"/\") + 1);\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            + "            return clazz.replaceAll(\"/\", \"\\\\.\");\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            + "        };\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            + "        Files.walk(FileSystems.getFileSystem(URI.create(\"jrt:/\")).getPath(\"/modules/\")).\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            + "                filter((p) -> {\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            + "                    return Files.isRegularFile(p) && p.toString().endsWith(\".class\")\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            + "                    && !p.toString().endsWith(\"module-info.class\");\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            + "                }).\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            + "                map(formatter).forEach((clazz) -> {\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            + "                    try {\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            + "                        long t = System.currentTimeMillis();\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            + "                        Class.forName(clazz, false, Thread.currentThread().getContextClassLoader());\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            + "                        total_time+= System.currentTimeMillis()-t;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            + "                        num_classes+=1;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            + "                    } catch (IllegalAccessError ex) {\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            + "                        // Security exceptions can occur, this is not what we are testing\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            + "                        System.err.println(\"Access error, OK \" + clazz);\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            + "                    } catch (Exception ex) {\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
            + "                        System.err.println(\"ERROR \" + clazz);\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            + "                        throw new RuntimeException(ex);\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            + "                    }\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            + "                });\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            + "    double res = (double) total_time / num_classes;\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            + "    // System.out.println(\"Total time \" + total_time + \" num classes \" + num_classes + \" average \" + res);\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            + "    }\n"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            + "}\n";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    private static final String OUTPUT_OPTION = "--output";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
    private static final String POST_PROCESS_OPTION = "--post-process-path";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    private static final String MAIN_CLASS_OPTION = "--main-class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    private static final String CLASS_PATH_OPTION = "--class-path";
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   109
    private static final String MODULE_PATH_OPTION = "--module-path";
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   110
    private static final String ADD_MODULES_OPTION = "--add-modules";
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   111
    private static final String LIMIT_MODULES_OPTION = "--limit-modules";
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   112
    private static final String PLUGIN_MODULE_PATH = "--plugin-module-path";
48223
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   113
    private static final String LAUNCHER = "--launcher";
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
    private static final String CMDS_OPTION = "--cmds";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    private static final String CONFIG_OPTION = "--config";
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 37615
diff changeset
   117
    private static final String HASH_MODULES_OPTION = "--hash-modules";
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    private static final String LIBS_OPTION = "--libs";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
    private static final String MODULE_VERSION_OPTION = "--module-version";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
    private JImageGenerator() {}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    private static String optionsPrettyPrint(String... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        return Stream.of(args).collect(Collectors.joining(" "));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    public static File getJModsDir(File jdkHome) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        File jdkjmods = new File(jdkHome, "jmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        if (!jdkjmods.exists()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        return jdkjmods;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    public static Path addFiles(Path module, InMemoryFile... resources) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        Path tempFile = Files.createTempFile("jlink-test", "");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        try (JarInputStream in = new JarInputStream(Files.newInputStream(module));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
             JarOutputStream out = new JarOutputStream(new FileOutputStream(tempFile.toFile()))) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            ZipEntry entry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            while ((entry = in.getNextEntry()) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                String name = entry.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                out.putNextEntry(new ZipEntry(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                copy(in, out);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                out.closeEntry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            for (InMemoryFile r : resources) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                addFile(r, out);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        Files.move(tempFile, module, StandardCopyOption.REPLACE_EXISTING);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        return module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
    private static void copy(InputStream in, OutputStream out) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        int len;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        byte[] buf = new byte[4096];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        while ((len = in.read(buf)) > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            out.write(buf, 0, len);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
    public static JModTask getJModTask() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        return new JModTask();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
    public static JLinkTask getJLinkTask() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        return new JLinkTask();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    public static JImageTask getJImageTask() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        return new JImageTask();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
    private static void addFile(InMemoryFile resource, JarOutputStream target) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        String fileName = resource.getPath();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        fileName = fileName.replace("\\", "/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        String[] ss = fileName.split("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        Path p = Paths.get("");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        for (int i = 0; i < ss.length; ++i) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
            if (i < ss.length - 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                if (!ss[i].isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                    p = p.resolve(ss[i]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
                    JarEntry entry = new JarEntry(p.toString() + "/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                    target.putNextEntry(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                    target.closeEntry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                p = p.resolve(ss[i]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
                JarEntry entry = new JarEntry(p.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
                target.putNextEntry(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                copy(resource.getBytes(), target);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                target.closeEntry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
    public static Path createNewFile(Path root, String pathName, String extension) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        Path out = root.resolve(pathName + extension);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
        int i = 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
        while (Files.exists(out)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            out = root.resolve(pathName + "-" + (++i) + extension);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        return out;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
    public static Path generateSources(Path output, String moduleName, List<InMemorySourceFile> sources) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        Path moduleDir = output.resolve(moduleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
        Files.createDirectory(moduleDir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        for (InMemorySourceFile source : sources) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
            Path fileDir = moduleDir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
            if (!source.packageName.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                String dir = source.packageName.replace('.', File.separatorChar);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                fileDir = moduleDir.resolve(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                Files.createDirectories(fileDir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
            Files.write(fileDir.resolve(source.className + ".java"), source.source.getBytes());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        return moduleDir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
    public static Path generateSourcesFromTemplate(Path output, String moduleName, String... classNames) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        List<InMemorySourceFile> sources = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        for (String className : classNames) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            String packageName = getPackageName(className);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
            String simpleName = getSimpleName(className);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
            String content = LOAD_ALL_CLASSES_TEMPLATE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
                    .replace("CLASS", simpleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
            if (packageName.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                content = content.replace("package PACKAGE;", packageName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
                content = content.replace("PACKAGE", packageName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            sources.add(new InMemorySourceFile(packageName, simpleName, content));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
        return generateSources(output, moduleName, sources);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
    public static void generateModuleInfo(Path moduleDir, List<String> packages, String... dependencies) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
        StringBuilder moduleInfoBuilder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
        Path file = moduleDir.resolve("module-info.java");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
        String moduleName = moduleDir.getFileName().toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        moduleInfoBuilder.append("module ").append(moduleName).append("{\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        for (String dep : dependencies) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            moduleInfoBuilder.append("requires ").append(dep).append(";\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
        for (String pkg : packages) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            if (!pkg.trim().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                moduleInfoBuilder.append("exports ").append(pkg).append(";\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
        moduleInfoBuilder.append("}");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        Files.write(file, moduleInfoBuilder.toString().getBytes());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
    public static void compileSuccess(Path source, Path destination, String... options) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
        if (!compile(source, destination, options)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
            throw new AssertionError("Compilation failed.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
    public static boolean compile(Path source, Path destination, String... options) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        try (StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
            List<Path> sources
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
                    = Files.find(source, Integer.MAX_VALUE,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                    (file, attrs) -> file.toString().endsWith(".java"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                    .collect(Collectors.toList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
            Files.createDirectories(destination);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
            jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singleton(destination));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
            List<String> opts = Arrays.asList(options);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
            JavaCompiler.CompilationTask task
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                    = compiler.getTask(null, jfm, null, opts, null,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                    jfm.getJavaFileObjectsFromPaths(sources));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
            List<String> list = new ArrayList<>(opts);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
            list.addAll(sources.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
                    .map(Path::toString)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                    .collect(Collectors.toList()));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
            System.err.println("javac options: " + optionsPrettyPrint(list.toArray(new String[list.size()])));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
            return task.call();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
    public static Path createJarFile(Path jarfile, Path dir) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
        return createJarFile(jarfile, dir, Paths.get("."));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
    public static Path createJarFile(Path jarfile, Path dir, Path file) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
        // create the target directory
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
        Path parent = jarfile.getParent();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
        if (parent != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
            Files.createDirectories(parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
        List<Path> entries = Files.find(dir.resolve(file), Integer.MAX_VALUE,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
                (p, attrs) -> attrs.isRegularFile())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
                .map(dir::relativize)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
                .collect(Collectors.toList());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
        try (OutputStream out = Files.newOutputStream(jarfile);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
             JarOutputStream jos = new JarOutputStream(out)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
            for (Path entry : entries) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
                // map the file path to a name in the JAR file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                Path normalized = entry.normalize();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
                String name = normalized
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
                        .subpath(0, normalized.getNameCount())  // drop root
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
                        .toString()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
                        .replace(File.separatorChar, '/');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                jos.putNextEntry(new JarEntry(name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                Files.copy(dir.resolve(entry), jos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        return jarfile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
    public static Set<String> getModuleContent(Path module) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
        Result result = JImageGenerator.getJModTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
                .jmod(module)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
                .list();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
        result.assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
        return Stream.of(result.getMessage().split("\r?\n"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
                .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
    public static void checkModule(Path module, Set<String> expected) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
        Set<String> actual = getModuleContent(module);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        if (!Objects.equals(actual, expected)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
            Set<String> unexpected = new HashSet<>(actual);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
            unexpected.removeAll(expected);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
            Set<String> notFound = new HashSet<>(expected);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
            notFound.removeAll(actual);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
            System.err.println("Unexpected files:");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
            unexpected.forEach(s -> System.err.println("\t" + s));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
            System.err.println("Not found files:");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
            notFound.forEach(s -> System.err.println("\t" + s));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            throw new AssertionError("Module check failed.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
    public static class JModTask {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   342
        static final java.util.spi.ToolProvider JMOD_TOOL =
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   343
            java.util.spi.ToolProvider.findFirst("jmod").orElseThrow(() ->
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   344
                new RuntimeException("jmod tool not found")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   345
            );
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        private final List<Path> classpath = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        private final List<Path> libs = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
        private final List<Path> cmds = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
        private final List<Path> config = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
        private final List<Path> jars = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
        private final List<Path> jmods = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
        private final List<String> options = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
        private Path output;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 37615
diff changeset
   355
        private String hashModules;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        private String mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
        private String moduleVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
        public JModTask addNativeLibraries(Path cp) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            this.libs.add(cp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 37615
diff changeset
   364
        public JModTask hashModules(String hash) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 37615
diff changeset
   365
            this.hashModules = hash;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
        public JModTask addCmds(Path cp) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
            this.cmds.add(cp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
        public JModTask addClassPath(Path cp) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
            this.classpath.add(cp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
        public JModTask addConfig(Path cp) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
            this.config.add(cp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
        public JModTask addJars(Path jars) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
            this.jars.add(jars);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
        public JModTask addJmods(Path jmods) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
            this.jmods.add(jmods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
        public JModTask jmod(Path output) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
            this.output = output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
        public JModTask moduleVersion(String moduleVersion) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
            this.moduleVersion = moduleVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
        public JModTask mainClass(String mainClass) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
            this.mainClass = mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
        public JModTask option(String o) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
            this.options.add(o);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
        private String modulePath() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
            // This is expect FIRST jmods THEN jars, if you change this, some tests could fail
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
            String jmods = toPath(this.jmods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
            String jars = toPath(this.jars);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
            return jmods + File.pathSeparator + jars;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
        private String toPath(List<Path> paths) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
            return paths.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
                    .map(Path::toString)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
                    .collect(Collectors.joining(File.pathSeparator));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
        private String[] optionsJMod(String cmd) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
            List<String> options = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
            options.add(cmd);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
            if (!cmds.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
                options.add(CMDS_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
                options.add(toPath(cmds));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
            if (!config.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
                options.add(CONFIG_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
                options.add(toPath(config));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
            }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 37615
diff changeset
   438
            if (hashModules != null) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 37615
diff changeset
   439
                options.add(HASH_MODULES_OPTION);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 37615
diff changeset
   440
                options.add(hashModules);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
            if (mainClass != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
                options.add(MAIN_CLASS_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
                options.add(mainClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
            if (!libs.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
                options.add(LIBS_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
                options.add(toPath(libs));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
            if (!classpath.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
                options.add(CLASS_PATH_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
                options.add(toPath(classpath));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
            if (!jars.isEmpty() || !jmods.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
                options.add(MODULE_PATH_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
                options.add(modulePath());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
            if (moduleVersion != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
                options.add(MODULE_VERSION_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
                options.add(moduleVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
            options.addAll(this.options);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
            if (output != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
                options.add(output.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
            return options.toArray(new String[options.size()]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
        public Result create() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
            return cmd("create");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        public Result list() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
            return cmd("list");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
        public Result call() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
            return cmd("");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
        private Result cmd(String cmd) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
            String[] args = optionsJMod(cmd);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
            System.err.println("jmod options: " + optionsPrettyPrint(args));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   485
            PrintStream ps = new PrintStream(baos);
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   486
            int exitCode = JMOD_TOOL.run(ps, ps, args);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
            String msg = new String(baos.toByteArray());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
            return new Result(exitCode, msg, output);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
    public static String getPackageName(String canonicalName) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
        int index = canonicalName.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
        return index > 0 ? canonicalName.substring(0, index) : "";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
    public static String getSimpleName(String canonicalName) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
        int index = canonicalName.lastIndexOf('.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
        return canonicalName.substring(index + 1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
    public static class JImageTask {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
        private final List<Path> pluginModulePath = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
        private final List<String> options = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
        private Path dir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
        private Path image;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
        public JImageTask pluginModulePath(Path p) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
            this.pluginModulePath.add(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
        public JImageTask image(Path image) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
            this.image = image;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
        public JImageTask dir(Path dir) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
            this.dir = dir;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
        public JImageTask option(String o) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            this.options.add(o);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
        private String toPath(List<Path> paths) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
            return paths.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
                    .map(Path::toString)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
                    .collect(Collectors.joining(File.pathSeparator));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
        private String[] optionsJImage(String cmd) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
            List<String> options = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
            options.add(cmd);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
            if (dir != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
                options.add("--dir");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
                options.add(dir.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
            if (!pluginModulePath.isEmpty()) {
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   543
                options.add(PLUGIN_MODULE_PATH);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
                options.add(toPath(pluginModulePath));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            options.addAll(this.options);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
            options.add(image.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
            return options.toArray(new String[options.size()]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
        private Result cmd(String cmd, Path returnPath) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
            String[] args = optionsJImage(cmd);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
            System.err.println("jimage options: " + optionsPrettyPrint(args));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
            StringWriter writer = new StringWriter();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
            int exitCode = jdk.tools.jimage.Main.run(args, new PrintWriter(writer));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
            return new Result(exitCode, writer.toString(), returnPath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
        public Result extract() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
            return cmd("extract", dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
    public static class JLinkTask {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   565
        static final java.util.spi.ToolProvider JLINK_TOOL =
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   566
            java.util.spi.ToolProvider.findFirst("jlink").orElseThrow(() ->
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   567
                new RuntimeException("jlink tool not found")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   568
            );
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
        private final List<Path> jars = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
        private final List<Path> jmods = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
        private final List<Path> pluginModulePath = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
        private final List<String> addMods = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
        private final List<String> limitMods = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
        private final List<String> options = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
        private String modulePath;
41212
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   577
        // if you want to specifiy repeated --module-path option
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   578
        private String repeatedModulePath;
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   579
        // if you want to specifiy repeated --limit-modules option
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   580
        private String repeatedLimitMods;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
        private Path output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
        private Path existing;
48223
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   583
        private String launcher; // optional
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
        public JLinkTask modulePath(String modulePath) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
            this.modulePath = modulePath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
48223
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   590
        public JLinkTask launcher(String cmd) {
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   591
            launcher = Objects.requireNonNull(cmd);
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   592
            return this;
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   593
        }
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   594
41212
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   595
        public JLinkTask repeatedModulePath(String modulePath) {
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   596
            this.repeatedModulePath = modulePath;
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   597
            return this;
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   598
        }
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   599
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
        public JLinkTask addJars(Path jars) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
            this.jars.add(jars);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
        public JLinkTask addJmods(Path jmods) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
            this.jmods.add(jmods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
        public JLinkTask pluginModulePath(Path p) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
            this.pluginModulePath.add(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
        public JLinkTask addMods(String moduleName) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
            this.addMods.add(moduleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
        public JLinkTask limitMods(String moduleName) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
            this.limitMods.add(moduleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
41212
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   625
        public JLinkTask repeatedLimitMods(String modules) {
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   626
            this.repeatedLimitMods = modules;
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   627
            return this;
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   628
        }
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   629
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
        public JLinkTask output(Path output) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
            this.output = output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
        public JLinkTask existing(Path existing) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
            this.existing = existing;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
        public JLinkTask option(String o) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
            this.options.add(o);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
        private String modulePath() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
            // This is expect FIRST jmods THEN jars, if you change this, some tests could fail
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
            String jmods = toPath(this.jmods);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
            String jars = toPath(this.jars);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
            return jmods + File.pathSeparator + jars;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
        private String toPath(List<Path> paths) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
            return paths.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
                    .map(Path::toString)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
                    .collect(Collectors.joining(File.pathSeparator));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
        private String[] optionsJLink() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
            List<String> options = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   660
            if (output != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
                options.add(OUTPUT_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
                options.add(output.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
            if (!addMods.isEmpty()) {
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   665
                options.add(ADD_MODULES_OPTION);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
                options.add(addMods.stream().collect(Collectors.joining(",")));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
            if (!limitMods.isEmpty()) {
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   669
                options.add(LIMIT_MODULES_OPTION);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
                options.add(limitMods.stream().collect(Collectors.joining(",")));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
            }
41212
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   672
            if (repeatedLimitMods != null) {
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   673
                options.add(LIMIT_MODULES_OPTION);
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   674
                options.add(repeatedLimitMods);
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   675
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
            if (!jars.isEmpty() || !jmods.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
                options.add(MODULE_PATH_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
                options.add(modulePath());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
            if (modulePath != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
                options.add(MODULE_PATH_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
                options.add(modulePath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
            }
41212
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   684
            if (repeatedModulePath != null) {
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   685
                options.add(MODULE_PATH_OPTION);
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   686
                options.add(repeatedModulePath);
54a8bb41b174 8165735: jlink incorrectly accepts multiple --module-path and --limit-modules options
sundar
parents: 40261
diff changeset
   687
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
            if (!pluginModulePath.isEmpty()) {
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   689
                options.add(PLUGIN_MODULE_PATH);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
                options.add(toPath(pluginModulePath));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
            }
48223
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   692
            if (launcher != null && !launcher.isEmpty()) {
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   693
                options.add(LAUNCHER);
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   694
                options.add(launcher);
962b4324320c 8192986: Inconsistent handling of exploded modules in jlink
sundar
parents: 47216
diff changeset
   695
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
            options.addAll(this.options);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
            return options.toArray(new String[options.size()]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
        private String[] optionsPostProcessJLink() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
            List<String> options = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
            if (existing != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
                options.add(POST_PROCESS_OPTION);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
                options.add(existing.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
            options.addAll(this.options);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
            return options.toArray(new String[options.size()]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
        public Result call() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
            String[] args = optionsJLink();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
            System.err.println("jlink options: " + optionsPrettyPrint(args));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
            StringWriter writer = new StringWriter();
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   714
            PrintWriter pw = new PrintWriter(writer);
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   715
            int exitCode = JLINK_TOOL.run(pw, pw, args);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
            return new Result(exitCode, writer.toString(), output);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
        public Result callPostProcess() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
            String[] args = optionsPostProcessJLink();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
            System.err.println("jlink options: " + optionsPrettyPrint(args));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
            StringWriter writer = new StringWriter();
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   723
            PrintWriter pw = new PrintWriter(writer);
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 41212
diff changeset
   724
            int exitCode = JLINK_TOOL.run(pw, pw, args);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
            return new Result(exitCode, writer.toString(), output);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
    public static class InMemorySourceFile {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
        public final String packageName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
        public final String className;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
        public final String source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
        public InMemorySourceFile(String packageName, String simpleName, String source) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
            this.packageName = packageName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
            this.className = simpleName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
            this.source = source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   740
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
    public static class InMemoryFile {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
        private final String path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
        private final byte[] bytes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
        public String getPath() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
            return path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
        public InputStream getBytes() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
            return new ByteArrayInputStream(bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   753
        public InMemoryFile(String path, byte[] bytes) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   754
            this.path = path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   755
            this.bytes = bytes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   756
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
        public InMemoryFile(String path, InputStream is) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
            this(path, readAllBytes(is));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
    public static byte[] readAllBytes(InputStream is) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
        byte[] buf = new byte[1024];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
        while (true) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   767
            int n = is.read(buf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   768
            if (n < 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   769
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   770
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   771
            baos.write(buf, 0, n);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   772
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   773
        return baos.toByteArray();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   774
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   775
}