jdk/src/java.base/share/classes/java/lang/module/Resolver.java
author alanb
Tue, 03 May 2016 09:09:57 +0100
changeset 37779 7c84df693837
parent 36511 9d0388c6b336
child 39050 9de41b79ec7e
permissions -rw-r--r--
8154956: Module system implementation refresh (4/2016) Reviewed-by: alanb, mchung, chegar, redestad Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, erik.joelsson@oracle.com, chris.hegarty@oracle.com, peter.levart@gmail.com, sundararajan.athijegannathan@oracle.com
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) 2013, 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
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    28
import java.io.PrintStream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.lang.module.ModuleDescriptor.Requires.Modifier;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.ArrayDeque;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Collection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Deque;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.LinkedHashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Map.Entry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.StringJoiner;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    46
import jdk.internal.module.ModuleHashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * The resolver used by {@link Configuration#resolveRequires} and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * {@link Configuration#resolveRequiresAndUses}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
final class Resolver {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    private final ModuleFinder beforeFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    private final Configuration parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    private final ModuleFinder afterFinder;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    58
    private final PrintStream traceOutput;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    // maps module name to module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    private final Map<String, ModuleReference> nameToReference = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
    Resolver(ModuleFinder beforeFinder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
             Configuration parent,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    66
             ModuleFinder afterFinder,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    67
             PrintStream traceOutput) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        this.beforeFinder = beforeFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        this.parent = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        this.afterFinder = afterFinder;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    71
        this.traceOutput = traceOutput;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
     * Resolves the given named modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
     * @throws ResolutionException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    Resolver resolveRequires(Collection<String> roots) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        // create the visit stack to get us started
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        Deque<ModuleDescriptor> q = new ArrayDeque<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        for (String root : roots) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            // find root module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            ModuleReference mref = findWithBeforeFinder(root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            if (mref == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                if (parent.findModule(root).isPresent()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                    // in parent, nothing to do
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                mref = findWithAfterFinder(root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                if (mref == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                    fail("Module %s not found", root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    99
            if (isTracing()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                trace("Root module %s located", root);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   101
                mref.location().ifPresent(uri -> trace("  (%s)", uri));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            assert mref.descriptor().name().equals(root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            nameToReference.put(root, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
            q.push(mref.descriptor());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        resolve(q);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
     * Resolve all modules in the given queue. On completion the queue will be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
     * empty and any resolved modules will be added to {@code nameToReference}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
     * @return The set of module resolved by this invocation of resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
    private Set<ModuleDescriptor> resolve(Deque<ModuleDescriptor> q) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        Set<ModuleDescriptor> resolved = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        while (!q.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            ModuleDescriptor descriptor = q.poll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            assert nameToReference.containsKey(descriptor.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            // process dependences
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            for (ModuleDescriptor.Requires requires : descriptor.requires()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                String dn = requires.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                // find dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                ModuleReference mref = findWithBeforeFinder(dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                if (mref == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
                    if (parent.findModule(dn).isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                        continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                    mref = findWithAfterFinder(dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                    if (mref == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                        fail("Module %s not found, required by %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                                dn, descriptor.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                if (!nameToReference.containsKey(dn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                    nameToReference.put(dn, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                    q.offer(mref.descriptor());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                    resolved.add(mref.descriptor());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   149
                    if (isTracing()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                        trace("Module %s located, required by %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
                                dn, descriptor.name());
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   152
                        mref.location().ifPresent(uri -> trace("  (%s)", uri));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
            resolved.add(descriptor);
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
        return resolved;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
     * Augments the set of resolved modules with modules induced by the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
     * service-use relation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
    Resolver resolveUses() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        // Scan the finders for all available service provider modules. As
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        // java.base uses services then then module finders will be scanned
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        // anyway.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        Map<String, Set<ModuleReference>> availableProviders = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        for (ModuleReference mref : findAll()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            ModuleDescriptor descriptor = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            if (!descriptor.provides().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                for (String sn : descriptor.provides().keySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                    // computeIfAbsent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                    Set<ModuleReference> providers = availableProviders.get(sn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                    if (providers == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                        providers = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
                        availableProviders.put(sn, providers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                    providers.add(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        // create the visit stack
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        Deque<ModuleDescriptor> q = new ArrayDeque<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        // the initial set of modules that may use services
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        Set<ModuleDescriptor> candidateConsumers = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
        Configuration p = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        while (p != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
            candidateConsumers.addAll(p.descriptors());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
            p = p.parent().orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        for (ModuleReference mref : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            candidateConsumers.add(mref.descriptor());
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
        // Where there is a consumer of a service then resolve all modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        // that provide an implementation of that service
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
        do {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
            for (ModuleDescriptor descriptor : candidateConsumers) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                if (!descriptor.uses().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                    for (String service : descriptor.uses()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                        Set<ModuleReference> mrefs = availableProviders.get(service);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                        if (mrefs != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                            for (ModuleReference mref : mrefs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                                ModuleDescriptor provider = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                                if (!provider.equals(descriptor)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
                                    trace("Module %s provides %s, used by %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
                                            provider.name(), service, descriptor.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
                                    String pn = provider.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
                                    if (!nameToReference.containsKey(pn)) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   223
                                        if (isTracing()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   224
                                            mref.location()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   225
                                                .ifPresent(uri -> trace("  (%s)", uri));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   226
                                        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
                                        nameToReference.put(pn, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
                                        q.push(provider);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
                                }
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
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
            candidateConsumers = resolve(q);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
        } while (!candidateConsumers.isEmpty());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
        return this;
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
     * Execute post-resolution checks and returns the module graph of resolved
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
     * modules as {@code Map}. The resolved modules will be in the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
     * configuration.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   249
     *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   250
     * @param check {@true} to execute the post resolution checks
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
     */
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   252
    Map<ResolvedModule, Set<ResolvedModule>> finish(Configuration cf,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   253
                                                    boolean check)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   254
    {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   255
        if (isTracing()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   256
            trace("Result:");
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   257
            Set<String> names = nameToReference.keySet();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   258
            names.stream().sorted().forEach(name -> trace("  %s", name));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   259
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   261
        if (check) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   262
            detectCycles();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   263
            checkPlatformConstraints();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   264
            checkHashes();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   265
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
        Map<ResolvedModule, Set<ResolvedModule>> graph = makeGraph(cf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   269
        if (check) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   270
            checkExportSuppliers(graph);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   271
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        return graph;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
     * Checks the given module graph for cycles.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
     * For now the implementation is a simple depth first search on the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
     * dependency graph. We'll replace this later, maybe with Tarjan.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
    private void detectCycles() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
        visited = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
        visitPath = new LinkedHashSet<>(); // preserve insertion order
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
        for (ModuleReference mref : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            visit(mref.descriptor());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
        visited.clear();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
    // the modules that were visited
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
    private Set<ModuleDescriptor> visited;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
    // the modules in the current visit path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
    private Set<ModuleDescriptor> visitPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
    private void visit(ModuleDescriptor descriptor) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
        if (!visited.contains(descriptor)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            boolean added = visitPath.add(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
            if (!added) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
                throw new ResolutionException("Cycle detected: " +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
                        cycleAsString(descriptor));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
            for (ModuleDescriptor.Requires requires : descriptor.requires()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
                String dn = requires.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
                ModuleReference mref = nameToReference.get(dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
                if (mref != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                    ModuleDescriptor other = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                    if (other != descriptor) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                        // dependency is in this configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
                        visit(other);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
            visitPath.remove(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            visited.add(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
     * Returns a String with a list of the modules in a detected cycle.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
    private String cycleAsString(ModuleDescriptor descriptor) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
        List<ModuleDescriptor> list = new ArrayList<>(visitPath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
        list.add(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
        int index = list.indexOf(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        return list.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
                .skip(index)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                .map(ModuleDescriptor::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                .collect(Collectors.joining(" -> "));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
     * If there are platform specific modules then check that the OS name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
     * architecture and version match.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
     * @apiNote This method does not currently check if the OS matches
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   340
     *          platform specific modules in parent configurations.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   341
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
    private void checkPlatformConstraints() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   343
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
        // first module encountered that is platform specific
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
        String savedModuleName = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
        String savedOsName = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        String savedOsArch = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        String savedOsVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
        for (ModuleReference mref : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
            ModuleDescriptor descriptor = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
            String osName = descriptor.osName().orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
            String osArch = descriptor.osArch().orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
            String osVersion = descriptor.osVersion().orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
            if (osName != null || osArch != null || osVersion != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
                if (savedModuleName == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
                    savedModuleName = descriptor.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
                    savedOsName = osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
                    savedOsArch = osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
                    savedOsVersion = osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
                    boolean matches = platformMatches(osName, savedOsName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
                            && platformMatches(osArch, savedOsArch)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
                            && platformMatches(osVersion, savedOsVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
                    if (!matches) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
                        String s1 = platformAsString(savedOsName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
                                                     savedOsArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
                                                     savedOsVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
                        String s2 = platformAsString(osName, osArch, osVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
                        fail("Mismatching constraints on target platform: "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
                                + savedModuleName + ": " + s1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
                                + ", " + descriptor.name() + ": " + s2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
     * Returns true if the s1 and s2 are equal or one of them is null.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
    private boolean platformMatches(String s1, String s2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
        if (s1 == null || s2 == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
            return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
        else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
            return Objects.equals(s1, s2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
     * Return a string that encodes the OS name/arch/version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
    private String platformAsString(String osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
                                    String osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
                                    String osVersion) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
        return new StringJoiner("-")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
                .add(Objects.toString(osName, "*"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
                .add(Objects.toString(osArch, "*"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
                .add(Objects.toString(osVersion, "*"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
                .toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
     * Checks the hashes in the module descriptor to ensure that they match
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   417
     * any recorded hashes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
    private void checkHashes() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
        for (ModuleReference mref : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
            ModuleDescriptor descriptor = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   423
            // get map of module hashes
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   424
            Optional<ModuleHashes> ohashes = descriptor.hashes();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
            if (!ohashes.isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
                continue;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   427
            ModuleHashes hashes = ohashes.get();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   429
            String algorithm = hashes.algorithm();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   430
            for (String dn : hashes.names()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   431
                ModuleReference other = nameToReference.get(dn);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   432
                if (other == null) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   433
                    other = parent.findModule(dn)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   434
                            .map(ResolvedModule::reference)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   435
                            .orElse(null);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   436
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   438
                // skip checking the hash if the module has been patched
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   439
                if (other != null && !other.isPatched()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   440
                    String recordedHash = hashes.hashFor(dn);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   441
                    String actualHash = other.computeHash(algorithm);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
                    if (actualHash == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
                        fail("Unable to compute the hash of module %s", dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
                    if (!recordedHash.equals(actualHash)) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   445
                        fail("Hash of %s (%s) differs to expected hash (%s)" +
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   446
                             " recorded in %s", dn, actualHash, recordedHash,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   447
                             descriptor.name());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
                    }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   449
                }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   450
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
    }
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
     * Computes and sets the readability graph for the modules in the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
     * Resolution object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
     * The readability graph is created by propagating "requires" through the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
     * "public requires" edges of the module dependence graph. So if the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
     * dependence graph has m1 requires m2 && m2 requires public m3 then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
     * resulting readability graph will contain m1 reads m2, m1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
     * reads m3, and m2 reads m3.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
     * TODO: Use a more efficient algorithm, maybe cache the requires public
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
     *       in parent configurations.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
    private Map<ResolvedModule, Set<ResolvedModule>> makeGraph(Configuration cf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
        // the "reads" graph starts as a module dependence graph and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
        // is iteratively updated to be the readability graph
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        Map<ResolvedModule, Set<ResolvedModule>> g1 = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
        // the "requires public" graph, contains requires public edges only
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
        Map<ResolvedModule, Set<ResolvedModule>> g2 = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
        // need "requires public" from the modules in parent configurations as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
        // there may be selected modules that have a dependency on modules in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
        // the parent configuration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
        Configuration p = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
        while (p != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
            for (ModuleDescriptor descriptor : p.descriptors()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
                ResolvedModule x = p.findModule(descriptor.name()).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
                if (x == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   488
                    throw new InternalError();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
                for (ModuleDescriptor.Requires requires : descriptor.requires()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
                    if (requires.modifiers().contains(Modifier.PUBLIC)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
                        String dn = requires.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
                        ResolvedModule y = p.findModule(dn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
                        if (y == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
                            throw new InternalError(dn + " not found");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                        g2.computeIfAbsent(x, k -> new HashSet<>()).add(y);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
            p = p.parent().orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
        // populate g1 and g2 with the dependences from the selected modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
        for (ModuleReference mref : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
            ModuleDescriptor descriptor = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
            ResolvedModule x = new ResolvedModule(cf, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
            Set<ResolvedModule> reads = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
            g1.put(x, reads);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
            Set<ResolvedModule> requiresPublic = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
            g2.put(x, requiresPublic);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
            for (ModuleDescriptor.Requires requires : descriptor.requires()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
                String dn = requires.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
                ResolvedModule y;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
                ModuleReference other = nameToReference.get(dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
                if (other != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
                    y = new ResolvedModule(cf, other);  // cache?
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
                    y = parent.findModule(dn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
                    if (y == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
                        throw new InternalError("unable to find " + dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
                // m requires other => m reads other
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
                reads.add(y);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
                // m requires public other
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
                if (requires.modifiers().contains(Modifier.PUBLIC)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
                    requiresPublic.add(y);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
            // automatic modules reads all selected modules and all modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
            // in parent configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
            if (descriptor.isAutomatic()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
                String name = descriptor.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
                // reads all selected modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
                // requires public` all selected automatic modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
                for (ModuleReference mref2 : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
                    ModuleDescriptor descriptor2 = mref2.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   546
                    if (!name.equals(descriptor2.name())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
                        ResolvedModule m = new ResolvedModule(cf, mref2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
                        reads.add(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   549
                        if (descriptor2.isAutomatic())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   550
                            requiresPublic.add(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   551
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   554
                // reads all modules in parent configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
                // `requires public` all automatic modules in parent configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
                p = parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
                while (p != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
                    for (ResolvedModule m : p.modules()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   559
                        reads.add(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
                        if (m.reference().descriptor().isAutomatic())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
                            requiresPublic.add(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
                    p = p.parent().orElse(null);
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
        // Iteratively update g1 until there are no more requires public to propagate
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
        boolean changed;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
        Map<ResolvedModule, Set<ResolvedModule>> changes = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   573
        do {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
            changed = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
            for (Entry<ResolvedModule, Set<ResolvedModule>> entry : g1.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   576
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   577
                ResolvedModule m1 = entry.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
                Set<ResolvedModule> m1Reads = entry.getValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   580
                for (ResolvedModule m2 : m1Reads) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   581
                    Set<ResolvedModule> m2RequiresPublic = g2.get(m2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
                    if (m2RequiresPublic != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
                        for (ResolvedModule m3 : m2RequiresPublic) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
                            if (!m1Reads.contains(m3)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
                                // computeIfAbsent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
                                Set<ResolvedModule> s = changes.get(m1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
                                if (s == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
                                    s = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
                                    changes.put(m1, s);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
                                s.add(m3);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   593
                                changed = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   594
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   596
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
                    }
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
            if (changed) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
                for (Map.Entry<ResolvedModule, Set<ResolvedModule>> e :
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
                        changes.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
                    ResolvedModule m = e.getKey();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
                    g1.get(m).addAll(e.getValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
                changes.clear();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
        } while (changed);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   612
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
        return g1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
     * Checks the readability graph to ensure that no two modules export the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
     * same package to a module. This includes the case where module M has
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
     * a local package P and M reads another module that exports P to M.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
     * Also checks the uses/provides of module M to ensure that it reads a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
     * module that exports the package of the service type to M.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
    private void checkExportSuppliers(Map<ResolvedModule, Set<ResolvedModule>> graph) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
        for (Map.Entry<ResolvedModule, Set<ResolvedModule>> e : graph.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
            ModuleDescriptor descriptor1 = e.getKey().descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
            // the map of packages that are local or exported to descriptor1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
            Map<String, ModuleDescriptor> packageToExporter = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
            // local packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
            Set<String> packages = descriptor1.packages();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
            for (String pn : packages) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
                packageToExporter.put(pn, descriptor1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
            // descriptor1 reads descriptor2
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
            Set<ResolvedModule> reads = e.getValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
            for (ResolvedModule endpoint : reads) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
                ModuleDescriptor descriptor2 = endpoint.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
                for (ModuleDescriptor.Exports export : descriptor2.exports()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
                    if (export.isQualified()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
                        if (!export.targets().contains(descriptor1.name()))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
                            continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
                    // source is exported to descriptor2
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
                    String source = export.source();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652
                    ModuleDescriptor other
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   653
                        = packageToExporter.put(source, descriptor2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
                    if (other != null && other != descriptor2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
                        // package might be local to descriptor1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
                        if (other == descriptor1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
                            fail("Module %s contains package %s"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
                                 + ", module %s exports package %s to %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   660
                                    descriptor1.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
                                    source,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
                                    descriptor2.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
                                    source,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
                                    descriptor1.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
                        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
                            fail("Modules %s and %s export package %s to module %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
                                    descriptor2.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
                                    other.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
                                    source,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
                                    descriptor1.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   677
            // uses/provides checks not applicable to automatic modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   678
            if (!descriptor1.isAutomatic()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   680
                // uses S
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   681
                for (String service : descriptor1.uses()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   682
                    String pn = packageName(service);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   683
                    if (!packageToExporter.containsKey(pn)) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   684
                        fail("Module %s does not read a module that exports %s",
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   685
                             descriptor1.name(), pn);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   686
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   689
                // provides S
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   690
                for (Map.Entry<String, ModuleDescriptor.Provides> entry :
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   691
                        descriptor1.provides().entrySet()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   692
                    String service = entry.getKey();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   693
                    ModuleDescriptor.Provides provides = entry.getValue();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   694
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   695
                    String pn = packageName(service);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   696
                    if (!packageToExporter.containsKey(pn)) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   697
                        fail("Module %s does not read a module that exports %s",
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   698
                             descriptor1.name(), pn);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   699
                    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   700
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   701
                    for (String provider : provides.providers()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   702
                        if (!packages.contains(packageName(provider))) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   703
                            fail("Provider %s not in module %s",
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   704
                                 provider, descriptor1.name());
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   705
                        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
                }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   708
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
     * Invokes the beforeFinder to find method to find the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
    private ModuleReference findWithBeforeFinder(String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
            return beforeFinder.find(mn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
        } catch (FindException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
            // unwrap
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
            throw new ResolutionException(e.getMessage(), e.getCause());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
     * Invokes the afterFinder to find method to find the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
    private ModuleReference findWithAfterFinder(String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
            return afterFinder.find(mn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
        } catch (FindException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
            // unwrap
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
            throw new ResolutionException(e.getMessage(), e.getCause());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   740
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
     * Returns the set of all modules that are observable with the before
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
     * and after ModuleFinders.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
    private Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
            Set<ModuleReference> beforeModules = beforeFinder.findAll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
            Set<ModuleReference> afterModules = afterFinder.findAll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
            if (afterModules.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
                return beforeModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   753
            if (beforeModules.isEmpty() && parent == Configuration.empty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   754
                return afterModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   755
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   756
            Set<ModuleReference> result = new HashSet<>(beforeModules);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
            for (ModuleReference mref : afterModules) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
                String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
                if (!beforeFinder.find(name).isPresent()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
                        && !parent.findModule(name).isPresent())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
                    result.add(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
            return result;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
        } catch (FindException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   767
            // unwrap
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   768
            throw new ResolutionException(e.getMessage(), e.getCause());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   769
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   770
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   771
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   772
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   773
     * Returns the package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   774
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   775
    private static String packageName(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   776
        int index = cn.lastIndexOf(".");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   777
        return (index == -1) ? "" : cn.substring(0, index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   778
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   779
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   780
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   781
     * Throw ResolutionException with the given format string and arguments
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   782
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   783
    private static void fail(String fmt, Object ... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   784
        String msg = String.format(fmt, args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   785
        throw new ResolutionException(msg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   786
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   787
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   788
    /**
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   789
     * Tracing support
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   790
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   791
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   792
    private boolean isTracing() {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   793
        return traceOutput != null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   794
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   795
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   796
    private void trace(String fmt, Object ... args) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   797
        if (traceOutput != null) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   798
            traceOutput.format("[Resolver] " + fmt, args);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   799
            traceOutput.println();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   800
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   801
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   802
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   803
}