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