src/java.base/share/classes/jdk/internal/module/ModulePathValidator.java
author alanb
Thu, 21 Jun 2018 18:56:35 +0100
changeset 50700 97e9c4f58986
permissions -rw-r--r--
8194937: Inconsistent behavior of --validate-modules when combined with -m and other options Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50700
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
     1
/*
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
     4
 *
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    10
 *
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    15
 * accompanied this code).
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    16
 *
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    20
 *
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    23
 * questions.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    24
 */
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    25
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    26
package jdk.internal.module;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    27
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    28
import java.io.File;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    29
import java.io.IOException;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    30
import java.io.PrintStream;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    31
import java.lang.module.FindException;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleDescriptor;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleFinder;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    34
import java.lang.module.ModuleReference;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    35
import java.net.URI;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    36
import java.nio.file.DirectoryStream;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    37
import java.nio.file.Files;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    38
import java.nio.file.NoSuchFileException;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    39
import java.nio.file.Path;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    40
import java.nio.file.attribute.BasicFileAttributes;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    41
import java.util.Comparator;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    42
import java.util.HashMap;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    43
import java.util.Map;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    44
import java.util.Optional;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    45
import java.util.stream.Stream;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    46
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    47
/**
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    48
 * A validator to check for errors and conflicts between modules.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    49
 */
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    50
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    51
class ModulePathValidator {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    52
    private static final String MODULE_INFO = "module-info.class";
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    53
    private static final String INDENT = "    ";
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    54
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    55
    private final Map<String, ModuleReference> nameToModule;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    56
    private final Map<String, ModuleReference> packageToModule;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    57
    private final PrintStream out;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    58
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    59
    private int errorCount;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    60
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    61
    private ModulePathValidator(PrintStream out) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    62
        this.nameToModule = new HashMap<>();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    63
        this.packageToModule = new HashMap<>();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    64
        this.out = out;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    65
    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    66
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    67
    /**
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    68
     * Scans and the validates all modules on the module path. The module path
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    69
     * comprises the upgrade module path, system modules, and the application
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    70
     * module path.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    71
     *
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    72
     * @param out the print stream for output messages
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    73
     * @return the number of errors found
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    74
     */
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    75
    static int scanAllModules(PrintStream out) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    76
        ModulePathValidator validator = new ModulePathValidator(out);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    77
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    78
        // upgrade module path
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    79
        String value = System.getProperty("jdk.module.upgrade.path");
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    80
        if (value != null) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    81
            Stream.of(value.split(File.pathSeparator))
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    82
                    .map(Path::of)
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    83
                    .forEach(validator::scan);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    84
        }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    85
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    86
        // system modules
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    87
        ModuleFinder.ofSystem().findAll().stream()
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    88
                .sorted(Comparator.comparing(ModuleReference::descriptor))
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    89
                .forEach(validator::process);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    90
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    91
        // application module path
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    92
        value = System.getProperty("jdk.module.path");
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    93
        if (value != null) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    94
            Stream.of(value.split(File.pathSeparator))
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    95
                    .map(Path::of)
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    96
                    .forEach(validator::scan);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    97
        }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    98
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
    99
        return validator.errorCount;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   100
    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   101
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   102
    /**
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   103
     * Prints the module location and name.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   104
     */
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   105
    private void printModule(ModuleReference mref) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   106
        mref.location()
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   107
                .filter(uri -> !isJrt(uri))
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   108
                .ifPresent(uri -> out.print(uri + " "));
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   109
        ModuleDescriptor descriptor = mref.descriptor();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   110
        out.print(descriptor.name());
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   111
        if (descriptor.isAutomatic())
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   112
            out.print(" automatic");
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   113
        out.println();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   114
    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   115
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   116
    /**
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   117
     * Prints the module location and name, checks if the module is
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   118
     * shadowed by a previously seen module, and finally checks for
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   119
     * package conflicts with previously seen modules.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   120
     */
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   121
    private void process(ModuleReference mref) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   122
        String name = mref.descriptor().name();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   123
        ModuleReference previous = nameToModule.putIfAbsent(name, mref);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   124
        if (previous != null) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   125
            printModule(mref);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   126
            out.print(INDENT + "shadowed by ");
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   127
            printModule(previous);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   128
        } else {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   129
            boolean first = true;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   130
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   131
            // check for package conflicts when not shadowed
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   132
            for (String pkg :  mref.descriptor().packages()) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   133
                previous = packageToModule.putIfAbsent(pkg, mref);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   134
                if (previous != null) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   135
                    if (first) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   136
                        printModule(mref);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   137
                        first = false;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   138
                        errorCount++;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   139
                    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   140
                    String mn = previous.descriptor().name();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   141
                    out.println(INDENT + "contains " + pkg
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   142
                            + " conflicts with module " + mn);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   143
                }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   144
            }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   145
        }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   146
    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   147
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   148
    /**
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   149
     * Scan an element on a module path. The element is a directory
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   150
     * of modules, an exploded module, or a JAR file.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   151
     */
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   152
    private void scan(Path entry) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   153
        BasicFileAttributes attrs;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   154
        try {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   155
            attrs = Files.readAttributes(entry, BasicFileAttributes.class);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   156
        } catch (NoSuchFileException ignore) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   157
            return;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   158
        } catch (IOException ioe) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   159
            out.println(entry + " " + ioe);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   160
            errorCount++;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   161
            return;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   162
        }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   163
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   164
        String fn = entry.getFileName().toString();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   165
        if (attrs.isRegularFile() && fn.endsWith(".jar")) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   166
            // JAR file, explicit or automatic module
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   167
            scanModule(entry).ifPresent(this::process);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   168
        } else if (attrs.isDirectory()) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   169
            Path mi = entry.resolve(MODULE_INFO);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   170
            if (Files.exists(mi)) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   171
                // exploded module
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   172
                scanModule(entry).ifPresent(this::process);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   173
            } else {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   174
                // directory of modules
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   175
                scanDirectory(entry);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   176
            }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   177
        }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   178
    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   179
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   180
    /**
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   181
     * Scan the JAR files and exploded modules in a directory.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   182
     */
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   183
    private void scanDirectory(Path dir) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   184
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   185
            Map<String, Path> moduleToEntry = new HashMap<>();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   186
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   187
            for (Path entry : stream) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   188
                BasicFileAttributes attrs;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   189
                try {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   190
                    attrs = Files.readAttributes(entry, BasicFileAttributes.class);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   191
                } catch (IOException ioe) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   192
                    out.println(entry + " " + ioe);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   193
                    errorCount++;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   194
                    continue;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   195
                }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   196
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   197
                ModuleReference mref = null;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   198
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   199
                String fn = entry.getFileName().toString();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   200
                if (attrs.isRegularFile() && fn.endsWith(".jar")) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   201
                    mref = scanModule(entry).orElse(null);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   202
                } else if (attrs.isDirectory()) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   203
                    Path mi = entry.resolve(MODULE_INFO);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   204
                    if (Files.exists(mi)) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   205
                        mref = scanModule(entry).orElse(null);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   206
                    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   207
                }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   208
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   209
                if (mref != null) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   210
                    String name = mref.descriptor().name();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   211
                    Path previous = moduleToEntry.putIfAbsent(name, entry);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   212
                    if (previous != null) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   213
                        // same name as other module in the directory
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   214
                        printModule(mref);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   215
                        out.println(INDENT + "contains same module as "
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   216
                                + previous.getFileName());
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   217
                        errorCount++;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   218
                    } else {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   219
                        process(mref);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   220
                    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   221
                }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   222
            }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   223
        } catch (IOException ioe) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   224
            out.println(dir + " " + ioe);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   225
            errorCount++;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   226
        }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   227
    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   228
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   229
    /**
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   230
     * Scan a JAR file or exploded module.
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   231
     */
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   232
    private Optional<ModuleReference> scanModule(Path entry) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   233
        ModuleFinder finder = ModuleFinder.of(entry);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   234
        try {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   235
            return finder.findAll().stream().findFirst();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   236
        } catch (FindException e) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   237
            out.println(entry);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   238
            out.println(INDENT + e.getMessage());
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   239
            Throwable cause = e.getCause();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   240
            if (cause != null) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   241
                out.println(INDENT + cause);
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   242
            }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   243
            errorCount++;
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   244
            return Optional.empty();
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   245
        }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   246
    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   247
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   248
    /**
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   249
     * Returns true if the given URI is a jrt URI
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   250
     */
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   251
    private static boolean isJrt(URI uri) {
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   252
        return (uri != null && uri.getScheme().equalsIgnoreCase("jrt"));
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   253
    }
97e9c4f58986 8194937: Inconsistent behavior of --validate-modules when combined with -m and other options
alanb
parents:
diff changeset
   254
}