jdk/src/java.base/share/classes/java/lang/module/Resolver.java
author alanb
Fri, 16 Dec 2016 06:19:16 +0000
changeset 42703 20c39ea4a507
parent 42338 a60f280f803c
child 43712 5dfd0950317c
permissions -rw-r--r--
8170987: Module system implementation refresh (12/2016) 8170859: Run time and tool support for ModuleResolution Reviewed-by: redestad, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, chris.hegarty@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    28
import java.io.PrintStream;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    29
import java.lang.module.ModuleDescriptor.Provides;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.ModuleDescriptor.Requires.Modifier;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    31
import java.lang.reflect.Layer;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.ArrayDeque;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.ArrayList;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    34
import java.util.Arrays;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.Collection;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Deque;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.LinkedHashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.StringJoiner;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    48
import jdk.internal.module.ModuleHashes;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    49
import jdk.internal.module.ModuleReferenceImpl;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 * The resolver used by {@link Configuration#resolveRequires} and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * {@link Configuration#resolveRequiresAndUses}.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    54
 *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    55
 * @implNote The resolver is used at VM startup and so deliberately avoids
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    56
 * using lambda and stream usages in code paths used during startup.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
final class Resolver {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    private final ModuleFinder beforeFinder;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    62
    private final List<Configuration> parents;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    private final ModuleFinder afterFinder;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    64
    private final PrintStream traceOutput;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    // maps module name to module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    private final Map<String, ModuleReference> nameToReference = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    Resolver(ModuleFinder beforeFinder,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    71
             List<Configuration> parents,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    72
             ModuleFinder afterFinder,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    73
             PrintStream traceOutput) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        this.beforeFinder = beforeFinder;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    75
        this.parents = parents;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        this.afterFinder = afterFinder;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    77
        this.traceOutput = traceOutput;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
     * Resolves the given named modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
     * @throws ResolutionException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    Resolver resolveRequires(Collection<String> roots) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        // create the visit stack to get us started
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        Deque<ModuleDescriptor> q = new ArrayDeque<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        for (String root : roots) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            // find root module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            ModuleReference mref = findWithBeforeFinder(root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            if (mref == null) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    95
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
    96
                if (findInParent(root) != null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                    // in parent, nothing to do
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   100
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                mref = findWithAfterFinder(root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                if (mref == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
                    fail("Module %s not found", root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   107
            if (isTracing()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                trace("Root module %s located", root);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   109
                mref.location().ifPresent(uri -> trace("  (%s)", uri));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            assert mref.descriptor().name().equals(root);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            nameToReference.put(root, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            q.push(mref.descriptor());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        resolve(q);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        return this;
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
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
     * Resolve all modules in the given queue. On completion the queue will be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
     * empty and any resolved modules will be added to {@code nameToReference}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
     * @return The set of module resolved by this invocation of resolve
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    private Set<ModuleDescriptor> resolve(Deque<ModuleDescriptor> q) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        Set<ModuleDescriptor> resolved = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        while (!q.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            ModuleDescriptor descriptor = q.poll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            assert nameToReference.containsKey(descriptor.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            // process dependences
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            for (ModuleDescriptor.Requires requires : descriptor.requires()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   137
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   138
                // only required at compile-time
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   139
                if (requires.modifiers().contains(Modifier.STATIC))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   140
                    continue;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   141
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                String dn = requires.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                // find dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                ModuleReference mref = findWithBeforeFinder(dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                if (mref == null) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   147
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   148
                    if (findInParent(dn) != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   149
                        // dependence is in parent
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                        continue;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   151
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
                    mref = findWithAfterFinder(dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                    if (mref == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
                        fail("Module %s not found, required by %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                                dn, descriptor.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                if (!nameToReference.containsKey(dn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                    nameToReference.put(dn, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                    q.offer(mref.descriptor());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                    resolved.add(mref.descriptor());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   165
                    if (isTracing()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
                        trace("Module %s located, required by %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
                                dn, descriptor.name());
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   168
                        mref.location().ifPresent(uri -> trace("  (%s)", uri));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
                    }
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            resolved.add(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        return resolved;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
     * Augments the set of resolved modules with modules induced by the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
     * service-use relation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
    Resolver resolveUses() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        // Scan the finders for all available service provider modules. As
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        // java.base uses services then then module finders will be scanned
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        // anyway.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        Map<String, Set<ModuleReference>> availableProviders = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
        for (ModuleReference mref : findAll()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
            ModuleDescriptor descriptor = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
            if (!descriptor.provides().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   194
                for (Provides provides :  descriptor.provides()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   195
                    String sn = provides.service();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   196
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
                    // computeIfAbsent
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                    Set<ModuleReference> providers = availableProviders.get(sn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
                    if (providers == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                        providers = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                        availableProviders.put(sn, providers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                    providers.add(mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        // create the visit stack
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        Deque<ModuleDescriptor> q = new ArrayDeque<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
        // the initial set of modules that may use services
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   213
        Set<ModuleDescriptor> initialConsumers;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   214
        if (Layer.boot() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   215
            initialConsumers = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   216
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   217
            initialConsumers = parents.stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   218
                    .flatMap(Configuration::configurations)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   219
                    .distinct()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   220
                    .flatMap(c -> c.descriptors().stream())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   221
                    .collect(Collectors.toSet());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        for (ModuleReference mref : nameToReference.values()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   224
            initialConsumers.add(mref.descriptor());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
        // Where there is a consumer of a service then resolve all modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        // that provide an implementation of that service
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   229
        Set<ModuleDescriptor> candidateConsumers = initialConsumers;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        do {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
            for (ModuleDescriptor descriptor : candidateConsumers) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                if (!descriptor.uses().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                    for (String service : descriptor.uses()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                        Set<ModuleReference> mrefs = availableProviders.get(service);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                        if (mrefs != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                            for (ModuleReference mref : mrefs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                                ModuleDescriptor provider = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                                if (!provider.equals(descriptor)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                                    trace("Module %s provides %s, used by %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                                            provider.name(), service, descriptor.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
                                    String pn = provider.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
                                    if (!nameToReference.containsKey(pn)) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   245
                                        if (isTracing()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   246
                                            mref.location()
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   247
                                                .ifPresent(uri -> trace("  (%s)", uri));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   248
                                        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
                                        nameToReference.put(pn, mref);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
                                        q.push(provider);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
                                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                }
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
            candidateConsumers = resolve(q);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
        } while (!candidateConsumers.isEmpty());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
     * Execute post-resolution checks and returns the module graph of resolved
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
     * modules as {@code Map}. The resolved modules will be in the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
     * configuration.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   270
     *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   271
     * @param check {@true} to execute the post resolution checks
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
     */
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   273
    Map<ResolvedModule, Set<ResolvedModule>> finish(Configuration cf,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   274
                                                    boolean check)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   275
    {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   276
        if (isTracing()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   277
            trace("Result:");
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   278
            Set<String> names = nameToReference.keySet();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   279
            names.stream().sorted().forEach(name -> trace("  %s", name));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   280
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   282
        if (check) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   283
            detectCycles();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   284
            checkPlatformConstraints();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   285
            checkHashes();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   286
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
        Map<ResolvedModule, Set<ResolvedModule>> graph = makeGraph(cf);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   290
        if (check) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   291
            checkExportSuppliers(graph);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   292
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        return graph;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
     * Checks the given module graph for cycles.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
     * For now the implementation is a simple depth first search on the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
     * dependency graph. We'll replace this later, maybe with Tarjan.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
    private void detectCycles() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
        visited = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
        visitPath = new LinkedHashSet<>(); // preserve insertion order
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        for (ModuleReference mref : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            visit(mref.descriptor());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
        visited.clear();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
    // the modules that were visited
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
    private Set<ModuleDescriptor> visited;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
    // the modules in the current visit path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
    private Set<ModuleDescriptor> visitPath;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
    private void visit(ModuleDescriptor descriptor) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
        if (!visited.contains(descriptor)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
            boolean added = visitPath.add(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
            if (!added) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
                throw new ResolutionException("Cycle detected: " +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
                        cycleAsString(descriptor));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
            for (ModuleDescriptor.Requires requires : descriptor.requires()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
                String dn = requires.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
                ModuleReference mref = nameToReference.get(dn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
                if (mref != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                    ModuleDescriptor other = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                    if (other != descriptor) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
                        // dependency is in this configuration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
                        visit(other);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            visitPath.remove(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
            visited.add(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   339
        }
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
     * Returns a String with a list of the modules in a detected cycle.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   344
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   345
    private String cycleAsString(ModuleDescriptor descriptor) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
        List<ModuleDescriptor> list = new ArrayList<>(visitPath);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        list.add(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
        int index = list.indexOf(descriptor);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
        return list.stream()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
                .skip(index)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
                .map(ModuleDescriptor::name)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
                .collect(Collectors.joining(" -> "));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
     * If there are platform specific modules then check that the OS name,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
     * architecture and version match.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
     * @apiNote This method does not currently check if the OS matches
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
     *          platform specific modules in parent configurations.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
    private void checkPlatformConstraints() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
        // first module encountered that is platform specific
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
        String savedModuleName = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
        String savedOsName = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
        String savedOsArch = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
        String savedOsVersion = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
        for (ModuleReference mref : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
            ModuleDescriptor descriptor = mref.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
            String osName = descriptor.osName().orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
            String osArch = descriptor.osArch().orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
            String osVersion = descriptor.osVersion().orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
            if (osName != null || osArch != null || osVersion != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
                if (savedModuleName == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
                    savedModuleName = descriptor.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
                    savedOsName = osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
                    savedOsArch = osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                    savedOsVersion = osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
                } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
                    boolean matches = platformMatches(osName, savedOsName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
                            && platformMatches(osArch, savedOsArch)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                            && platformMatches(osVersion, savedOsVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
                    if (!matches) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                        String s1 = platformAsString(savedOsName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                                                     savedOsArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                                                     savedOsVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
                        String s2 = platformAsString(osName, osArch, osVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
                        fail("Mismatching constraints on target platform: "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
                                + savedModuleName + ": " + s1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                                + ", " + descriptor.name() + ": " + s2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
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
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
     * Returns true if the s1 and s2 are equal or one of them is null.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
    private boolean platformMatches(String s1, String s2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
        if (s1 == null || s2 == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
            return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
        else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
            return Objects.equals(s1, s2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
    }
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
     * Return a string that encodes the OS name/arch/version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
    private String platformAsString(String osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
                                    String osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
                                    String osVersion) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
        return new StringJoiner("-")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
                .add(Objects.toString(osName, "*"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
                .add(Objects.toString(osArch, "*"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
                .add(Objects.toString(osVersion, "*"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
                .toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
     * Checks the hashes in the module descriptor to ensure that they match
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   438
     * any recorded hashes.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
    private void checkHashes() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
        for (ModuleReference mref : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   443
            // get the recorded hashes, if any
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   444
            if (!(mref instanceof ModuleReferenceImpl))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
                continue;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   446
            ModuleHashes hashes = ((ModuleReferenceImpl)mref).recordedHashes();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   447
            if (hashes == null)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   448
                continue;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   450
            ModuleDescriptor descriptor = mref.descriptor();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   451
            String algorithm = hashes.algorithm();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   452
            for (String dn : hashes.names()) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   453
                ModuleReference mref2 = nameToReference.get(dn);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   454
                if (mref2 == null) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   455
                    ResolvedModule resolvedModule = findInParent(dn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   456
                    if (resolvedModule != null)
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   457
                        mref2 = resolvedModule.reference();
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   458
                }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   459
                if (mref2 == null)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   460
                    continue;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   461
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   462
                if (!(mref2 instanceof ModuleReferenceImpl)) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   463
                    fail("Unable to compute the hash of module %s", dn);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   464
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   466
                // skip checking the hash if the module has been patched
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   467
                ModuleReferenceImpl other = (ModuleReferenceImpl)mref2;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   468
                if (other != null && !other.isPatched()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   469
                    byte[] recordedHash = hashes.hashFor(dn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   470
                    byte[] actualHash = other.computeHash(algorithm);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
                    if (actualHash == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
                        fail("Unable to compute the hash of module %s", dn);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   473
                    if (!Arrays.equals(recordedHash, actualHash)) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   474
                        fail("Hash of %s (%s) differs to expected hash (%s)" +
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   475
                             " recorded in %s", dn, toHexString(actualHash),
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   476
                             toHexString(recordedHash), descriptor.name());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
                    }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   478
                }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   479
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   484
    private static String toHexString(byte[] ba) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   485
        StringBuilder sb = new StringBuilder(ba.length * 2);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   486
        for (byte b: ba) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   487
            sb.append(String.format("%02x", b & 0xff));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   488
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   489
        return sb.toString();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   490
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   491
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
    /**
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   494
     * Computes the readability graph for the modules in the given Configuration.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
     * The readability graph is created by propagating "requires" through the
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   497
     * "requires transitive" edges of the module dependence graph. So if the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   498
     * module dependence graph has m1 requires m2 && m2 requires transitive m3
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   499
     * then the resulting readability graph will contain m1 reads m2, m1 reads m3,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   500
     * and m2 reads m3.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
    private Map<ResolvedModule, Set<ResolvedModule>> makeGraph(Configuration cf) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   504
        // initial capacity of maps to avoid resizing
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   505
        int capacity = 1 + (4 * nameToReference.size())/ 3;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   506
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
        // the "reads" graph starts as a module dependence graph and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
        // is iteratively updated to be the readability graph
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   509
        Map<ResolvedModule, Set<ResolvedModule>> g1 = new HashMap<>(capacity);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   511
        // the "requires transitive" graph, contains requires transitive edges only
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   512
        Map<ResolvedModule, Set<ResolvedModule>> g2;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   514
        // need "requires transitive" from the modules in parent configurations
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   515
        // as there may be selected modules that have a dependency on modules in
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   516
        // the parent configuration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   517
        if (Layer.boot() == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   518
            g2 = new HashMap<>(capacity);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   519
        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   520
            g2 = parents.stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   521
                .flatMap(Configuration::configurations)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   522
                .distinct()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   523
                .flatMap(c ->
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   524
                    c.modules().stream().flatMap(m1 ->
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   525
                        m1.descriptor().requires().stream()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   526
                            .filter(r -> r.modifiers().contains(Modifier.TRANSITIVE))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   527
                            .flatMap(r -> {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   528
                                Optional<ResolvedModule> m2 = c.findModule(r.name());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   529
                                assert m2.isPresent()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   530
                                        || r.modifiers().contains(Modifier.STATIC);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   531
                                return m2.stream();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   532
                            })
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   533
                            .map(m2 -> Map.entry(m1, m2))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   534
                    )
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   535
                )
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   536
                // stream of m1->m2
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   537
                .collect(Collectors.groupingBy(Map.Entry::getKey,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   538
                        HashMap::new,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   539
                        Collectors.mapping(Map.Entry::getValue, Collectors.toSet())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   540
            ));
36511
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
        // populate g1 and g2 with the dependences from the selected modules
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   544
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   545
        Map<String, ResolvedModule> nameToResolved = new HashMap<>(capacity);
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   546
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   547
        for (ModuleReference mref : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   548
            ModuleDescriptor descriptor = mref.descriptor();
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   549
            String name = descriptor.name();
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   550
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   551
            ResolvedModule m1 = computeIfAbsent(nameToResolved, name, cf, mref);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   552
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   553
            Set<ResolvedModule> reads = new HashSet<>();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   554
            Set<ResolvedModule> requiresTransitive = new HashSet<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   555
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   556
            for (ModuleDescriptor.Requires requires : descriptor.requires()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   557
                String dn = requires.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   558
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   559
                ResolvedModule m2 = null;
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   560
                ModuleReference mref2 = nameToReference.get(dn);
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   561
                if (mref2 != null) {
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   562
                    // same configuration
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   563
                    m2 = computeIfAbsent(nameToResolved, dn, cf, mref2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
                } else {
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   565
                    // parent configuration
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   566
                    m2 = findInParent(dn);
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   567
                    if (m2 == null) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   568
                        assert requires.modifiers().contains(Modifier.STATIC);
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   569
                        continue;
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   570
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   573
                // m1 requires m2 => m1 reads m2
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   574
                reads.add(m2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   575
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   576
                // m1 requires transitive m2
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   577
                if (requires.modifiers().contains(Modifier.TRANSITIVE)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   578
                    requiresTransitive.add(m2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
                }
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
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   583
            // automatic modules read all selected modules and all modules
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
            // in parent configurations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
            if (descriptor.isAutomatic()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
                // reads all selected modules
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   588
                // `requires transitive` all selected automatic modules
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
                for (ModuleReference mref2 : nameToReference.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
                    ModuleDescriptor descriptor2 = mref2.descriptor();
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   591
                    String name2 = descriptor2.name();
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   592
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   593
                    if (!name.equals(name2)) {
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   594
                        ResolvedModule m2
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   595
                            = computeIfAbsent(nameToResolved, name2, cf, mref2);
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   596
                        reads.add(m2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   597
                        if (descriptor2.isAutomatic())
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   598
                            requiresTransitive.add(m2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   599
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   600
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   601
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   602
                // reads all modules in parent configurations
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   603
                // `requires transitive` all automatic modules in parent
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   604
                // configurations
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   605
                for (Configuration parent : parents) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   606
                    parent.configurations()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   607
                            .map(Configuration::modules)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   608
                            .flatMap(Set::stream)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   609
                            .forEach(m -> {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   610
                                reads.add(m);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   611
                                if (m.reference().descriptor().isAutomatic())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   612
                                    requiresTransitive.add(m);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   613
                            });
36511
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
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   617
            g1.put(m1, reads);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   618
            g2.put(m1, requiresTransitive);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   620
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   621
        // Iteratively update g1 until there are no more requires transitive
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   622
        // to propagate
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
        boolean changed;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   624
        List<ResolvedModule> toAdd = new ArrayList<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
        do {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
            changed = false;
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   627
            for (Set<ResolvedModule> m1Reads : g1.values()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
                for (ResolvedModule m2 : m1Reads) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   629
                    Set<ResolvedModule> m2RequiresTransitive = g2.get(m2);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   630
                    if (m2RequiresTransitive != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   631
                        for (ResolvedModule m3 : m2RequiresTransitive) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
                            if (!m1Reads.contains(m3)) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   633
                                // m1 reads m2, m2 requires transitive m3
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   634
                                // => need to add m1 reads m3
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   635
                                toAdd.add(m3);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
                }
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   640
                if (!toAdd.isEmpty()) {
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   641
                    m1Reads.addAll(toAdd);
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   642
                    toAdd.clear();
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   643
                    changed = true;
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   644
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
        } while (changed);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   648
        return g1;
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   649
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   651
    /**
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   652
     * Equivalent to
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   653
     * <pre>{@code
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   654
     *     map.computeIfAbsent(name, k -> new ResolvedModule(cf, mref))
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   655
     * </pre>}
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   656
     */
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   657
    private ResolvedModule computeIfAbsent(Map<String, ResolvedModule> map,
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   658
                                           String name,
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   659
                                           Configuration cf,
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   660
                                           ModuleReference mref)
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   661
    {
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   662
        ResolvedModule m = map.get(name);
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   663
        if (m == null) {
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   664
            m = new ResolvedModule(cf, mref);
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   665
            map.put(name, m);
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   666
        }
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 37779
diff changeset
   667
        return m;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
     * Checks the readability graph to ensure that no two modules export the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
     * same package to a module. This includes the case where module M has
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
     * a local package P and M reads another module that exports P to M.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
     * Also checks the uses/provides of module M to ensure that it reads a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
     * module that exports the package of the service type to M.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
    private void checkExportSuppliers(Map<ResolvedModule, Set<ResolvedModule>> graph) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
        for (Map.Entry<ResolvedModule, Set<ResolvedModule>> e : graph.entrySet()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
            ModuleDescriptor descriptor1 = e.getKey().descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
            // the map of packages that are local or exported to descriptor1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
            Map<String, ModuleDescriptor> packageToExporter = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
            // local packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
            Set<String> packages = descriptor1.packages();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
            for (String pn : packages) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   689
                packageToExporter.put(pn, descriptor1);
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
            // descriptor1 reads descriptor2
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   693
            Set<ResolvedModule> reads = e.getValue();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   694
            for (ResolvedModule endpoint : reads) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   695
                ModuleDescriptor descriptor2 = endpoint.descriptor();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
                for (ModuleDescriptor.Exports export : descriptor2.exports()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
                    if (export.isQualified()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
                        if (!export.targets().contains(descriptor1.name()))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
                            continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
                    // source is exported to descriptor2
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
                    String source = export.source();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
                    ModuleDescriptor other
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   707
                        = packageToExporter.putIfAbsent(source, descriptor2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
                    if (other != null && other != descriptor2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
                        // package might be local to descriptor1
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
                        if (other == descriptor1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
                            fail("Module %s contains package %s"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
                                 + ", module %s exports package %s to %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
                                    descriptor1.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
                                    source,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
                                    descriptor2.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
                                    source,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
                                    descriptor1.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
                        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
                            fail("Modules %s and %s export package %s to module %s",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
                                    descriptor2.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
                                    other.name(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
                                    source,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
                                    descriptor1.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   731
            // uses/provides checks not applicable to automatic modules
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   732
            if (!descriptor1.isAutomatic()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   734
                // uses S
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   735
                for (String service : descriptor1.uses()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   736
                    String pn = packageName(service);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   737
                    if (!packageToExporter.containsKey(pn)) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   738
                        fail("Module %s does not read a module that exports %s",
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   739
                             descriptor1.name(), pn);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   740
                    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   743
                // provides S
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   744
                for (ModuleDescriptor.Provides provides : descriptor1.provides()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   745
                    String pn = packageName(provides.service());
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   746
                    if (!packageToExporter.containsKey(pn)) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   747
                        fail("Module %s does not read a module that exports %s",
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   748
                             descriptor1.name(), pn);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   749
                    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   750
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   751
                    for (String provider : provides.providers()) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   752
                        if (!packages.contains(packageName(provider))) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   753
                            fail("Provider %s not in module %s",
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   754
                                 provider, descriptor1.name());
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   755
                        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   756
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
                }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   758
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   765
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   766
     * Find a module of the given name in the parent configurations
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   767
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   768
    private ResolvedModule findInParent(String mn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   769
        for (Configuration parent : parents) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   770
            Optional<ResolvedModule> om = parent.findModule(mn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   771
            if (om.isPresent())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   772
                return om.get();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   773
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   774
        return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   775
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   776
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   777
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   778
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   779
     * Invokes the beforeFinder to find method to find the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   780
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   781
    private ModuleReference findWithBeforeFinder(String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   782
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   783
            return beforeFinder.find(mn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   784
        } catch (FindException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   785
            // unwrap
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   786
            throw new ResolutionException(e.getMessage(), e.getCause());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   787
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   788
    }
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
     * Invokes the afterFinder to find method to find the given module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   792
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   793
    private ModuleReference findWithAfterFinder(String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   794
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   795
            return afterFinder.find(mn).orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   796
        } catch (FindException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   797
            // unwrap
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   798
            throw new ResolutionException(e.getMessage(), e.getCause());
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   802
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   803
     * Returns the set of all modules that are observable with the before
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   804
     * and after ModuleFinders.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   805
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   806
    private Set<ModuleReference> findAll() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   807
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   808
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   809
            Set<ModuleReference> beforeModules = beforeFinder.findAll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   810
            Set<ModuleReference> afterModules = afterFinder.findAll();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   811
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   812
            if (afterModules.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   813
                return beforeModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   814
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   815
            if (beforeModules.isEmpty()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   816
                    && parents.size() == 1
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   817
                    && parents.get(0) == Configuration.empty())
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   818
                return afterModules;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   819
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   820
            Set<ModuleReference> result = new HashSet<>(beforeModules);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   821
            for (ModuleReference mref : afterModules) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   822
                String name = mref.descriptor().name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   823
                if (!beforeFinder.find(name).isPresent()
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   824
                        && findInParent(name) == null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   825
                    result.add(mref);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39050
diff changeset
   826
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   827
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   828
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   829
            return result;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   830
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   831
        } catch (FindException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   832
            // unwrap
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   833
            throw new ResolutionException(e.getMessage(), e.getCause());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   834
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   835
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   836
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   837
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   838
     * Returns the package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   839
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   840
    private static String packageName(String cn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   841
        int index = cn.lastIndexOf(".");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   842
        return (index == -1) ? "" : cn.substring(0, index);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   843
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   844
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   845
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   846
     * Throw ResolutionException with the given format string and arguments
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   847
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   848
    private static void fail(String fmt, Object ... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   849
        String msg = String.format(fmt, args);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   850
        throw new ResolutionException(msg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   851
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   852
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   853
    /**
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   854
     * Tracing support
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   855
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   856
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   857
    private boolean isTracing() {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   858
        return traceOutput != null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   859
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   860
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   861
    private void trace(String fmt, Object ... args) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   862
        if (traceOutput != null) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   863
            traceOutput.format("[Resolver] " + fmt, args);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   864
            traceOutput.println();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   865
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   866
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   867
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   868
}