jdk/src/java.base/share/classes/jdk/internal/module/SystemModuleFinders.java
author duke
Thu, 24 Aug 2017 16:37:10 +0200
changeset 46188 4586bc5d28d1
parent 44359 jdk/src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java@c6761862ca0b
parent 46096 jdk/src/java.base/share/classes/jdk/internal/module/SystemModuleFinder.java@62c77b334012
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     1
/*
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     4
 *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    10
 *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    15
 * accompanied this code).
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    16
 *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    20
 *
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    23
 * questions.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    24
 */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    25
package jdk.internal.module;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    26
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    27
import java.io.ByteArrayInputStream;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    28
import java.io.IOException;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    29
import java.io.InputStream;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    30
import java.io.UncheckedIOException;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleDescriptor;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleFinder;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleReader;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    34
import java.lang.module.ModuleReference;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    35
import java.lang.reflect.Constructor;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    36
import java.net.URI;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    37
import java.net.URLConnection;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    38
import java.nio.ByteBuffer;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    39
import java.nio.file.Files;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    40
import java.nio.file.Path;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    41
import java.nio.file.Paths;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    42
import java.security.AccessController;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    43
import java.security.PrivilegedAction;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    44
import java.util.ArrayDeque;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    45
import java.util.Collections;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    46
import java.util.Deque;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    47
import java.util.HashMap;
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    48
import java.util.HashSet;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    49
import java.util.Iterator;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    50
import java.util.Map;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    51
import java.util.Objects;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    52
import java.util.Optional;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    53
import java.util.Set;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    54
import java.util.Spliterator;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    55
import java.util.function.Consumer;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    56
import java.util.function.Supplier;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    57
import java.util.stream.Stream;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    58
import java.util.stream.StreamSupport;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    59
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    60
import jdk.internal.jimage.ImageLocation;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    61
import jdk.internal.jimage.ImageReader;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    62
import jdk.internal.jimage.ImageReaderFactory;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    63
import jdk.internal.misc.JavaNetUriAccess;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    64
import jdk.internal.misc.SharedSecrets;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    65
import jdk.internal.module.ModuleHashes.HashSupplier;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    66
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    67
/**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    68
 * The factory for SystemModules objects and for creating ModuleFinder objects
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    69
 * that find modules in the runtime image.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    70
 *
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    71
 * This class supports initializing the module system when the runtime is an
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    72
 * images build, an exploded build, or an images build with java.base patched
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    73
 * by an exploded java.base. It also supports a testing mode that re-parses
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    74
 * the module-info.class resources in the run-time image.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    75
 */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    76
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    77
public final class SystemModuleFinders {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    78
    private static final JavaNetUriAccess JNUA = SharedSecrets.getJavaNetUriAccess();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    79
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    80
    private static final boolean USE_FAST_PATH;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    81
    static {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    82
        String value = System.getProperty("jdk.system.module.finder.disableFastPath");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    83
        if (value == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    84
            USE_FAST_PATH = true;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    85
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    86
            USE_FAST_PATH = (value.length() > 0) && !Boolean.parseBoolean(value);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    87
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    88
    }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    89
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    90
    // cached ModuleFinder returned from ofSystem
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    91
    private static volatile ModuleFinder cachedSystemModuleFinder;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    92
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    93
    private SystemModuleFinders() { }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    94
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    95
    /**
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    96
     * Returns the SystemModules object to reconstitute all modules. Returns
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    97
     * null if this is an exploded build or java.base is patched by an exploded
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
    98
     * build.
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
    99
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   100
    static SystemModules allSystemModules() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   101
        if (USE_FAST_PATH) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   102
            return SystemModulesMap.allSystemModules();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   103
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   104
            return null;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42752
diff changeset
   105
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42752
diff changeset
   106
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42752
diff changeset
   107
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   108
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   109
     * Returns a SystemModules object to reconstitute the modules for the
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   110
     * given initial module. If the initial module is null then return the
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   111
     * SystemModules object to reconstitute the default modules.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   112
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   113
     * Return null if there is no SystemModules class for the initial module,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   114
     * this is an exploded build, or java.base is patched by an exploded build.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   115
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   116
    static SystemModules systemModules(String initialModule) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   117
        if (USE_FAST_PATH) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   118
            if (initialModule == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   119
                return SystemModulesMap.defaultSystemModules();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   120
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   121
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   122
            String[] initialModules = SystemModulesMap.moduleNames();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   123
            for (int i = 0; i < initialModules.length; i++) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   124
                String moduleName = initialModules[i];
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   125
                if (initialModule.equals(moduleName)) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   126
                    String cn = SystemModulesMap.classNames()[i];
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   127
                    try {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   128
                        // one-arg Class.forName as java.base may not be defined
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   129
                        Constructor<?> ctor = Class.forName(cn).getConstructor();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   130
                        return (SystemModules) ctor.newInstance();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   131
                    } catch (Exception e) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   132
                        throw new InternalError(e);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   133
                    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   134
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   135
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   136
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   137
        return null;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   138
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   139
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   140
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   141
     * Returns a ModuleFinder that is backed by the given SystemModules object.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   142
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   143
     * @apiNote The returned ModuleFinder is thread safe.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   144
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   145
    static ModuleFinder of(SystemModules systemModules) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   146
        ModuleDescriptor[] descriptors = systemModules.moduleDescriptors();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   147
        ModuleTarget[] targets = systemModules.moduleTargets();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   148
        ModuleHashes[] recordedHashes = systemModules.moduleHashes();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   149
        ModuleResolution[] moduleResolutions = systemModules.moduleResolutions();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   150
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   151
        int moduleCount = descriptors.length;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   152
        ModuleReference[] mrefs = new ModuleReference[moduleCount];
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   153
        @SuppressWarnings(value = {"rawtypes", "unchecked"})
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   154
        Map.Entry<String, ModuleReference>[] map
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   155
            = (Map.Entry<String, ModuleReference>[])new Map.Entry[moduleCount];
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   156
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   157
        Map<String, byte[]> nameToHash = generateNameToHash(recordedHashes);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   158
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   159
        for (int i = 0; i < moduleCount; i++) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   160
            String name = descriptors[i].name();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   161
            HashSupplier hashSupplier = hashSupplier(nameToHash, name);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   162
            ModuleReference mref = toModuleReference(descriptors[i],
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   163
                                                     targets[i],
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   164
                                                     recordedHashes[i],
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   165
                                                     hashSupplier,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   166
                                                     moduleResolutions[i]);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   167
            mrefs[i] = mref;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   168
            map[i] = Map.entry(name, mref);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   169
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   170
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   171
        return new SystemModuleFinder(mrefs, map);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   172
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   173
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   174
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   175
     * Returns the ModuleFinder to find all system modules. Supports both
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   176
     * images and exploded builds.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   177
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   178
     * @apiNote Used by ModuleFinder.ofSystem()
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   179
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   180
    public static ModuleFinder ofSystem() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   181
        ModuleFinder finder = cachedSystemModuleFinder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   182
        if (finder != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   183
            return finder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   184
        }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   185
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   186
        // probe to see if this is an images build
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   187
        String home = System.getProperty("java.home");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   188
        Path modules = Paths.get(home, "lib", "modules");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   189
        if (Files.isRegularFile(modules)) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   190
            if (USE_FAST_PATH) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   191
                SystemModules systemModules = allSystemModules();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   192
                if (systemModules != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   193
                    finder = of(systemModules);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   194
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   195
            }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   196
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   197
            // fall back to parsing the module-info.class files in image
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   198
            if (finder == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   199
                finder = ofModuleInfos();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   200
            }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   201
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   202
            cachedSystemModuleFinder = finder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   203
            return finder;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   204
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   205
        }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   206
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   207
        // exploded build (do not cache module finder)
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   208
        Path dir = Paths.get(home, "modules");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   209
        if (!Files.isDirectory(dir))
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   210
            throw new InternalError("Unable to detect the run-time image");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   211
        ModuleFinder f = ModulePath.of(ModuleBootstrap.patcher(), dir);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   212
        return new ModuleFinder() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   213
            @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   214
            public Optional<ModuleReference> find(String name) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   215
                PrivilegedAction<Optional<ModuleReference>> pa = () -> f.find(name);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   216
                return AccessController.doPrivileged(pa);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   217
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   218
            @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   219
            public Set<ModuleReference> findAll() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   220
                PrivilegedAction<Set<ModuleReference>> pa = f::findAll;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   221
                return AccessController.doPrivileged(pa);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   222
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   223
        };
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   224
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   225
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   226
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   227
     * Parses the module-info.class of all module in the runtime image and
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   228
     * returns a ModuleFinder to find the modules.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   229
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   230
     * @apiNote The returned ModuleFinder is thread safe.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   231
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   232
    private static ModuleFinder ofModuleInfos() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   233
        // parse the module-info.class in every module
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   234
        Map<String, ModuleInfo.Attributes> nameToAttributes = new HashMap<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   235
        Map<String, byte[]> nameToHash = new HashMap<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   236
        ImageReader reader = SystemImage.reader();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   237
        for (String mn : reader.getModuleNames()) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   238
            ImageLocation loc = reader.findLocation(mn, "module-info.class");
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   239
            ModuleInfo.Attributes attrs
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   240
                = ModuleInfo.read(reader.getResourceBuffer(loc), null);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   241
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   242
            nameToAttributes.put(mn, attrs);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   243
            ModuleHashes hashes = attrs.recordedHashes();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   244
            if (hashes != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   245
                for (String name : hashes.names()) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   246
                    nameToHash.computeIfAbsent(name, k -> hashes.hashFor(name));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   247
                }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   248
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   249
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   250
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   251
        // create a ModuleReference for each module
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   252
        Set<ModuleReference> mrefs = new HashSet<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   253
        Map<String, ModuleReference> nameToModule = new HashMap<>();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   254
        for (Map.Entry<String, ModuleInfo.Attributes> e : nameToAttributes.entrySet()) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   255
            String mn = e.getKey();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   256
            ModuleInfo.Attributes attrs = e.getValue();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   257
            HashSupplier hashSupplier = hashSupplier(nameToHash, mn);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   258
            ModuleReference mref = toModuleReference(attrs.descriptor(),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   259
                                                     attrs.target(),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   260
                                                     attrs.recordedHashes(),
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   261
                                                     hashSupplier,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   262
                                                     attrs.moduleResolution());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   263
            mrefs.add(mref);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   264
            nameToModule.put(mn, mref);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   265
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   266
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   267
        return new SystemModuleFinder(mrefs, nameToModule);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   268
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   269
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   270
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   271
     * A ModuleFinder that finds module in an array or set of modules.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   272
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   273
    private static class SystemModuleFinder implements ModuleFinder {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   274
        final Set<ModuleReference> mrefs;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   275
        final Map<String, ModuleReference> nameToModule;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   276
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   277
        SystemModuleFinder(ModuleReference[] array,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   278
                           Map.Entry<String, ModuleReference>[] map) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   279
            this.mrefs = Set.of(array);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   280
            this.nameToModule = Map.ofEntries(map);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   281
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   282
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   283
        SystemModuleFinder(Set<ModuleReference> mrefs,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   284
                           Map<String, ModuleReference> nameToModule) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   285
            this.mrefs = Collections.unmodifiableSet(mrefs);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   286
            this.nameToModule = Collections.unmodifiableMap(nameToModule);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   287
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   288
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   289
        @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   290
        public Optional<ModuleReference> find(String name) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   291
            Objects.requireNonNull(name);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   292
            return Optional.ofNullable(nameToModule.get(name));
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   293
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   294
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   295
        @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   296
        public Set<ModuleReference> findAll() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   297
            return mrefs;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   298
        }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   299
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   300
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   301
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   302
     * Creates a ModuleReference to the system module.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   303
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   304
    static ModuleReference toModuleReference(ModuleDescriptor descriptor,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   305
                                             ModuleTarget target,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   306
                                             ModuleHashes recordedHashes,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   307
                                             HashSupplier hasher,
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   308
                                             ModuleResolution mres) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   309
        String mn = descriptor.name();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   310
        URI uri = JNUA.create("jrt", "/".concat(mn));
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   311
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   312
        Supplier<ModuleReader> readerSupplier = new Supplier<>() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   313
            @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   314
            public ModuleReader get() {
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   315
                return new SystemModuleReader(mn, uri);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   316
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   317
        };
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   318
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   319
        ModuleReference mref = new ModuleReferenceImpl(descriptor,
44359
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   320
                                                       uri,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   321
                                                       readerSupplier,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   322
                                                       null,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   323
                                                       target,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   324
                                                       recordedHashes,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   325
                                                       hasher,
c6761862ca0b 8174823: Module system implementation refresh (3/2017)
alanb
parents: 43712
diff changeset
   326
                                                       mres);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   327
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   328
        // may need a reference to a patched module if --patch-module specified
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   329
        mref = ModuleBootstrap.patcher().patchIfNeeded(mref);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   330
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   331
        return mref;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   332
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   333
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   334
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   335
     * Generates a map of module name to hash value.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   336
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   337
    static Map<String, byte[]> generateNameToHash(ModuleHashes[] recordedHashes) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   338
        Map<String, byte[]> nameToHash = null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   339
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   340
        boolean secondSeen = false;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   341
        // record the hashes to build HashSupplier
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   342
        for (ModuleHashes mh : recordedHashes) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   343
            if (mh != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   344
                // if only one module contain ModuleHashes, use it
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   345
                if (nameToHash == null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   346
                    nameToHash = mh.hashes();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   347
                } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   348
                    if (!secondSeen) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   349
                        nameToHash = new HashMap<>(nameToHash);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   350
                        secondSeen = true;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   351
                    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   352
                    nameToHash.putAll(mh.hashes());
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   353
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   354
            }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   355
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   356
        return (nameToHash != null) ? nameToHash : Collections.emptyMap();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   357
    }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   358
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   359
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   360
     * Returns a HashSupplier that returns the hash of the given module.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   361
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   362
    static HashSupplier hashSupplier(Map<String, byte[]> nameToHash, String name) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   363
        byte[] hash = nameToHash.get(name);
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   364
        if (hash != null) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   365
            // avoid lambda here
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   366
            return new HashSupplier() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   367
                @Override
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   368
                public byte[] generate(String algorithm) {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   369
                    return hash;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   370
                }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   371
            };
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   372
        } else {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   373
            return null;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   374
        }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   375
    }
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   376
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   377
    /**
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   378
     * Holder class for the ImageReader
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   379
     *
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   380
     * @apiNote This class must be loaded before a security manager is set.
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   381
     */
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   382
    private static class SystemImage {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   383
        static final ImageReader READER = ImageReaderFactory.getImageReader();
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   384
        static ImageReader reader() {
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   385
            return READER;
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   386
        }
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   387
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   388
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   389
    /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   390
     * A ModuleReader for reading resources from a module linked into the
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   391
     * run-time image.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   392
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   393
    private static class SystemModuleReader implements ModuleReader {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   394
        private final String module;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   395
        private volatile boolean closed;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   396
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   397
        /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   398
         * If there is a security manager set then check permission to
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   399
         * connect to the run-time image.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   400
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   401
        private static void checkPermissionToConnect(URI uri) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   402
            SecurityManager sm = System.getSecurityManager();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   403
            if (sm != null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   404
                try {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   405
                    URLConnection uc = uri.toURL().openConnection();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   406
                    sm.checkPermission(uc.getPermission());
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   407
                } catch (IOException ioe) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   408
                    throw new UncheckedIOException(ioe);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   409
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   410
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   411
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   412
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   413
        SystemModuleReader(String module, URI uri) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   414
            checkPermissionToConnect(uri);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   415
            this.module = module;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   416
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   417
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   418
        /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   419
         * Returns the ImageLocation for the given resource, {@code null}
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   420
         * if not found.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   421
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   422
        private ImageLocation findImageLocation(String name) throws IOException {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   423
            Objects.requireNonNull(name);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   424
            if (closed)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   425
                throw new IOException("ModuleReader is closed");
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42752
diff changeset
   426
            ImageReader imageReader = SystemImage.reader();
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   427
            if (imageReader != null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   428
                return imageReader.findLocation(module, name);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   429
            } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   430
                // not an images build
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   431
                return null;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   432
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   433
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   434
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   435
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   436
        public Optional<URI> find(String name) throws IOException {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   437
            ImageLocation location = findImageLocation(name);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   438
            if (location != null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   439
                URI u = URI.create("jrt:/" + module + "/" + name);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   440
                return Optional.of(u);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   441
            } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   442
                return Optional.empty();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   443
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   444
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   445
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   446
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   447
        public Optional<InputStream> open(String name) throws IOException {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   448
            return read(name).map(this::toInputStream);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   449
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   450
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   451
        private InputStream toInputStream(ByteBuffer bb) { // ## -> ByteBuffer?
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   452
            try {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   453
                int rem = bb.remaining();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   454
                byte[] bytes = new byte[rem];
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   455
                bb.get(bytes);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   456
                return new ByteArrayInputStream(bytes);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   457
            } finally {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   458
                release(bb);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   459
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   460
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   461
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   462
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   463
        public Optional<ByteBuffer> read(String name) throws IOException {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   464
            ImageLocation location = findImageLocation(name);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   465
            if (location != null) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42752
diff changeset
   466
                return Optional.of(SystemImage.reader().getResourceBuffer(location));
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   467
            } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   468
                return Optional.empty();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   469
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   470
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   471
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   472
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   473
        public void release(ByteBuffer bb) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   474
            Objects.requireNonNull(bb);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   475
            ImageReader.releaseByteBuffer(bb);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   476
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   477
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   478
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   479
        public Stream<String> list() throws IOException {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   480
            if (closed)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   481
                throw new IOException("ModuleReader is closed");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   482
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   483
            Spliterator<String> s = new ModuleContentSpliterator(module);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   484
            return StreamSupport.stream(s, false);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   485
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   486
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   487
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   488
        public void close() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   489
            // nothing else to do
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   490
            closed = true;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   491
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   492
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   493
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   494
    /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   495
     * A Spliterator for traversing the resources of a module linked into the
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   496
     * run-time image.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   497
     */
46096
62c77b334012 8185853: Generate readability graph at link time and other startup improvements
alanb
parents: 44359
diff changeset
   498
    private static class ModuleContentSpliterator implements Spliterator<String> {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   499
        final String moduleRoot;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   500
        final Deque<ImageReader.Node> stack;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   501
        Iterator<ImageReader.Node> iterator;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   502
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   503
        ModuleContentSpliterator(String module) throws IOException {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   504
            moduleRoot = "/modules/" + module;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   505
            stack = new ArrayDeque<>();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   506
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   507
            // push the root node to the stack to get started
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42752
diff changeset
   508
            ImageReader.Node dir = SystemImage.reader().findNode(moduleRoot);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   509
            if (dir == null || !dir.isDirectory())
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   510
                throw new IOException(moduleRoot + " not a directory");
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   511
            stack.push(dir);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   512
            iterator = Collections.emptyIterator();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   513
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   514
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   515
        /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   516
         * Returns the name of the next non-directory node or {@code null} if
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   517
         * there are no remaining nodes to visit.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   518
         */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   519
        private String next() throws IOException {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   520
            for (;;) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   521
                while (iterator.hasNext()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   522
                    ImageReader.Node node = iterator.next();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   523
                    String name = node.getName();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   524
                    if (node.isDirectory()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   525
                        // build node
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42752
diff changeset
   526
                        ImageReader.Node dir = SystemImage.reader().findNode(name);
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   527
                        assert dir.isDirectory();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   528
                        stack.push(dir);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   529
                    } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   530
                        // strip /modules/$MODULE/ prefix
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   531
                        return name.substring(moduleRoot.length() + 1);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   532
                    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   533
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   534
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   535
                if (stack.isEmpty()) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   536
                    return null;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   537
                } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   538
                    ImageReader.Node dir = stack.poll();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   539
                    assert dir.isDirectory();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   540
                    iterator = dir.getChildren().iterator();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   541
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   542
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   543
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   544
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   545
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   546
        public boolean tryAdvance(Consumer<? super String> action) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   547
            String next;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   548
            try {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   549
                next = next();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   550
            } catch (IOException ioe) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   551
                throw new UncheckedIOException(ioe);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   552
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   553
            if (next != null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   554
                action.accept(next);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   555
                return true;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   556
            } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   557
                return false;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   558
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   559
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   560
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   561
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   562
        public Spliterator<String> trySplit() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   563
            return null;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   564
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   565
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   566
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   567
        public int characteristics() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   568
            return Spliterator.DISTINCT + Spliterator.NONNULL + Spliterator.IMMUTABLE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   569
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   570
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   571
        @Override
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   572
        public long estimateSize() {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   573
            return Long.MAX_VALUE;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   574
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   575
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents:
diff changeset
   576
}