src/jdk.jlink/share/classes/jdk/tools/jimage/JImageTask.java
author goetz
Tue, 16 Jan 2018 08:48:34 +0100
changeset 48543 7067fe4e054e
parent 47216 71c04702a3d5
child 48913 7f9c3cd11e97
permissions -rw-r--r--
8189102: All tools should support -?, -h and --help Reviewed-by: kvn, jjg, weijun, alanb, rfield, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
     2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
36511
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.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package jdk.tools.jimage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.File;
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.PrintWriter;
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
    31
import java.nio.file.FileSystem;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.nio.file.Files;
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
    33
import java.nio.file.PathMatcher;
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    34
import java.util.ArrayList;
42174
fa763b059501 8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents: 41919
diff changeset
    35
import java.util.Arrays;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.LinkedList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.List;
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
    38
import java.util.Locale;
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
    39
import java.util.MissingResourceException;
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    40
import java.util.function.Predicate;
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
    41
import java.util.stream.Collectors;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
    42
import java.util.stream.Stream;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
    43
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import jdk.internal.jimage.BasicImageReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.internal.jimage.ImageHeader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.internal.jimage.ImageLocation;
39762
935ffb4260ba 8158407: jimage: verify should do more extensive test
jlaskey
parents: 39150
diff changeset
    47
import jdk.internal.org.objectweb.asm.ClassReader;
935ffb4260ba 8158407: jimage: verify should do more extensive test
jlaskey
parents: 39150
diff changeset
    48
import jdk.internal.org.objectweb.asm.tree.ClassNode;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import jdk.tools.jlink.internal.ImageResourcesTree;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import jdk.tools.jlink.internal.TaskHelper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import jdk.tools.jlink.internal.TaskHelper.BadArgs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import static jdk.tools.jlink.internal.TaskHelper.JIMAGE_BUNDLE;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import jdk.tools.jlink.internal.TaskHelper.Option;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.tools.jlink.internal.TaskHelper.OptionsHelper;
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
    55
import jdk.tools.jlink.internal.Utils;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
class JImageTask {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
    58
    private static final Option<?>[] RECOGNIZED_OPTIONS = {
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
    59
        new Option<JImageTask>(true, (task, option, arg) -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
            task.options.directory = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        }, "--dir"),
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    62
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    63
        new Option<JImageTask>(true, (task, option, arg) -> {
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
    64
            task.options.include = arg;
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
    65
        }, "--include"),
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    66
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
    67
        new Option<JImageTask>(false, (task, option, arg) -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            task.options.fullVersion = true;
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 39762
diff changeset
    69
        }, true, "--full-version"),
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    70
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
    71
        new Option<JImageTask>(false, (task, option, arg) -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            task.options.help = true;
48543
7067fe4e054e 8189102: All tools should support -?, -h and --help
goetz
parents: 47216
diff changeset
    73
        }, "--help", "-h", "-?"),
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    74
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
    75
        new Option<JImageTask>(false, (task, option, arg) -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            task.options.verbose = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        }, "--verbose"),
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    78
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
    79
        new Option<JImageTask>(false, (task, option, arg) -> {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            task.options.version = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }, "--version")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    };
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
    83
    private static final TaskHelper TASK_HELPER
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            = new TaskHelper(JIMAGE_BUNDLE);
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
    85
    private static final OptionsHelper<JImageTask> OPTION_HELPER
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
    86
            = TASK_HELPER.newOptionsHelper(JImageTask.class, RECOGNIZED_OPTIONS);
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    87
    private static final String PROGNAME = "jimage";
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
    88
    private static final FileSystem JRT_FILE_SYSTEM = Utils.jrtFileSystem();
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    89
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    90
    private final OptionsValues options;
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
    91
    private final List<Predicate<String>> includePredicates;
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
    92
    private PrintWriter log;
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    93
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    94
    JImageTask() {
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    95
        this.options = new OptionsValues();
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
    96
        this.includePredicates = new ArrayList<>();
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    97
        log = null;
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    98
    }
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
    99
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   100
    void setLog(PrintWriter out) {
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   101
        log = out;
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   102
        TASK_HELPER.setLog(log);
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   103
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    static class OptionsValues {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   106
        Task task = null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        String directory = ".";
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
   108
        String include = "";
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        boolean fullVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        boolean help;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        boolean verbose;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        boolean version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        List<File> jimages = new LinkedList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
    enum Task {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        EXTRACT,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        INFO,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        LIST,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        VERIFY
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    private String pad(String string, int width, boolean justifyRight) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        int length = string.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        if (length == width) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            return string;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        if (length > width) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            return string.substring(0, width);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        int padding = width - length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        StringBuilder sb = new StringBuilder(width);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        if (justifyRight) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
            for (int i = 0; i < padding; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                sb.append(' ');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        sb.append(string);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        if (!justifyRight) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            for (int i = 0; i < padding; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                sb.append(' ');
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        return sb.toString();
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 String pad(String string, int width) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        return pad(string, width, false);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
    private String pad(long value, int width) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        return pad(Long.toString(value), width, true);
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
    private static final int EXIT_OK = 0;        // No errors.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
    private static final int EXIT_ERROR = 1;     // Completed but reported errors.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    private static final int EXIT_CMDERR = 2;    // Bad command-line arguments and/or switches.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
    private static final int EXIT_SYSERR = 3;    // System error or resource exhaustion.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
    private static final int EXIT_ABNORMAL = 4;  // Terminated abnormally.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
    int run(String[] args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        if (log == null) {
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   170
            setLog(new PrintWriter(System.out, true));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        }
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   172
37613
2040f359c9b1 8082537: jimage should print usage when started with no args
jlaskey
parents: 36511
diff changeset
   173
        if (args.length == 0) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   174
            log.println(TASK_HELPER.getMessage("main.usage.summary", PROGNAME));
37613
2040f359c9b1 8082537: jimage should print usage when started with no args
jlaskey
parents: 36511
diff changeset
   175
            return EXIT_ABNORMAL;
2040f359c9b1 8082537: jimage should print usage when started with no args
jlaskey
parents: 36511
diff changeset
   176
        }
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   177
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        try {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   179
            String command;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   180
            String[] remaining = args;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   181
            try {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   182
                command = args[0];
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   183
                options.task = Enum.valueOf(Task.class, args[0].toUpperCase(Locale.ENGLISH));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   184
                remaining = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   185
                                            : new String[0];
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   186
            } catch (IllegalArgumentException ex) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   187
                command = null;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   188
                options.task = null;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   189
            }
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   190
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   191
            // process arguments
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   192
            List<String> unhandled = OPTION_HELPER.handleOptions(this, remaining);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   193
            for (String f : unhandled) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   194
                options.jimages.add(new File(f));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   195
            }
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   196
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   197
            if (options.task == null && !options.help && !options.version && !options.fullVersion) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   198
                throw TASK_HELPER.newBadArgs("err.not.a.task",
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   199
                    command != null ? command : "<unspecified>");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
            }
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   201
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            if (options.help) {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   203
                if (options.task == null) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   204
                    log.println(TASK_HELPER.getMessage("main.usage", PROGNAME));
42174
fa763b059501 8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents: 41919
diff changeset
   205
                    Arrays.asList(RECOGNIZED_OPTIONS).stream()
fa763b059501 8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents: 41919
diff changeset
   206
                        .filter(option -> !option.isHidden())
fa763b059501 8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents: 41919
diff changeset
   207
                        .sorted()
fa763b059501 8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents: 41919
diff changeset
   208
                        .forEach(option -> {
fa763b059501 8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents: 41919
diff changeset
   209
                             log.println(TASK_HELPER.getMessage(option.resourceName()));
fa763b059501 8168256: Plugin alias options in jlink --help output seems to be in an arbitrary order
jlaskey
parents: 41919
diff changeset
   210
                        });
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
   211
                    log.println(TASK_HELPER.getMessage("main.opt.footer"));
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   212
                } else {
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   213
                    try {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   214
                        log.println(TASK_HELPER.getMessage("main.usage." +
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   215
                                options.task.toString().toLowerCase()));
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   216
                    } catch (MissingResourceException ex) {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   217
                        throw TASK_HELPER.newBadArgs("err.not.a.task", command);
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   218
                    }
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   219
                }
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   220
                return EXIT_OK;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
            }
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   222
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
            if (options.version || options.fullVersion) {
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   224
                if (options.task == null && !unhandled.isEmpty()) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   225
                    throw TASK_HELPER.newBadArgs("err.not.a.task",
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   226
                        Stream.of(args).collect(Collectors.joining(" ")));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   227
                }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   228
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   229
                TASK_HELPER.showVersion(options.fullVersion);
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   230
                if (unhandled.isEmpty()) {
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   231
                    return EXIT_OK;
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   232
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            }
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   234
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
   235
            processInclude(options.include);
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   236
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   237
            return run() ? EXIT_OK : EXIT_ERROR;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
        } catch (BadArgs e) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   239
            TASK_HELPER.reportError(e.key, e.args);
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   240
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            if (e.showUsage) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   242
                log.println(TASK_HELPER.getMessage("main.usage.summary", PROGNAME));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            }
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   244
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            return EXIT_CMDERR;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
        } catch (Exception x) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            x.printStackTrace();
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   248
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            return EXIT_ABNORMAL;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        } finally {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
            log.flush();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
   255
    private void processInclude(String include) {
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
   256
        if (include.isEmpty()) {
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   257
            return;
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   258
        }
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   259
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
   260
        for (String filter : include.split(",")) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   261
            final PathMatcher matcher = Utils.getPathMatcher(JRT_FILE_SYSTEM, filter);
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   262
            Predicate<String> predicate = (path) -> matcher.matches(JRT_FILE_SYSTEM.getPath(path));
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
   263
            includePredicates.add(predicate);
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   264
        }
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   265
    }
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   266
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
    private void listTitle(File file, BasicImageReader reader) {
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   268
        log.println("jimage: " + file);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
    private interface JImageAction {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
        public void apply(File file, BasicImageReader reader) throws IOException, BadArgs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   275
    private interface ModuleAction {
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   276
         public void apply(BasicImageReader reader,
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   277
                 String oldModule, String newModule) throws IOException, BadArgs;
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   278
    }
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   279
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
    private interface ResourceAction {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
        public void apply(BasicImageReader reader, String name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
                ImageLocation location) throws IOException, BadArgs;
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
    private void extract(BasicImageReader reader, String name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            ImageLocation location) throws IOException, BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
        File directory = new File(options.directory);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
        byte[] bytes = reader.getResource(location);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
        File resource =  new File(directory, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
        File parent = resource.getParentFile();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
        if (parent.exists()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
            if (!parent.isDirectory()) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   294
                throw TASK_HELPER.newBadArgs("err.cannot.create.dir",
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   295
                                            parent.getAbsolutePath());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
        } else if (!parent.mkdirs()) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   298
            throw TASK_HELPER.newBadArgs("err.cannot.create.dir",
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   299
                                        parent.getAbsolutePath());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
        if (!ImageResourcesTree.isTreeInfoResource(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
            Files.write(resource.toPath(), bytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   307
    private static final int OFFSET_WIDTH = 12;
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   308
    private static final int SIZE_WIDTH = 10;
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   309
    private static final int COMPRESSEDSIZE_WIDTH = 10;
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   310
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   311
    private String trimModule(String name) {
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   312
        int offset = name.indexOf('/', 1);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   314
        if (offset != -1 && offset + 1 < name.length()) {
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   315
            return name.substring(offset + 1);
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   316
        }
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   317
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   318
        return name;
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   319
    }
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   320
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   321
    private void print(String name, ImageLocation location) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
        log.print(pad(location.getContentOffset(), OFFSET_WIDTH) + " ");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
        log.print(pad(location.getUncompressedSize(), SIZE_WIDTH) + " ");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
        log.print(pad(location.getCompressedSize(), COMPRESSEDSIZE_WIDTH) + " ");
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   325
        log.println(trimModule(name));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   328
    private void print(BasicImageReader reader, String name) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
        if (options.verbose) {
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   330
            print(name, reader.findLocation(name));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
        } else {
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   332
            log.println("    " + trimModule(name));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
    private void info(File file, BasicImageReader reader) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
        ImageHeader header = reader.getHeader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
        log.println(" Major Version:  " + header.getMajorVersion());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
        log.println(" Minor Version:  " + header.getMinorVersion());
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   341
        log.println(" Flags:          " + Integer.toHexString(header.getFlags()));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
        log.println(" Resource Count: " + header.getResourceCount());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
        log.println(" Table Length:   " + header.getTableLength());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
        log.println(" Offsets Size:   " + header.getOffsetsSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
        log.println(" Redirects Size: " + header.getRedirectSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
        log.println(" Locations Size: " + header.getLocationsSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        log.println(" Strings Size:   " + header.getStringsSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        log.println(" Index Size:     " + header.getIndexSize());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   351
    private void listModule(BasicImageReader reader, String oldModule, String newModule) {
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   352
        log.println();
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   353
        log.println("Module: " + newModule);
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   354
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   355
        if (options.verbose) {
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   356
            log.print(pad("Offset", OFFSET_WIDTH) + " ");
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   357
            log.print(pad("Size", SIZE_WIDTH) + " ");
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   358
            log.print(pad("Compressed", COMPRESSEDSIZE_WIDTH) + " ");
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   359
            log.println("Entry");
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   360
        }
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   361
    }
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   362
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
    private void list(BasicImageReader reader, String name, ImageLocation location) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
        print(reader, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
38869
4bac8f3bce74 8156994: jimage --help is not helpful
jlaskey
parents: 37616
diff changeset
   367
      void verify(BasicImageReader reader, String name, ImageLocation location) {
39762
935ffb4260ba 8158407: jimage: verify should do more extensive test
jlaskey
parents: 39150
diff changeset
   368
        if (name.endsWith(".class") && !name.endsWith("module-info.class")) {
935ffb4260ba 8158407: jimage: verify should do more extensive test
jlaskey
parents: 39150
diff changeset
   369
            try {
935ffb4260ba 8158407: jimage: verify should do more extensive test
jlaskey
parents: 39150
diff changeset
   370
                byte[] bytes = reader.getResource(location);
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 40261
diff changeset
   371
                ClassReader cr = new ClassReader(bytes);
39762
935ffb4260ba 8158407: jimage: verify should do more extensive test
jlaskey
parents: 39150
diff changeset
   372
                ClassNode cn = new ClassNode();
41919
612b698f8b8f 8159393: jlink should print a warning that a signed modular JAR will be treated as unsigned
jlaskey
parents: 40261
diff changeset
   373
                cr.accept(cn, 0);
39762
935ffb4260ba 8158407: jimage: verify should do more extensive test
jlaskey
parents: 39150
diff changeset
   374
            } catch (Exception ex) {
935ffb4260ba 8158407: jimage: verify should do more extensive test
jlaskey
parents: 39150
diff changeset
   375
                log.println("Error(s) in Class: " + name);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
            }
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
    private void iterate(JImageAction jimageAction,
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   381
            ModuleAction moduleAction,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
            ResourceAction resourceAction) throws IOException, BadArgs {
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   383
        if (options.jimages.isEmpty()) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   384
            throw TASK_HELPER.newBadArgs("err.no.jimage");
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   385
        }
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   386
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
        for (File file : options.jimages) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
            if (!file.exists() || !file.isFile()) {
38871
ec08bf1979d4 8158402: jlink: should use regex for all pattern operations (--order-resources or --exclude-resources)
jlaskey
parents: 38870
diff changeset
   389
                throw TASK_HELPER.newBadArgs("err.not.a.jimage", file.getName());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
            try (BasicImageReader reader = BasicImageReader.open(file.toPath())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
                if (jimageAction != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                    jimageAction.apply(file, reader);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
                if (resourceAction != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
                    String[] entryNames = reader.getEntryNames();
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   399
                    String oldModule = "";
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                    for (String name : entryNames) {
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
   402
                        boolean match = includePredicates.isEmpty();
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   403
39150
cb99396625b4 8159172: Update usage of jlink/jimage/jmod to show option patterns
jlaskey
parents: 38871
diff changeset
   404
                        for (Predicate<String> predicate : includePredicates) {
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   405
                            if (predicate.test(name)) {
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   406
                                match = true;
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   407
                                break;
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   408
                            }
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   409
                        }
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   410
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   411
                        if (!match) {
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   412
                            continue;
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   413
                        }
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   414
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
                        if (!ImageResourcesTree.isTreeInfoResource(name)) {
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   416
                            if (moduleAction != null) {
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   417
                                int offset = name.indexOf('/', 1);
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   418
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   419
                                String newModule = offset != -1 ?
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   420
                                        name.substring(1, offset) :
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   421
                                        "<unknown>";
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   422
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   423
                                if (!oldModule.equals(newModule)) {
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   424
                                    moduleAction.apply(reader, oldModule, newModule);
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   425
                                    oldModule = newModule;
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   426
                                }
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   427
                            }
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   428
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
                            ImageLocation location = reader.findLocation(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
                            resourceAction.apply(reader, name, location);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
    private boolean run() throws Exception, BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
        switch (options.task) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
            case EXTRACT:
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   441
                iterate(null, null, this::extract);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
            case INFO:
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   444
                iterate(this::info, null, null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
            case LIST:
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   447
                iterate(this::listTitle, this::listModule, this::list);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
            case VERIFY:
37616
d7794846510d 8069079: jimage extract / list to organize classes by modules
jlaskey
parents: 37615
diff changeset
   450
                iterate(this::listTitle, null, this::verify);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
            default:
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents: 42174
diff changeset
   453
                throw TASK_HELPER.newBadArgs("err.not.a.task",
38870
9fcc420c73f9 8156995: jimage: extract specified contents
jlaskey
parents: 38869
diff changeset
   454
                        options.task.name()).showUsage(true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
        return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
}