jdk/src/java.base/share/classes/java/lang/module/ModulePath.java
author alanb
Fri, 17 Jun 2016 08:41:39 +0100
changeset 39050 9de41b79ec7e
parent 37779 7c84df693837
child 39646 2bf99fe5023b
permissions -rw-r--r--
8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class 8159248: ModuleFinder.of not clear that FindException thrown if module descriptor cannot be derived for automatic module Reviewed-by: chegar, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  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 java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.BufferedInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.BufferedReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.io.InputStreamReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.module.ModuleDescriptor.Requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.DirectoryStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.NoSuchFileException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.file.attribute.BasicFileAttributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.HashMap;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    43
import java.util.LinkedHashSet;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.jar.Attributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.jar.JarEntry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.jar.JarFile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.util.jar.Manifest;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import java.util.regex.Matcher;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import java.util.regex.Pattern;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import java.util.zip.ZipEntry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import java.util.zip.ZipFile;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import jdk.internal.module.ConfigurableModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import jdk.internal.perf.PerfCounter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 * A {@code ModuleFinder} that locates modules on the file system by searching
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 * a sequence of directories or packaged modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 * The {@code ModuleFinder} can be configured to work in either the run-time
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 * or link-time phases. In both cases it locates modular JAR and exploded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 * modules. When configured for link-time then it additionally locates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 * modules in JMOD files.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
class ModulePath implements ConfigurableModuleFinder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
    private static final String MODULE_INFO = "module-info.class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    // the entries on this module path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    private final Path[] entries;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    private int next;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
    // true if in the link phase
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    private boolean isLinkPhase;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
    // map of module name to module reference map for modules already located
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    private final Map<String, ModuleReference> cachedModules = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    ModulePath(Path... entries) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        this.entries = entries.clone();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        for (Path entry : this.entries) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            Objects.requireNonNull(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    public void configurePhase(Phase phase) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        isLinkPhase = (phase == Phase.LINK_TIME);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    public Optional<ModuleReference> find(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        Objects.requireNonNull(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        // try cached modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        ModuleReference m = cachedModules.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        if (m != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            return Optional.of(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        // the module may not have been encountered yet
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        while (hasNextEntry()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            scanNextEntry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            m = cachedModules.get(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            if (m != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                return Optional.of(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        return Optional.empty();
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
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
    public Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        // need to ensure that all entries have been scanned
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        while (hasNextEntry()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            scanNextEntry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        return cachedModules.values().stream().collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
     * Returns {@code true} if there are additional entries to scan
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    private boolean hasNextEntry() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        return next < entries.length;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
     * Scans the next entry on the module path. A no-op if all entries have
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
     * already been scanned.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     * @throws FindException if an error occurs scanning the next entry
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    private void scanNextEntry() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        if (hasNextEntry()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            long t0 = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            Path entry = entries[next];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
            Map<String, ModuleReference> modules = scan(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            next++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
            // update cache, ignoring duplicates
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            int initialSize = cachedModules.size();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
            for (Map.Entry<String, ModuleReference> e : modules.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                cachedModules.putIfAbsent(e.getKey(), e.getValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            // update counters
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            int added = cachedModules.size() - initialSize;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            moduleCount.add(added);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
            scanTime.addElapsedTimeFrom(t0);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
     * Scan the given module path entry. If the entry is a directory then it is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
     * a directory of modules or an exploded module. If the entry is a regular
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
     * file then it is assumed to be a packaged module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
     * @throws FindException if an error occurs scanning the entry
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
    private Map<String, ModuleReference> scan(Path entry) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        BasicFileAttributes attrs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            attrs = Files.readAttributes(entry, BasicFileAttributes.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        } catch (NoSuchFileException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            return Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            throw new FindException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
            if (attrs.isDirectory()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
                Path mi = entry.resolve(MODULE_INFO);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                if (!Files.exists(mi)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                    // does not exist or unable to determine so assume a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                    // directory of modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
                    return scanDirectory(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   191
            // packaged or exploded module
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   192
            ModuleReference mref = readModule(entry, attrs);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   193
            if (mref != null) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   194
                String name = mref.descriptor().name();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   195
                return Collections.singletonMap(name, mref);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   196
            } else {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   197
                // skipped
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   198
                return Collections.emptyMap();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            throw new FindException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
     * Scans the given directory for packaged or exploded modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
     * @return a map of module name to ModuleReference for the modules found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
     *         in the directory
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
     * @throws IOException if an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
     * @throws FindException if an error occurs scanning the entry or the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
     *         directory contains two or more modules with the same name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
    private Map<String, ModuleReference> scanDirectory(Path dir)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
        // The map of name -> mref of modules found in this directory.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
        Map<String, ModuleReference> nameToReference = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            for (Path entry : stream) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
                BasicFileAttributes attrs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
                    attrs = Files.readAttributes(entry, BasicFileAttributes.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
                } catch (NoSuchFileException ignore) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                    // file has been removed or moved, ignore for now
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                ModuleReference mref = readModule(entry, attrs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                // module found
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                if (mref != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                    // can have at most one version of a module in the directory
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                    String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
                    if (nameToReference.put(name, mref) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                        throw new FindException("Two versions of module "
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   241
                                                  + name + " found in " + dir);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
        return nameToReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
     * Locates a packaged or exploded module, returning a {@code ModuleReference}
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   253
     * to the module. Returns {@code null} if the entry is skipped because it is
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   254
     * to a directory that does not contain a module-info.class or it's a hidden
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   255
     * file.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
     * @throws IOException if an I/O error occurs
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   258
     * @throws FindException if the file is not recognized as a module or an
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   259
     *         error occurs parsing its module descriptor
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
    private ModuleReference readModule(Path entry, BasicFileAttributes attrs)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
            if (attrs.isDirectory()) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   267
                return readExplodedModule(entry); // may return null
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   268
            }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   269
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   270
            String fn = entry.getFileName().toString();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   271
            if (attrs.isRegularFile()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   272
                if (fn.endsWith(".jar")) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   273
                    return readJar(entry);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   274
                } else if (fn.endsWith(".jmod")) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   275
                    if (isLinkPhase)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   276
                        return readJMod(entry);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   277
                    throw new FindException("JMOD files not supported: " + entry);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
            }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   280
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   281
            // skip hidden files
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   282
            if (fn.startsWith(".") || Files.isHidden(entry)) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   283
                return null;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   284
            } else {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   285
                throw new FindException("Unrecognized module: " + entry);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   286
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
        } catch (InvalidModuleDescriptorException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
            throw new FindException("Error reading module: " + entry, e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
    // -- jmod files --
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
    private Set<String> jmodPackages(ZipFile zf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
        return zf.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            .filter(e -> e.getName().startsWith("classes/") &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
                    e.getName().endsWith(".class"))
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   300
            .map(e -> toPackageName(e.getName().substring(8)))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
            .filter(pkg -> pkg.length() > 0) // module-info
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
            .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
    }
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
     * Returns a {@code ModuleReference} to a module in jmod file on the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
     * file system.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   308
     *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   309
     * @throws IOException
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   310
     * @throws InvalidModuleDescriptorException
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
    private ModuleReference readJMod(Path file) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        try (ZipFile zf = new ZipFile(file.toString())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
            ZipEntry ze = zf.getEntry("classes/" + MODULE_INFO);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
            if (ze == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
                throw new IOException(MODULE_INFO + " is missing: " + file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            ModuleDescriptor md;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
            try (InputStream in = zf.getInputStream(ze)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
                md = ModuleDescriptor.read(in, () -> jmodPackages(zf));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
            return ModuleReferences.newJModModule(md, file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
    // -- JAR files --
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
    private static final String SERVICES_PREFIX = "META-INF/services/";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
     * Returns a container with the service type corresponding to the name of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
     * a services configuration file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
     * For example, if called with "META-INF/services/p.S" then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
     * returns a container with the value "p.S".
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
    private Optional<String> toServiceName(String cf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
        assert cf.startsWith(SERVICES_PREFIX);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
        int index = cf.lastIndexOf("/") + 1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
        if (index < cf.length()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
            String prefix = cf.substring(0, index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
            if (prefix.equals(SERVICES_PREFIX)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
                String sn = cf.substring(index);
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   345
                return Optional.of(sn);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        return Optional.empty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
     * Reads the next line from the given reader and trims it of comments and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
     * leading/trailing white space.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
     * Returns null if the reader is at EOF.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
    private String nextLine(BufferedReader reader) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
        String ln = reader.readLine();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
        if (ln != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            int ci = ln.indexOf('#');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
            if (ci >= 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
                ln = ln.substring(0, ci);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
            ln = ln.trim();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
        return ln;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
     * Treat the given JAR file as a module as follows:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
     * 1. The module name (and optionally the version) is derived from the file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
     *    name of the JAR file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
     * 2. The packages of all .class files in the JAR file are exported
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
     * 3. It has no module-private/concealed packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
     * 4. The contents of any META-INF/services configuration files are mapped
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
     *    to "provides" declarations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
     * 5. The Main-Class attribute in the main attributes of the JAR manifest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
     *    is mapped to the module descriptor mainClass
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
    private ModuleDescriptor deriveModuleDescriptor(JarFile jf)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
        // Derive module name and version from JAR file name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
        String fn = jf.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
        int i = fn.lastIndexOf(File.separator);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
        if (i != -1)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
            fn = fn.substring(i+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
        // drop .jar
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
        String mn = fn.substring(0, fn.length()-4);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        String vs = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
        // find first occurrence of -${NUMBER}. or -${NUMBER}$
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   395
        Matcher matcher = Patterns.DASH_VERSION.matcher(mn);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
        if (matcher.find()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
            int start = matcher.start();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
            // attempt to parse the tail as a version string
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                String tail = mn.substring(start+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
                ModuleDescriptor.Version.parse(tail);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                vs = tail;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
            } catch (IllegalArgumentException ignore) { }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
            mn = mn.substring(0, start);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
        // finally clean up the module name
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   410
        mn = cleanModuleName(mn);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
        // Builder throws IAE if module name is empty or invalid
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
        ModuleDescriptor.Builder builder
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   414
            = new ModuleDescriptor.Builder(mn)
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   415
                .automatic()
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   416
                .requires(Set.of(Requires.Modifier.MANDATED), "java.base");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
        if (vs != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
            builder.version(vs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
        // scan the entries in the JAR file to locate the .class and service
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
        // configuration file
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   422
        Map<Boolean, Set<String>> map =
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   423
            jf.stream()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   424
              .map(JarEntry::getName)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   425
              .filter(s -> (s.endsWith(".class") ^ s.startsWith(SERVICES_PREFIX)))
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   426
              .collect(Collectors.partitioningBy(s -> s.endsWith(".class"),
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   427
                                                 Collectors.toSet()));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
        Set<String> classFiles = map.get(Boolean.TRUE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
        Set<String> configFiles = map.get(Boolean.FALSE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
        // all packages are exported
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
        classFiles.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
            .map(c -> toPackageName(c))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
            .distinct()
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   435
            .forEach(builder::exports);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
        // map names of service configuration files to service names
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
        Set<String> serviceNames = configFiles.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
            .map(this::toServiceName)
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   440
            .flatMap(Optional::stream)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
        // parse each service configuration file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
        for (String sn : serviceNames) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
            JarEntry entry = jf.getJarEntry(SERVICES_PREFIX + sn);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   446
            Set<String> providerClasses = new LinkedHashSet<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
            try (InputStream in = jf.getInputStream(entry)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
                BufferedReader reader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
                    = new BufferedReader(new InputStreamReader(in, "UTF-8"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
                String cn;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
                while ((cn = nextLine(reader)) != null) {
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   452
                    if (cn.length() > 0) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
                        providerClasses.add(cn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
            if (!providerClasses.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
                builder.provides(sn, providerClasses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
        // Main-Class attribute if it exists
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
        Manifest man = jf.getManifest();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
        if (man != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
            Attributes attrs = man.getMainAttributes();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
            String mainClass = attrs.getValue(Attributes.Name.MAIN_CLASS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
            if (mainClass != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
                builder.mainClass(mainClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
        return builder.build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   473
    /**
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   474
     * Patterns used to derive the module name from a JAR file name.
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   475
     */
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   476
    private static class Patterns {
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   477
        static final Pattern DASH_VERSION = Pattern.compile("-(\\d+(\\.|$))");
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   478
        static final Pattern NON_ALPHANUM = Pattern.compile("[^A-Za-z0-9]");
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   479
        static final Pattern REPEATING_DOTS = Pattern.compile("(\\.)(\\1)+");
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   480
        static final Pattern LEADING_DOTS = Pattern.compile("^\\.");
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   481
        static final Pattern TRAILING_DOTS = Pattern.compile("\\.$");
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   482
    }
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   483
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   484
    /**
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   485
     * Clean up candidate module name derived from a JAR file name.
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   486
     */
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   487
    private static String cleanModuleName(String mn) {
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   488
        // replace non-alphanumeric
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   489
        mn = Patterns.NON_ALPHANUM.matcher(mn).replaceAll(".");
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   490
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   491
        // collapse repeating dots
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   492
        mn = Patterns.REPEATING_DOTS.matcher(mn).replaceAll(".");
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   493
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   494
        // drop leading dots
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   495
        if (mn.length() > 0 && mn.charAt(0) == '.')
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   496
            mn = Patterns.LEADING_DOTS.matcher(mn).replaceAll("");
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   497
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   498
        // drop trailing dots
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   499
        int len = mn.length();
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   500
        if (len > 0 && mn.charAt(len-1) == '.')
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   501
            mn = Patterns.TRAILING_DOTS.matcher(mn).replaceAll("");
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   502
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   503
        return mn;
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   504
    }
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   505
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
    private Set<String> jarPackages(JarFile jf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
        return jf.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
            .filter(e -> e.getName().endsWith(".class"))
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   509
            .map(e -> toPackageName(e.getName()))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
            .filter(pkg -> pkg.length() > 0)   // module-info
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
            .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
     * Returns a {@code ModuleReference} to a module in modular JAR file on
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
     * the file system.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   517
     *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   518
     * @throws IOException
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   519
     * @throws FindException
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   520
     * @throws InvalidModuleDescriptorException
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
    private ModuleReference readJar(Path file) throws IOException {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   523
        try (JarFile jf = new JarFile(file.toFile(),
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   524
                                      true,               // verify
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   525
                                      ZipFile.OPEN_READ,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   526
                                      JarFile.Release.RUNTIME))
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   527
        {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
            ModuleDescriptor md;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
            JarEntry entry = jf.getJarEntry(MODULE_INFO);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
            if (entry == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
                // no module-info.class so treat it as automatic module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
                    md = deriveModuleDescriptor(jf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
                } catch (IllegalArgumentException iae) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
                    throw new FindException(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
                        "Unable to derive module descriptor for: "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
                        + jf.getName(), iae);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
                md = ModuleDescriptor.read(jf.getInputStream(entry),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
                                           () -> jarPackages(jf));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            return ModuleReferences.newJarModule(md, file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
    // -- exploded directories --
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
    private Set<String> explodedPackages(Path dir) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
            return Files.find(dir, Integer.MAX_VALUE,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
                              ((path, attrs) -> attrs.isRegularFile() &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
                               path.toString().endsWith(".class")))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
                .map(path -> toPackageName(dir.relativize(path)))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
                .filter(pkg -> pkg.length() > 0)   // module-info
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
                .collect(Collectors.toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
        } catch (IOException x) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
            throw new UncheckedIOException(x);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
     * Returns a {@code ModuleReference} to an exploded module on the file
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
     * system or {@code null} if {@code module-info.class} not found.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   569
     *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   570
     * @throws IOException
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   571
     * @throws InvalidModuleDescriptorException
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
    private ModuleReference readExplodedModule(Path dir) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
        Path mi = dir.resolve(MODULE_INFO);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
        ModuleDescriptor md;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
        try (InputStream in = Files.newInputStream(mi)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
            md = ModuleDescriptor.read(new BufferedInputStream(in),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
                                       () -> explodedPackages(dir));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
        } catch (NoSuchFileException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
            // for now
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
        return ModuleReferences.newExplodedModule(md, dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
    //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
    // p/q/T.class => p.q
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
    private String toPackageName(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
        assert cn.endsWith(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
        int start = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
        int index = cn.lastIndexOf("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
        if (index > start) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
            return cn.substring(start, index).replace('/', '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
            return "";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
    private String toPackageName(Path path) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
        String name = path.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
        assert name.endsWith(".class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
        int index = name.lastIndexOf(File.separatorChar);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
        if (index != -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
            return name.substring(0, index).replace(File.separatorChar, '.');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
            return "";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
    private static final PerfCounter scanTime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
        = PerfCounter.newPerfCounter("jdk.module.finder.modulepath.scanTime");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
    private static final PerfCounter moduleCount
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
        = PerfCounter.newPerfCounter("jdk.module.finder.modulepath.modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
}