jdk/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 41817 b90ad1de93ea
child 42703 20c39ea4a507
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@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) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package java.lang.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    30
import java.io.PrintStream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.UncheckedIOException;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    32
import java.net.URI;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.nio.ByteBuffer;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 39050
diff changeset
    34
import java.nio.file.Path;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.ArrayList;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    36
import java.util.Collection;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.EnumSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.HashSet;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    41
import java.util.LinkedHashSet;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.function.Supplier;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    48
import java.util.stream.Collectors;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    49
import java.util.stream.Stream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import static jdk.internal.module.Checks.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import static java.util.Objects.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import jdk.internal.module.Checks;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    55
import jdk.internal.module.ModuleHashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
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
 * A module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 * <p> A {@code ModuleDescriptor} is typically created from the binary form
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    62
 * of a module declaration. Alternatively, the {@link ModuleDescriptor.Builder}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    63
 * class can be used to create a {@code ModuleDescriptor} from its components.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    64
 * The {@link #module module}, {@link #openModule openModule}, and {@link
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    65
 * #automaticModule automaticModule} methods create builders for building
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
    66
 * different kinds of modules. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 * <p> {@code ModuleDescriptor} objects are immutable and safe for use by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 * multiple concurrent threads.</p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
 * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
 * @see java.lang.reflect.Module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
public class ModuleDescriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
    implements Comparable<ModuleDescriptor>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
{
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
     * <p> A dependence upon a module </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
     * @see ModuleDescriptor#requires()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    public final static class Requires
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        implements Comparable<Requires>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
         * A modifier on a module dependence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
         * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        public static enum Modifier {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
            /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
             * The dependence causes any module which depends on the <i>current
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
             * module</i> to have an implicitly declared dependence on the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
             * named by the {@code Requires}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
             */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   102
            TRANSITIVE,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   103
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   104
            /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   105
             * The dependence is mandatory in the static phase, during compilation,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   106
             * but is optional in the dynamic phase, during execution.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   107
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   108
            STATIC,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
             * The dependence was not explicitly or implicitly declared in the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
             * source of the module declaration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
             */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            SYNTHETIC,
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
             * The dependence was implicitly declared in the source of the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
             * declaration.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
             */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            MANDATED;
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        private final Set<Modifier> mods;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        private final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        private Requires(Set<Modifier> ms, String mn) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   128
            if (ms.isEmpty()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   129
                ms = Collections.emptySet();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   130
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   131
                ms = Collections.unmodifiableSet(EnumSet.copyOf(ms));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   132
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   133
            this.mods = ms;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   134
            this.name = mn;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   136
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   137
        private Requires(Set<Modifier> ms, String mn, boolean unused) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   138
            this.mods = ms;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   139
            this.name = mn;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
         * Returns the set of modifiers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
         * @return A possibly-empty unmodifiable set of modifiers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        public Set<Modifier> modifiers() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            return mods;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
         * Return the module name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
         * @return The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        public String name() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
            return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
         * Compares this module dependence to another.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
         * <p> Two {@code Requires} objects are compared by comparing their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
         * module name lexicographically.  Where the module names are equal then
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
         * the sets of modifiers are compared based on a value computed from the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
         * ordinal of each modifier. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
         * @return A negative integer, zero, or a positive integer if this module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
         *         dependence is less than, equal to, or greater than the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
         *         module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        public int compareTo(Requires that) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            int c = this.name().compareTo(that.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            if (c != 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
                return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            // same name, compare by modifiers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            return Long.compare(this.modsValue(), that.modsValue());
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
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
         * Return a value for the modifiers to allow sets of modifiers to be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
         * compared.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        private long modsValue() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
            long value = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
            for (Modifier m : mods) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                value += 1 << m.ordinal();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
            return value;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
         * Tests this module dependence for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
         * <p> If the given object is not a {@code Requires} then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
         * returns {@code false}. Two module dependence objects are equal if
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
         * the module names are equal and set of modifiers are equal. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
         * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
         *          dependence that is equal to this module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
            if (!(ob instanceof Requires))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
            Requires that = (Requires)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
            return (name.equals(that.name) && mods.equals(that.mods));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
         * Computes a hash code for this module dependence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
         * <p> The hash code is based upon the module name and modifiers. It
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
         * satisfies the general contract of the {@link Object#hashCode
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
         * Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
         * @return The hash-code value for this module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
        public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
            return name.hashCode() * 43 + mods.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
         * Returns a string describing module dependence.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
         * @return A string describing module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
        public String toString() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   238
            return ModuleDescriptor.toString(mods, name);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242

9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
     * <p> A module export, may be qualified or unqualified. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
     * @see ModuleDescriptor#exports()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
    public final static class Exports {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   253
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   254
         * A modifier on a module export.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   255
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   256
         * @since 9
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   257
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   258
        public static enum Modifier {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   259
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   260
            /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   261
             * The export was not explicitly or implicitly declared in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   262
             * source of the module declaration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   263
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   264
            SYNTHETIC,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   265
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   266
            /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   267
             * The export was implicitly declared in the source of the module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   268
             * declaration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   269
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   270
            MANDATED;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   271
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   272
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   273
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   274
        private final Set<Modifier> mods;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
        private final String source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
        private final Set<String> targets;  // empty if unqualified export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   279
         * Constructs an export
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   281
        private Exports(Set<Modifier> ms, String source, Set<String> targets) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   282
            if (ms.isEmpty()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   283
                ms = Collections.emptySet();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   284
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   285
                ms = Collections.unmodifiableSet(EnumSet.copyOf(ms));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   286
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   287
            this.mods = ms;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   288
            this.source = source;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   289
            this.targets = emptyOrUnmodifiableSet(targets);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   292
        private Exports(Set<Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   293
                        String source,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   294
                        Set<String> targets,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   295
                        boolean unused) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   296
            this.mods = ms;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   297
            this.source = source;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            this.targets = targets;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   302
         * Returns the set of modifiers.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   303
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   304
         * @return A possibly-empty unmodifiable set of modifiers
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   306
        public Set<Modifier> modifiers() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   307
            return mods;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
         * Returns {@code true} if this is a qualified export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
         * @return {@code true} if this is a qualified export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        public boolean isQualified() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
            return !targets.isEmpty();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
         * Returns the package name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
         * @return The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
        public String source() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
            return source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   329
         * For a qualified export, returns the non-empty and immutable set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   330
         * of the module names to which the package is exported. For an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   331
         * unqualified export, returns an empty set.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   332
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   333
         * @return The set of target module names or for an unqualified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   334
         *         export, an empty set
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   335
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   336
        public Set<String> targets() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            return targets;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   338
        }
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
         * Computes a hash code for this module export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   342
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   343
         * <p> The hash code is based upon the modifiers, the package name,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   344
         * and for a qualified export, the set of modules names to which the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   345
         * package is exported. It satisfies the general contract of the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   346
         * {@link Object#hashCode Object.hashCode} method.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
         * @return The hash-code value for this module export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
        public int hashCode() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   352
            int hash = mods.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   353
            hash = hash * 43 + source.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   354
            return hash * 43 + targets.hashCode();
36511
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
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
         * Tests this module export for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
         *
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 37779
diff changeset
   360
         * <p> If the given object is not an {@code Exports} then this method
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   361
         * returns {@code false}. Two module exports objects are equal if their
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   362
         * set of modifiers is equal, the package names are equal and the set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   363
         * of target module names is equal. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
         * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
         *          dependence that is equal to this module dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
            if (!(ob instanceof Exports))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
            Exports other = (Exports)ob;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   379
            return Objects.equals(this.mods, other.mods)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   380
                    && Objects.equals(this.source, other.source)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   381
                    && Objects.equals(this.targets, other.targets);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
         * Returns a string describing module export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
         * @return A string describing module export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
        public String toString() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   391
            String s = ModuleDescriptor.toString(mods, source);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
            if (targets.isEmpty())
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   393
                return s;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
            else
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   395
                return s + " to " + targets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   396
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   397
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   398
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   399
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   400
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   401
     * <p> Represents a module <em>opens</em> directive, may be qualified or
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   402
     * unqualified. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   403
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   404
     * <p> The <em>opens</em> directive in a module declaration declares a
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   405
     * package to be open to allow all types in the package, and all their
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   406
     * members, not just public types and their public members to be reflected
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   407
     * on by APIs that support private access or a way to bypass or suppress
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   408
     * default Java language access control checks. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   409
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   410
     * @see ModuleDescriptor#opens()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   411
     * @since 9
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   412
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   413
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   414
    public final static class Opens {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   415
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   416
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   417
         * A modifier on a module <em>opens</em> directive.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   418
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   419
         * @since 9
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   420
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   421
        public static enum Modifier {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   422
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   423
            /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   424
             * The opens was not explicitly or implicitly declared in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   425
             * source of the module declaration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   426
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   427
            SYNTHETIC,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   428
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   429
            /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   430
             * The opens was implicitly declared in the source of the module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   431
             * declaration.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   432
             */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   433
            MANDATED;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   434
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   435
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   436
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   437
        private final Set<Modifier> mods;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   438
        private final String source;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   439
        private final Set<String> targets;  // empty if unqualified export
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   440
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   441
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   442
         * Constructs an Opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   443
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   444
        private Opens(Set<Modifier> ms, String source, Set<String> targets) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   445
            if (ms.isEmpty()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   446
                ms = Collections.emptySet();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   447
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   448
                ms = Collections.unmodifiableSet(EnumSet.copyOf(ms));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   449
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   450
            this.mods = ms;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   451
            this.source = source;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   452
            this.targets = emptyOrUnmodifiableSet(targets);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   453
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   454
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   455
        private Opens(Set<Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   456
                      String source,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   457
                      Set<String> targets,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   458
                      boolean unused) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   459
            this.mods = ms;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   460
            this.source = source;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   461
            this.targets = targets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   462
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   463
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   464
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   465
         * Returns the set of modifiers.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   466
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   467
         * @return A possibly-empty unmodifiable set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   468
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   469
        public Set<Modifier> modifiers() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   470
            return mods;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   473
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   474
         * Returns {@code true} if this is a qualified opens.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   475
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   476
         * @return {@code true} if this is a qualified opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   477
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   478
        public boolean isQualified() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   479
            return !targets.isEmpty();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   480
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   481
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   482
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   483
         * Returns the package name.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   484
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   485
         * @return The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   486
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   487
        public String source() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   488
            return source;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   489
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   490
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   491
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   492
         * For a qualified opens, returns the non-empty and immutable set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   493
         * of the module names to which the package is open. For an
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   494
         * unqualified opens, returns an empty set.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   495
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   496
         * @return The set of target module names or for an unqualified
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   497
         *         opens, an empty set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   498
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   499
        public Set<String> targets() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   500
            return targets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   501
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   502
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   503
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   504
         * Computes a hash code for this module opens.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   505
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   506
         * <p> The hash code is based upon the modifiers, the package name,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   507
         * and for a qualified opens, the set of modules names to which the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   508
         * package is opened. It satisfies the general contract of the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   509
         * {@link Object#hashCode Object.hashCode} method.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   510
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   511
         * @return The hash-code value for this module opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   512
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   513
        @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   514
        public int hashCode() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   515
            int hash = mods.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   516
            hash = hash * 43 + source.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   517
            return hash * 43 + targets.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   518
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   519
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   520
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   521
         * Tests this module opens for equality with the given object.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   522
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   523
         * <p> If the given object is not an {@code Opens} then this method
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   524
         * returns {@code false}. Two {@code Opens} objects are equal if their
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   525
         * set of modifiers is equal, the package names are equal and the set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   526
         * of target module names is equal. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   527
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   528
         * <p> This method satisfies the general contract of the {@link
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   529
         * java.lang.Object#equals(Object) Object.equals} method. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   530
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   531
         * @param   ob
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   532
         *          the object to which this object is to be compared
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   533
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   534
         * @return  {@code true} if, and only if, the given object is a module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   535
         *          dependence that is equal to this module dependence
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   536
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   537
        @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   538
        public boolean equals(Object ob) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   539
            if (!(ob instanceof Opens))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   540
                return false;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   541
            Opens other = (Opens)ob;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   542
            return Objects.equals(this.mods, other.mods)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   543
                    && Objects.equals(this.source, other.source)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   544
                    && Objects.equals(this.targets, other.targets);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   545
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   546
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   547
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   548
         * Returns a string describing module opens.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   549
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   550
         * @return A string describing module opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   551
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   552
        @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   553
        public String toString() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   554
            String s = ModuleDescriptor.toString(mods, source);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   555
            if (targets.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   556
                return s;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   557
            else
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   558
                return s + " to " + targets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   559
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   560
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   561
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   562
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   563
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   564
     * <p> A service that a module provides one or more implementations of. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   565
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   566
     * @see ModuleDescriptor#provides()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   567
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   568
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   569
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   570
    public final static class Provides {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   571
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   572
        private final String service;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   573
        private final List<String> providers;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   574
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   575
        private Provides(String service, List<String> providers) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   576
            this.service = service;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   577
            this.providers = Collections.unmodifiableList(providers);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   578
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   579
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   580
        private Provides(String service, List<String> providers, boolean unused) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   581
            this.service = service;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   582
            this.providers = providers;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   583
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   584
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   585
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   586
         * Returns the fully qualified class name of the service type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   587
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   588
         * @return The fully qualified class name of the service type.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   589
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   590
        public String service() { return service; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   591
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   592
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   593
         * Returns the list of the fully qualified class names of the providers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   594
         * or provider factories.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   595
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   596
         * @return A non-empty and unmodifiable list of the fully qualified class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   597
         *         names of the providers or provider factories
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   598
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   599
        public List<String> providers() { return providers; }
36511
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
         * Computes a hash code for this provides.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   603
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   604
         * <p> The hash code is based upon the service type and the set of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   605
         * providers. It satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   606
         * Object#hashCode Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   607
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   608
         * @return The hash-code value for this module provides
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   609
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   610
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   611
        public int hashCode() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   612
            return service.hashCode() * 43 + providers.hashCode();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   613
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   614
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   615
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   616
         * Tests this provides for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   617
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   618
         * <p> If the given object is not a {@code Provides} then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   619
         * returns {@code false}. Two {@code Provides} objects are equal if the
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   620
         * service type is equal and the list of providers is equal. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   621
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   622
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   623
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   624
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   625
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   626
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   627
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   628
         * @return  {@code true} if, and only if, the given object is a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   629
         *          {@code Provides} that is equal to this {@code Provides}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   630
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   631
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   632
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   633
            if (!(ob instanceof Provides))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   634
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   635
            Provides other = (Provides)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   636
            return Objects.equals(this.service, other.service) &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   637
                    Objects.equals(this.providers, other.providers);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   638
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   639
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   640
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   641
         * Returns a string describing this provides.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   642
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   643
         * @return A string describing this provides
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   644
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   645
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   646
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   647
            return service + " with " + providers;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   648
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   649
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   650
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   651
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   652

9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   653
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   654
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   655
     * A module's version string.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   656
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   657
     * <p> A version string has three components: The version number itself, an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   658
     * optional pre-release version, and an optional build version.  Each
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   659
     * component is sequence of tokens; each token is either a non-negative
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   660
     * integer or a string.  Tokens are separated by the punctuation characters
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   661
     * {@code '.'}, {@code '-'}, or {@code '+'}, or by transitions from a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   662
     * sequence of digits to a sequence of characters that are neither digits
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   663
     * nor punctuation characters, or vice versa.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   664
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   665
     * <ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   666
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   667
     *   <li> The <i>version number</i> is a sequence of tokens separated by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   668
     *   {@code '.'} characters, terminated by the first {@code '-'} or {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   669
     *   '+'} character. </li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   670
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   671
     *   <li> The <i>pre-release version</i> is a sequence of tokens separated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   672
     *   by {@code '.'} or {@code '-'} characters, terminated by the first
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   673
     *   {@code '+'} character. </li>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   674
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   675
     *   <li> The <i>build version</i> is a sequence of tokens separated by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   676
     *   {@code '.'}, {@code '-'}, or {@code '+'} characters.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   677
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   678
     * </ul>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   679
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   680
     * <p> When comparing two version strings, the elements of their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   681
     * corresponding components are compared in pointwise fashion.  If one
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   682
     * component is longer than the other, but otherwise equal to it, then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   683
     * first component is considered the greater of the two; otherwise, if two
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   684
     * corresponding elements are integers then they are compared as such;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   685
     * otherwise, at least one of the elements is a string, so the other is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   686
     * converted into a string if it is an integer and the two are compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   687
     * lexicographically.  Trailing integer elements with the value zero are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   688
     * ignored.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   689
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   690
     * <p> Given two version strings, if their version numbers differ then the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   691
     * result of comparing them is the result of comparing their version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   692
     * numbers; otherwise, if one of them has a pre-release version but the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   693
     * other does not then the first is considered to precede the second,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   694
     * otherwise the result of comparing them is the result of comparing their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   695
     * pre-release versions; otherwise, the result of comparing them is the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   696
     * result of comparing their build versions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   697
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   698
     * @see ModuleDescriptor#version()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   699
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   700
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   701
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   702
    public final static class Version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   703
        implements Comparable<Version>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   704
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   705
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   706
        private final String version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   707
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   708
        // If Java had disjunctive types then we'd write List<Integer|String> here
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   709
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   710
        private final List<Object> sequence;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   711
        private final List<Object> pre;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   712
        private final List<Object> build;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   713
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   714
        // Take a numeric token starting at position i
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   715
        // Append it to the given list
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   716
        // Return the index of the first character not taken
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   717
        // Requires: s.charAt(i) is (decimal) numeric
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   718
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   719
        private static int takeNumber(String s, int i, List<Object> acc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   720
            char c = s.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   721
            int d = (c - '0');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   722
            int n = s.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   723
            while (++i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   724
                c = s.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   725
                if (c >= '0' && c <= '9') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   726
                    d = d * 10 + (c - '0');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   727
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   728
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   729
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   730
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   731
            acc.add(d);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   732
            return i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   733
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   734
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   735
        // Take a string token starting at position i
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   736
        // Append it to the given list
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   737
        // Return the index of the first character not taken
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   738
        // Requires: s.charAt(i) is not '.'
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   739
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   740
        private static int takeString(String s, int i, List<Object> acc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   741
            int b = i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   742
            int n = s.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   743
            while (++i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   744
                char c = s.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   745
                if (c != '.' && c != '-' && c != '+' && !(c >= '0' && c <= '9'))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   746
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   747
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   748
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   749
            acc.add(s.substring(b, i));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   750
            return i;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   751
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   752
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   753
        // Syntax: tok+ ( '-' tok+)? ( '+' tok+)?
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   754
        // First token string is sequence, second is pre, third is build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   755
        // Tokens are separated by '.' or '-', or by changes between alpha & numeric
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   756
        // Numeric tokens are compared as decimal integers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   757
        // Non-numeric tokens are compared lexicographically
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   758
        // A version with a non-empty pre is less than a version with same seq but no pre
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   759
        // Tokens in build may contain '-' and '+'
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   760
        //
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   761
        private Version(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   762
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   763
            if (v == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   764
                throw new IllegalArgumentException("Null version string");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   765
            int n = v.length();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   766
            if (n == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   767
                throw new IllegalArgumentException("Empty version string");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   768
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   769
            int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   770
            char c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   771
            if (!(c >= '0' && c <= '9'))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   772
                throw new IllegalArgumentException(v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   773
                                                   + ": Version string does not start"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   774
                                                   + " with a number");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   775
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   776
            List<Object> sequence = new ArrayList<>(4);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   777
            List<Object> pre = new ArrayList<>(2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   778
            List<Object> build = new ArrayList<>(2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   779
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   780
            i = takeNumber(v, i, sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   781
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   782
            while (i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   783
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   784
                if (c == '.') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   785
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   786
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   787
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   788
                if (c == '-' || c == '+') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   789
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   790
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   791
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   792
                if (c >= '0' && c <= '9')
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   793
                    i = takeNumber(v, i, sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   794
                else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   795
                    i = takeString(v, i, sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   796
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   797
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   798
            if (c == '-' && i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   799
                throw new IllegalArgumentException(v + ": Empty pre-release");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   800
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   801
            while (i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   802
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   803
                if (c >= '0' && c <= '9')
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   804
                    i = takeNumber(v, i, pre);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   805
                else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   806
                    i = takeString(v, i, pre);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   807
                if (i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   808
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   809
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   810
                if (c == '.' || c == '-') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   811
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   812
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   813
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   814
                if (c == '+') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   815
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   816
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   817
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   818
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   819
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   820
            if (c == '+' && i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   821
                throw new IllegalArgumentException(v + ": Empty pre-release");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   822
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   823
            while (i < n) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   824
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   825
                if (c >= '0' && c <= '9')
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   826
                    i = takeNumber(v, i, build);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   827
                else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   828
                    i = takeString(v, i, build);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   829
                if (i >= n)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   830
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   831
                c = v.charAt(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   832
                if (c == '.' || c == '-' || c == '+') {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   833
                    i++;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   834
                    continue;
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
            this.version = v;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   839
            this.sequence = sequence;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   840
            this.pre = pre;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   841
            this.build = build;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   842
        }
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
         * Parses the given string as a version string.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   846
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   847
         * @param  v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   848
         *         The string to parse
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   849
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   850
         * @return The resulting {@code Version}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   851
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   852
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   853
         *         If {@code v} is {@code null}, an empty string, or cannot be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   854
         *         parsed as a version string
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   855
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   856
        public static Version parse(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   857
            return new Version(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   858
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   859
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   860
        @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   861
        private int cmp(Object o1, Object o2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   862
            return ((Comparable)o1).compareTo(o2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   863
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   864
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   865
        private int compareTokens(List<Object> ts1, List<Object> ts2) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   866
            int n = Math.min(ts1.size(), ts2.size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   867
            for (int i = 0; i < n; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   868
                Object o1 = ts1.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   869
                Object o2 = ts2.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   870
                if ((o1 instanceof Integer && o2 instanceof Integer)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   871
                    || (o1 instanceof String && o2 instanceof String))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   872
                {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   873
                    int c = cmp(o1, o2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   874
                    if (c == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   875
                        continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   876
                    return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   877
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   878
                // Types differ, so convert number to string form
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   879
                int c = o1.toString().compareTo(o2.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   880
                if (c == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   881
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   882
                return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   883
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   884
            List<Object> rest = ts1.size() > ts2.size() ? ts1 : ts2;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   885
            int e = rest.size();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   886
            for (int i = n; i < e; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   887
                Object o = rest.get(i);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   888
                if (o instanceof Integer && ((Integer)o) == 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   889
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   890
                return ts1.size() - ts2.size();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   891
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   892
            return 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   893
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   894
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   895
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   896
         * Compares this module version to another module version. Module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   897
         * versions are compared as described in the class description.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   898
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   899
         * @param that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   900
         *        The module version to compare
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   901
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   902
         * @return A negative integer, zero, or a positive integer as this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   903
         *         module version is less than, equal to, or greater than the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   904
         *         given module version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   905
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   906
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   907
        public int compareTo(Version that) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   908
            int c = compareTokens(this.sequence, that.sequence);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   909
            if (c != 0) return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   910
            if (this.pre.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   911
                if (!that.pre.isEmpty()) return +1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   912
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   913
                if (that.pre.isEmpty()) return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   914
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   915
            c = compareTokens(this.pre, that.pre);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   916
            if (c != 0) return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   917
            return compareTokens(this.build, that.build);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   918
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   919
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   920
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   921
         * Tests this module version for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   922
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   923
         * <p> If the given object is not a {@code Version} then this method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   924
         * returns {@code false}. Two module version are equal if their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   925
         * corresponding components are equal. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   926
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   927
         * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   928
         * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   929
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   930
         * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   931
         *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   932
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   933
         * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   934
         *          reference that is equal to this module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   935
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   936
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   937
        public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   938
            if (!(ob instanceof Version))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   939
                return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   940
            return compareTo((Version)ob) == 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   941
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   942
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   943
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   944
         * Computes a hash code for this module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   945
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   946
         * <p> The hash code is based upon the components of the version and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   947
         * satisfies the general contract of the {@link Object#hashCode
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   948
         * Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   949
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   950
         * @return The hash-code value for this module version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   951
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   952
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   953
        public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   954
            return version.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   955
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   956
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   957
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   958
         * Returns the string from which this version was parsed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   959
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   960
         * @return The string from which this version was parsed.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   961
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   962
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   963
        public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   964
            return version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   965
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   966
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   967
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   968
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   969

9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   970
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   971
    // From module declarations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   972
    private final String name;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   973
    private final boolean open;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   974
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   975
    // Indicates if synthesised for a JAR file found on the module path
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   976
    private final boolean automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   977
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   978
    // Not generated from a module-info.java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   979
    private final boolean synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   980
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   981
    private final Set<Requires> requires;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   982
    private final Set<Exports> exports;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   983
    private final Set<Opens> opens;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   984
    private final Set<String> uses;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   985
    private final Set<Provides> provides;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   986
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   987
    // "Extended" information, added post-compilation by tools
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   988
    private final Version version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   989
    private final String mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   990
    private final String osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   991
    private final String osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   992
    private final String osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   993
    private final Set<String> packages;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   994
    private final ModuleHashes hashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   995
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
   996
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   997
    private ModuleDescriptor(String name,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
   998
                             boolean open,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   999
                             boolean automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1000
                             boolean synthetic,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1001
                             Set<Requires> requires,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1002
                             Set<Exports> exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1003
                             Set<Opens> opens,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1004
                             Set<String> uses,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1005
                             Set<Provides> provides,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1006
                             Version version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1007
                             String mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1008
                             String osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1009
                             String osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1010
                             String osVersion,
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1011
                             Set<String> packages,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1012
                             ModuleHashes hashes)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1013
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1014
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1015
        this.name = name;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1016
        this.open = open;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1017
        this.automatic = automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1018
        this.synthetic = synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1019
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1020
        assert (requires.stream().map(Requires::name).distinct().count()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1021
                == requires.size());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1022
        this.requires = emptyOrUnmodifiableSet(requires);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1023
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1024
        this.exports = emptyOrUnmodifiableSet(exports);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1025
        this.opens = emptyOrUnmodifiableSet(opens);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1026
        this.uses = emptyOrUnmodifiableSet(uses);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1027
        this.provides = emptyOrUnmodifiableSet(provides);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1028
        this.version = version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1029
        this.mainClass = mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1030
        this.osName = osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1031
        this.osArch = osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1032
        this.osVersion = osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1033
        this.hashes = hashes;
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1034
        this.packages = emptyOrUnmodifiableSet(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1035
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1036
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1037
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1038
     * Clones the given module descriptor with an augmented set of packages
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1039
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1040
    ModuleDescriptor(ModuleDescriptor md, Set<String> pkgs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1041
        this.name = md.name;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1042
        this.open = md.open;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1043
        this.automatic = md.automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1044
        this.synthetic = md.synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1045
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1046
        this.requires = md.requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1047
        this.exports = md.exports;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1048
        this.opens = md.opens;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1049
        this.uses = md.uses;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1050
        this.provides = md.provides;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1051
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1052
        this.version = md.version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1053
        this.mainClass = md.mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1054
        this.osName = md.osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1055
        this.osArch = md.osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1056
        this.osVersion = md.osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1057
        this.hashes = null; // need to ignore
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1058
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1059
        Set<String> packages = new HashSet<>(md.packages);
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1060
        packages.addAll(pkgs);
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  1061
        this.packages = emptyOrUnmodifiableSet(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1062
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1063
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1064
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1065
     * Creates a module descriptor from its components.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1066
     * The arguments are pre-validated and sets are unmodifiable sets.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1067
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1068
    ModuleDescriptor(String name,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1069
                     boolean open,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1070
                     boolean automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1071
                     boolean synthetic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1072
                     Set<Requires> requires,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1073
                     Set<Exports> exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1074
                     Set<Opens> opens,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1075
                     Set<String> uses,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1076
                     Set<Provides> provides,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1077
                     Version version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1078
                     String mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1079
                     String osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1080
                     String osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1081
                     String osVersion,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1082
                     Set<String> packages,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1083
                     ModuleHashes hashes,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1084
                     int hashCode,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1085
                     boolean unused) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1086
        this.name = name;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1087
        this.open = open;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1088
        this.automatic = automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1089
        this.synthetic = synthetic;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1090
        this.requires = requires;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1091
        this.exports = exports;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1092
        this.opens = opens;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1093
        this.uses = uses;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1094
        this.provides = provides;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1095
        this.packages = packages;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1096
        this.version = version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1097
        this.mainClass = mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1098
        this.osName = osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1099
        this.osArch = osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1100
        this.osVersion = osVersion;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1101
        this.hashes = hashes;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1102
        this.hash = hashCode;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1103
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1105
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1106
     * <p> The module name. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1107
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1108
     * @return The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1109
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1110
    public String name() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1111
        return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1112
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1114
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1115
     * <p> Returns {@code true} if this is an open module. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1116
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1117
     * <p> An open module does not declare any open packages (the {@link #opens()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1118
     * opens} method returns an empty set) but the resulting module is treated
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1119
     * as if all packages are open. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1120
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1121
     * @return  {@code true} if this is an open module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1122
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1123
    public boolean isOpen() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1124
        return open;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1125
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1126
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1127
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1128
     * <p> Returns {@code true} if this is an automatic module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1129
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1130
     * <p> An automatic module is defined implicitly rather than explicitly
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1131
     * and therefore does not have a module declaration. JAR files located on
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1132
     * the application module path, or by the {@link ModuleFinder} returned by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1133
     * {@link ModuleFinder#of(java.nio.file.Path[]) ModuleFinder.of}, are
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1134
     * treated as automatic modules if they do have not have a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1135
     * declaration. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1136
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1137
     * @return  {@code true} if this is an automatic module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1138
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1139
    public boolean isAutomatic() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1140
        return automatic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1141
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1143
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1144
     * <p> Returns {@code true} if this module descriptor was not generated
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1145
     * from an explicit module declaration ({@code module-info.java})
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1146
     * or an implicit module declaration (an {@link #isAutomatic() automatic}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1147
     * module). </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1148
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1149
     * @return  {@code true} if this module descriptor was not generated by
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1150
     *          an explicit or implicit module declaration
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1151
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1152
    public boolean isSynthetic() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1153
        return synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1154
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1155
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1156
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1157
     * <p> The dependences of this module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1158
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1159
     * @return  A possibly-empty unmodifiable set of {@link Requires} objects
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1160
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1161
    public Set<Requires> requires() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1162
        return requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1163
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1164
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1165
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1166
     * <p> The module exports. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1167
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1168
     * @return  A possibly-empty unmodifiable set of exported packages
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1169
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1170
    public Set<Exports> exports() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1171
        return exports;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1172
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1173
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1174
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1175
     * <p> The module <em>opens</em> directives. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1176
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1177
     * <p> Each {@code Opens} object in the set represents a package (and
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1178
     * the set of target module names when qualified) where all types in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1179
     * package, and all their members, not just public types and their public
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1180
     * members, can be reflected on when using APIs that bypass or suppress
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1181
     * default Java language access control checks. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1182
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1183
     * <p> This method returns an empty set when invoked on {@link #isOpen()
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1184
     * open} module. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1185
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1186
     * @return  A possibly-empty unmodifiable set of open packages
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1187
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1188
    public Set<Opens> opens() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1189
        return opens;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1190
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1191
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1192
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1193
     * <p> The service dependences of this module. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1194
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1195
     * @return  A possibly-empty unmodifiable set of the fully qualified class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1196
     *          names of the service types used
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1197
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1198
    public Set<String> uses() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1199
        return uses;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1200
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1201
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1202
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1203
     * <p> The services that this module provides. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1204
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1205
     * @return The possibly-empty unmodifiable set of the services that this
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1206
     *         module provides
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1207
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1208
    public Set<Provides> provides() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1209
        return provides;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1210
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1211
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1212
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1213
     * Returns this module's version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1214
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1215
     * @return This module's version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1216
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1217
    public Optional<Version> version() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1218
        return Optional.ofNullable(version);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1219
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1220
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1221
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1222
     * Returns a string containing this module's name and, if present, its
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1223
     * version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1224
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1225
     * @return A string containing this module's name and, if present, its
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1226
     *         version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1227
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1228
    public String toNameAndVersion() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1229
        if (version != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1230
            return name() + "@" + version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1231
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1232
            return name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1233
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1234
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1235
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1236
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1237
     * Returns the module's main class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1238
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1239
     * @return The fully qualified class name of this module's main class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1240
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1241
    public Optional<String> mainClass() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1242
        return Optional.ofNullable(mainClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1243
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1244
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1245
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1246
     * Returns the operating system name if this module is operating system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1247
     * specific.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1248
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1249
     * @return The operating system name or an empty {@code Optional}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1250
     *         if this module is not operating system specific
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1251
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1252
    public Optional<String> osName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1253
        return Optional.ofNullable(osName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1254
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1255
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1256
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1257
     * Returns the operating system architecture if this module is operating
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1258
     * system architecture specific.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1259
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1260
     * @return The operating system architecture or an empty {@code Optional}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1261
     *         if this module is not operating system architecture specific
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1262
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1263
    public Optional<String> osArch() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1264
        return Optional.ofNullable(osArch);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1265
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1266
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1267
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1268
     * Returns the operating system version if this module is operating
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1269
     * system version specific.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1270
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1271
     * @return The operating system version or an empty {@code Optional}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1272
     *         if this module is not operating system version specific
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1273
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1274
    public Optional<String> osVersion() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1275
        return Optional.ofNullable(osVersion);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1276
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1277
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1278
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1279
     * Returns the names of all packages in this module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1280
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1281
     * @return A possibly-empty unmodifiable set of all packages in the module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1282
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1283
    public Set<String> packages() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1284
        return packages;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1285
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1286
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1287
    /**
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1288
     * Returns the object with the hashes of other modules
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1289
     */
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1290
    Optional<ModuleHashes> hashes() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1291
        return Optional.ofNullable(hashes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1292
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1293
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1294
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1295
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1296
     * A builder used for building {@link ModuleDescriptor} objects.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1297
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1298
     * <p> {@code ModuleDescriptor} defines the {@link #module module}, {@link
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1299
     * #openModule openModule}, and {@link #automaticModule automaticModule}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1300
     * methods to create builders for building different kinds of modules. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1301
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1302
     * <p> Example usage: </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1303
     * <pre>{@code    ModuleDescriptor descriptor = ModuleDescriptor.module("m1")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1304
     *         .exports("p")
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1305
     *         .requires("m2")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1306
     *         .build();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1307
     * }</pre>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1308
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1309
     * @apiNote A {@code Builder} checks the components and invariants as
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1310
     * components are added to the builder. The rational for this is to detect
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1311
     * errors as early as possible and not defer all validation to the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1312
     * {@link #build build} method. A {@code Builder} cannot be used to create
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1313
     * a {@link ModuleDescriptor#isSynthetic() synthetic} module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1314
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1315
     * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1316
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1317
    public static final class Builder {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1318
        final String name;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1319
        final boolean strict; // true if module names are checked
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1320
        boolean open;
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1321
        boolean automatic;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1322
        boolean synthetic;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1323
        final Map<String, Requires> requires = new HashMap<>();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1324
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1325
        final Map<String, Exports> exports = new HashMap<>();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1326
        final Map<String, Opens> opens = new HashMap<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1327
        final Set<String> concealedPackages = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1328
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1329
        final Set<String> uses = new HashSet<>();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1330
        final Map<String, Provides> provides = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1331
        Version version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1332
        String osName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1333
        String osArch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1334
        String osVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1335
        String mainClass;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1336
        ModuleHashes hashes;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1337
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1338
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1339
         * Initializes a new builder with the given module name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1340
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1341
         * @param strict
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1342
         *        Indicates whether module names are checked or not
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1343
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1344
        Builder(String name, boolean strict) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1345
            this.strict = strict;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1346
            this.name = (strict) ? requireModuleName(name) : name;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1347
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1348
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1349
        /* package */ Builder open(boolean open) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1350
            this.open = open;
39050
9de41b79ec7e 8158456: ModuleDescriptor.read does not verify dependence on java.base in module-info.class
alanb
parents: 38888
diff changeset
  1351
            return this;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1352
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1353
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1354
        /* package */ Builder automatic(boolean automatic) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1355
            this.automatic = automatic;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1356
            return this;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1357
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1358
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1359
        /* package */ boolean isOpen() { return open; }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1360
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1361
        /* package */ boolean isAutomatic() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1362
            return automatic;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1363
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1364
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1365
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1366
         * Adds a dependence on a module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1367
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1368
         * @param  req
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1369
         *         The dependence
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1370
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1371
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1372
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1373
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1374
         *         If the dependence is on the module that this builder was
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1375
         *         initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1376
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1377
         *         If the dependence on the module has already been declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1378
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1379
        public Builder requires(Requires req) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1380
            String mn = req.name();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1381
            if (name.equals(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1382
                throw new IllegalArgumentException("Dependence on self");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1383
            if (requires.containsKey(mn))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1384
                throw new IllegalStateException("Dependence upon " + mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1385
                                                + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1386
            requires.put(mn, req);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1387
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1388
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1389
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1390
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1391
         * Adds a dependence on a module with the given (and possibly empty)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1392
         * set of modifiers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1393
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1394
         * @param  ms
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1395
         *         The set of modifiers
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1396
         * @param  mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1397
         *         The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1398
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1399
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1400
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1401
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1402
         *         If the module name is {@code null}, is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1403
         *         identifier, or is equal to the module name that this builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1404
         *         was initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1405
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1406
         *         If the dependence on the module has already been declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1407
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1408
        public Builder requires(Set<Requires.Modifier> ms, String mn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1409
            if (strict)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1410
                mn = requireModuleName(mn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1411
            return requires(new Requires(ms, mn));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1412
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1413
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1414
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1415
         * Adds a dependence on a module with an empty set of modifiers.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1416
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1417
         * @param  mn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1418
         *         The module name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1419
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1420
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1421
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1422
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1423
         *         If the module name is {@code null}, is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1424
         *         identifier, or is equal to the module name that this builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1425
         *         was initialized to build
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1426
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1427
         *         If the dependence on the module has already been declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1428
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1429
        public Builder requires(String mn) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1430
            return requires(EnumSet.noneOf(Requires.Modifier.class), mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1431
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1432
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1433
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1434
         * Adds an export.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1435
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1436
         * @param  e
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1437
         *         The export
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1438
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1439
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1440
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1441
         * @throws IllegalStateException
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1442
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1443
         *         {@link #contains contains} method or the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1444
         *         declared as exported
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1445
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1446
        public Builder exports(Exports e) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1447
            // can't be exported and concealed
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1448
            String source = e.source();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1449
            if (concealedPackages.contains(source)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1450
                throw new IllegalStateException("Package " + source
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1451
                                                 + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1452
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1453
            if (exports.containsKey(source)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1454
                throw new IllegalStateException("Exported package " + source
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1455
                                                 + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1456
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1457
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1458
            exports.put(source, e);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1459
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1460
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1461
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1462
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1463
         * Adds an export, with the given (and possibly empty) set of modifiers,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1464
         * to export a package to a set of target modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1465
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1466
         * @param  ms
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1467
         *         The set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1468
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1469
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1470
         * @param  targets
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1471
         *         The set of target modules names
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1472
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1473
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1474
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1475
         * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1476
         *         If the package name or any of the target modules is {@code
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1477
         *         null} or is not a legal Java identifier, or the set of
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1478
         *         targets is empty
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1479
         * @throws IllegalStateException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1480
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1481
         *         {@link #contains contains} method or the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1482
         *         declared as exported
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1483
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1484
        public Builder exports(Set<Exports.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1485
                               String pn,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1486
                               Set<String> targets)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1487
        {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1488
            Exports e = new Exports(ms, requirePackageName(pn), targets);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1489
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1490
            // check targets
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1491
            targets = e.targets();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1492
            if (targets.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1493
                throw new IllegalArgumentException("Empty target set");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1494
            if (strict)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1495
                targets.stream().forEach(Checks::requireModuleName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1496
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1497
            return exports(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1498
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1499
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1500
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1501
         * Adds an unqualified export with the given (and possibly empty) set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1502
         * of modifiers.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1503
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1504
         * @param  ms
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1505
         *         The set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1506
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1507
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1508
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1509
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1510
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1511
         * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1512
         *         If the package name is {@code null} or is not a legal Java
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1513
         *         identifier
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1514
         * @throws IllegalStateException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1515
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1516
         *         {@link #contains contains} method or the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1517
         *         declared as exported
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1518
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1519
        public Builder exports(Set<Exports.Modifier> ms, String pn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1520
            Exports e = new Exports(ms, requirePackageName(pn), Collections.emptySet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1521
            return exports(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1522
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1523
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1524
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1525
         * Adds an export to export a package to a set of target modules.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1526
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1527
         * @param  pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1528
         *         The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1529
         * @param  targets
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1530
         *         The set of target modules names
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1531
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1532
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1533
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1534
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1535
         *         If the package name or any of the target modules is {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1536
         *         null} or is not a legal Java identifier, or the set of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1537
         *         targets is empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1538
         * @throws IllegalStateException
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1539
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1540
         *         {@link #contains contains} method or the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1541
         *         declared as exported
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1542
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1543
        public Builder exports(String pn, Set<String> targets) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1544
            return exports(Collections.emptySet(), pn, targets);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1545
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1546
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1547
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1548
         * Adds an unqualified export.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1549
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1550
         * @param  pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1551
         *         The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1552
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1553
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1554
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1555
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1556
         *         If the package name is {@code null} or is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1557
         *         identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1558
         * @throws IllegalStateException
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1559
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1560
         *         {@link #contains contains} method or the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1561
         *         declared as exported
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1562
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1563
        public Builder exports(String pn) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1564
            return exports(Collections.emptySet(), pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1565
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1566
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1567
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1568
         * Adds an <em>opens</em> directive.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1569
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1570
         * @param  obj
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1571
         *         The {@code Opens} object
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1572
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1573
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1574
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1575
         * @throws IllegalStateException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1576
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1577
         *         {@link #contains contains} method, the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1578
         *         declared as open, or this is a builder for an open module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1579
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1580
        public Builder opens(Opens obj) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1581
            if (open) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1582
                throw new IllegalStateException("open modules cannot declare"
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1583
                                                + " open packages");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1584
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1585
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1586
            // can't be open and concealed
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1587
            String source = obj.source();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1588
            if (concealedPackages.contains(source)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1589
                throw new IllegalStateException("Package " + source
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1590
                                                + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1591
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1592
            if (opens.containsKey(source)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1593
                throw new IllegalStateException("Open package " + source
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1594
                                                + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1595
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1596
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1597
            opens.put(source, obj);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1598
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1599
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1600
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1601
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1602
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1603
         * Adds an <em>opens</em> directive, with the given (and possibly empty)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1604
         * set of modifiers, to open a package to a set of target modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1605
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1606
         * @param  ms
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1607
         *         The set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1608
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1609
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1610
         * @param  targets
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1611
         *         The set of target modules names
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1612
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1613
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1614
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1615
         * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1616
         *         If the package name or any of the target modules is {@code
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1617
         *         null} or is not a legal Java identifier, or the set of
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1618
         *         targets is empty
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1619
         * @throws IllegalStateException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1620
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1621
         *         {@link #contains contains} method, the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1622
         *         declared as open, or this is a builder for an open module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1623
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1624
        public Builder opens(Set<Opens.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1625
                             String pn,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1626
                             Set<String> targets)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1627
        {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1628
            Opens e = new Opens(ms, requirePackageName(pn), targets);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1629
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1630
            // check targets
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1631
            targets = e.targets();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1632
            if (targets.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1633
                throw new IllegalArgumentException("Empty target set");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1634
            if (strict)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1635
                targets.stream().forEach(Checks::requireModuleName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1636
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1637
            return opens(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1638
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1639
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1640
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1641
         * Adds an <em>opens</em> directive to open a package with the given (and
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1642
         * possibly empty) set of modifiers.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1643
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1644
         * @param  ms
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1645
         *         The set of modifiers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1646
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1647
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1648
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1649
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1650
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1651
         * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1652
         *         If the package name is {@code null} or is not a legal Java
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1653
         *         identifier
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1654
         * @throws IllegalStateException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1655
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1656
         *         {@link #contains contains} method, the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1657
         *         declared as open, or this is a builder for an open module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1658
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1659
        public Builder opens(Set<Opens.Modifier> ms, String pn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1660
            Opens e = new Opens(ms, requirePackageName(pn), Collections.emptySet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1661
            return opens(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1662
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1663
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1664
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1665
         * Adds an <em>opens</em> directive to open a package to a set of target
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1666
         * modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1667
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1668
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1669
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1670
         * @param  targets
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1671
         *         The set of target modules names
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1672
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1673
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1674
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1675
         * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1676
         *         If the package name or any of the target modules is {@code
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1677
         *         null} or is not a legal Java identifier, or the set of
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1678
         *         targets is empty
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1679
         * @throws IllegalStateException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1680
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1681
         *         {@link #contains contains} method, the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1682
         *         declared as open, or this is a builder for an open module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1683
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1684
        public Builder opens(String pn, Set<String> targets) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1685
            return opens(Collections.emptySet(), pn, targets);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1686
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1687
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1688
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1689
         * Adds an <em>opens</em> directive to open a package.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1690
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1691
         * @param  pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1692
         *         The package name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1693
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1694
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1695
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1696
         * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1697
         *         If the package name is {@code null} or is not a legal Java
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1698
         *         identifier
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1699
         * @throws IllegalStateException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1700
         *         If the package is already declared as a package with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1701
         *         {@link #contains contains} method, the package is already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1702
         *         declared as open, or this is a builder for an open module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1703
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1704
        public Builder opens(String pn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1705
            return opens(Collections.emptySet(), pn);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1706
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1707
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1708
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1709
        // Used by ModuleInfo, after a packageFinder is invoked
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1710
        /* package */ Set<String> exportedAndOpenPackages() {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1711
            if (opens.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1712
                return exports.keySet();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1713
            Set<String> result = new HashSet<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1714
            result.addAll(exports.keySet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1715
            result.addAll(opens.keySet());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1716
            return result;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1717
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1718
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1719
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1720
         * Adds a service dependence.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1721
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1722
         * @param  service
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1723
         *         The service type
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1724
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1725
         * @return This builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1726
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1727
         * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1728
         *         If the service type is {@code null} or is not a legal Java
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1729
         *         identifier
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1730
         * @throws IllegalStateException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1731
         *         If a dependency on the service type has already been declared
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1732
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1733
        public Builder uses(String service) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1734
            if (uses.contains(requireServiceTypeName(service)))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1735
                throw new IllegalStateException("Dependence upon service "
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1736
                                                + service + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1737
            uses.add(service);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1738
            return this;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1739
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1740
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1741
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1742
         * Provides a service with one or more implementations.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1743
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1744
         * @param  p
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1745
         *         The provides
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1746
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1747
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1748
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1749
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1750
         *         If the providers for the service type have already been
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1751
         *         declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1752
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1753
        public Builder provides(Provides p) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1754
            String st = p.service();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1755
            if (provides.containsKey(st))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1756
                throw new IllegalStateException("Providers of service "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1757
                                                + st + " already declared");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1758
            provides.put(st, p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1759
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1760
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1761
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1762
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1763
         * Provides implementations of a service.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1764
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1765
         * @param  service
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1766
         *         The service type
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1767
         * @param  providers
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1768
         *         The list of provider or provider factory class names
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1769
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1770
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1771
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1772
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1773
         *         If the service type or any of the provider class names is
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1774
         *         {@code null} or is not a legal Java identifier, or the list
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1775
         *         of provider class names is empty
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1776
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1777
         *         If the providers for the service type have already been
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1778
         *         declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1779
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1780
        public Builder provides(String service, List<String> providers) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1781
            if (provides.containsKey(service))
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1782
                throw new IllegalStateException("Providers of service "
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1783
                                                + service + " already declared by " + name);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1784
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1785
            Provides p = new Provides(requireServiceTypeName(service), providers);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1786
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1787
            // check providers after the set has been copied.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1788
            List<String> providerNames = p.providers();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1789
            if (providerNames.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1790
                throw new IllegalArgumentException("Empty providers set");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1791
            providerNames.forEach(Checks::requireServiceProviderName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1792
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1793
            provides.put(service, p);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1794
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1795
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1796
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1797
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1798
         * Provides an implementation of a service.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1799
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1800
         * @param  service
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1801
         *         The service type
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1802
         * @param  provider
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1803
         *         The provider or provider factory class name
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1804
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1805
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1806
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1807
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1808
         *         If the service type or the provider class name is {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1809
         *         null} or is not a legal Java identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1810
         * @throws IllegalStateException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1811
         *         If the providers for the service type have already been
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1812
         *         declared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1813
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1814
        public Builder provides(String service, String provider) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1815
            if (provider == null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1816
                throw new IllegalArgumentException("'provider' is null");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1817
            return provides(service, List.of(provider));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1818
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1819
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1820
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1821
         * Adds a (possible empty) set of packages to the module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1822
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1823
         * @param  pns
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1824
         *         The set of package names
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1825
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1826
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1827
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1828
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1829
         *         If any of the package names is {@code null} or is not a
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1830
         *         legal Java identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1831
         * @throws IllegalStateException
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1832
         *         If any of packages are already declared as packages in
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1833
         *         the module. This includes packages that are already
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1834
         *         declared as exported or open packages.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1835
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1836
        public Builder contains(Set<String> pns) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1837
            pns.forEach(this::contains);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1838
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1839
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1840
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1841
        /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1842
         * Adds a package to the module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1843
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1844
         * @param  pn
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1845
         *         The package name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1846
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1847
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1848
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1849
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1850
         *         If the package name is {@code null}, or is not a legal Java
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1851
         *         identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1852
         * @throws IllegalStateException
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1853
         *         If the package is already declared as a package in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1854
         *         module. This includes the package already declared as an
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1855
         *         exported or open package.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1856
         */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1857
        public Builder contains(String pn) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1858
            Checks.requirePackageName(pn);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1859
            if (concealedPackages.contains(pn)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1860
                throw new IllegalStateException("Package " + pn
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1861
                                                + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1862
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1863
            if (exports.containsKey(pn)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1864
                throw new IllegalStateException("Exported package "
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1865
                                                + pn + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1866
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1867
            if (opens.containsKey(pn)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1868
                throw new IllegalStateException("Open package "
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1869
                                                 + pn + " already declared");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1870
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1871
            concealedPackages.add(pn);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1872
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1873
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1874
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1875
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1876
         * Sets the module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1877
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1878
         * @param  v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1879
         *         The version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1880
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1881
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1882
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1883
        public Builder version(Version v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1884
            version = requireNonNull(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1885
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1886
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1887
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1888
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1889
         * Sets the module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1890
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1891
         * @param  v
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1892
         *         The version string to parse
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1893
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1894
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1895
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1896
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1897
         *         If {@code v} is null or cannot be parsed as a version string
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1898
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1899
         * @see Version#parse(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1900
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1901
        public Builder version(String v) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1902
            return version(Version.parse(v));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1903
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1904
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1905
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1906
         * Sets the module main class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1907
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1908
         * @param  mc
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1909
         *         The module main class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1910
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1911
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1912
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1913
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1914
         *         If {@code mainClass} is null or is not a legal Java identifier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1915
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1916
        public Builder mainClass(String mc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1917
            mainClass = requireJavaIdentifier("main class name", mc);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1918
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1919
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1920
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1921
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1922
         * Sets the operating system name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1923
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1924
         * @param  name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1925
         *         The operating system name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1926
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1927
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1928
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1929
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1930
         *         If {@code name} is null or the empty String
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1931
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1932
        public Builder osName(String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1933
            if (name == null || name.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1934
                throw new IllegalArgumentException("OS name is null or empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1935
            osName = name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1936
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1937
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1938
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1939
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1940
         * Sets the operating system architecture.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1941
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1942
         * @param  arch
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1943
         *         The operating system architecture
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1944
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1945
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1946
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1947
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1948
         *         If {@code name} is null or the empty String
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1949
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1950
        public Builder osArch(String arch) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1951
            if (arch == null || arch.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1952
                throw new IllegalArgumentException("OS arch is null or empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1953
            osArch = arch;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1954
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1955
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1956
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1957
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1958
         * Sets the operating system version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1959
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1960
         * @param  version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1961
         *         The operating system version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1962
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1963
         * @return This builder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1964
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1965
         * @throws IllegalArgumentException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1966
         *         If {@code name} is null or the empty String
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1967
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1968
        public Builder osVersion(String version) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1969
            if (version == null || version.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1970
                throw new IllegalArgumentException("OS version is null or empty");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1971
            osVersion = version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1972
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1973
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1974
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  1975
        /* package */ Builder hashes(ModuleHashes hashes) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1976
            this.hashes = hashes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1977
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1978
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1979
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1980
        /* package */ Builder synthetic(boolean v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1981
            this.synthetic = v;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1982
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1983
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1984
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1985
        /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1986
         * Builds and returns a {@code ModuleDescriptor} from its components.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1987
         *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1988
         * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1989
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1990
        public ModuleDescriptor build() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1991
            Set<Requires> requires = new HashSet<>(this.requires.values());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1992
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1993
            Set<String> packages = new HashSet<>(exportedAndOpenPackages());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1994
            packages.addAll(concealedPackages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  1995
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1996
            Set<Exports> exports = new HashSet<>(this.exports.values());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1997
            Set<Opens> opens = new HashSet<>(this.opens.values());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1998
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  1999
            Set<Provides> provides = new HashSet<>(this.provides.values());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2000
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2001
            return new ModuleDescriptor(name,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2002
                                        open,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2003
                                        automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2004
                                        synthetic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2005
                                        requires,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2006
                                        exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2007
                                        opens,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2008
                                        uses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2009
                                        provides,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2010
                                        version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2011
                                        mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2012
                                        osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2013
                                        osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2014
                                        osVersion,
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  2015
                                        packages,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2016
                                        hashes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2017
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2018
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2019
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2020
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2021
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2022
     * Compares this module descriptor to another.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2023
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2024
     * <p> Two {@code ModuleDescriptor} objects are compared by comparing their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2025
     * module name lexicographically.  Where the module names are equal then
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2026
     * the versions, if present, are compared. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2027
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2028
     * @apiNote For now, the natural ordering is not consistent with equals.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2029
     * If two module descriptors have equal module names, equal versions if
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2030
     * present, but their corresponding components are not equal, then they
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2031
     * will be considered equal by this method.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2032
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2033
     * @param  that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2034
     *         The object to which this module descriptor is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2035
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2036
     * @return A negative integer, zero, or a positive integer if this module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2037
     *         descriptor is less than, equal to, or greater than the given
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2038
     *         module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2039
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2040
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2041
    public int compareTo(ModuleDescriptor that) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2042
        int c = this.name().compareTo(that.name());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2043
        if (c != 0) return c;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2044
        if (version == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2045
            if (that.version == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2046
                return 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2047
            return -1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2048
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2049
        if (that.version == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2050
            return +1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2051
        return version.compareTo(that.version);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2052
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2053
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2054
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2055
     * Tests this module descriptor for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2056
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2057
     * <p> If the given object is not a {@code ModuleDescriptor} then this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2058
     * method returns {@code false}. Two module descriptors are equal if each
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2059
     * of their corresponding components is equal. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2060
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2061
     * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2062
     * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2063
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2064
     * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2065
     *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2066
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2067
     * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2068
     *          descriptor that is equal to this module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2069
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2070
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2071
    public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2072
        if (ob == this)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2073
            return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2074
        if (!(ob instanceof ModuleDescriptor))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2075
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2076
        ModuleDescriptor that = (ModuleDescriptor)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2077
        return (name.equals(that.name)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2078
                && open == that.open
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2079
                && automatic == that.automatic
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2080
                && synthetic == that.synthetic
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2081
                && requires.equals(that.requires)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2082
                && exports.equals(that.exports)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2083
                && opens.equals(that.opens)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2084
                && uses.equals(that.uses)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2085
                && provides.equals(that.provides)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2086
                && Objects.equals(version, that.version)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2087
                && Objects.equals(mainClass, that.mainClass)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2088
                && Objects.equals(osName, that.osName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2089
                && Objects.equals(osArch, that.osArch)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2090
                && Objects.equals(osVersion, that.osVersion)
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  2091
                && Objects.equals(packages, that.packages)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2092
                && Objects.equals(hashes, that.hashes));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2093
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2094
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2095
    private transient int hash;  // cached hash code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2096
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2097
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2098
     * Computes a hash code for this module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2099
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2100
     * <p> The hash code is based upon the components of the module descriptor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2101
     * and satisfies the general contract of the {@link Object#hashCode
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2102
     * Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2103
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2104
     * @return The hash-code value for this module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2105
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2106
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2107
    public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2108
        int hc = hash;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2109
        if (hc == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2110
            hc = name.hashCode();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2111
            hc = hc * 43 + Boolean.hashCode(open);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2112
            hc = hc * 43 + Boolean.hashCode(automatic);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2113
            hc = hc * 43 + Boolean.hashCode(synthetic);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2114
            hc = hc * 43 + requires.hashCode();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2115
            hc = hc * 43 + exports.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2116
            hc = hc * 43 + opens.hashCode();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2117
            hc = hc * 43 + uses.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2118
            hc = hc * 43 + provides.hashCode();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2119
            hc = hc * 43 + Objects.hashCode(version);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2120
            hc = hc * 43 + Objects.hashCode(mainClass);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2121
            hc = hc * 43 + Objects.hashCode(osName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2122
            hc = hc * 43 + Objects.hashCode(osArch);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2123
            hc = hc * 43 + Objects.hashCode(osVersion);
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  2124
            hc = hc * 43 + Objects.hashCode(packages);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2125
            hc = hc * 43 + Objects.hashCode(hashes);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2126
            if (hc == 0)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2127
                hc = -1;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2128
            hash = hc;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2129
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2130
        return hc;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2131
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2132
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2133
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2134
     * Returns a string describing this descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2135
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2136
     * @return A string describing this descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2137
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2138
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2139
    public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2140
        StringBuilder sb = new StringBuilder();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2141
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2142
        if (isOpen())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2143
            sb.append("open ");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2144
        sb.append("module { name: ").append(toNameAndVersion());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2145
        if (!requires.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2146
            sb.append(", ").append(requires);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2147
        if (!uses.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2148
            sb.append(", ").append(uses);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2149
        if (!exports.isEmpty())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2150
            sb.append(", exports: ").append(exports);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2151
        if (!opens.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2152
            sb.append(", opens: ").append(opens);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2153
        if (!provides.isEmpty()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2154
            sb.append(", provides: ").append(provides);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2155
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2156
        sb.append(" }");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2157
        return sb.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2158
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2159
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2160
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2161
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2162
     * Instantiates a builder to build a module descriptor.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2163
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2164
     * @param  name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2165
     *         The module name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2166
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2167
     * @return A new builder
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2168
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2169
     * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2170
     *         If the module name is {@code null} or is not a legal Java
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2171
     *         identifier
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2172
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2173
    public static Builder module(String name) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2174
        return new Builder(name, true);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2175
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2176
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2177
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2178
     * Instantiates a builder to build a module descriptor for an open module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2179
     * An open module does not declare any open packages but the resulting
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2180
     * module is treated as if all packages are open.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2181
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2182
     * <p> As an example, the following creates a module descriptor for an open
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2183
     * name "{@code m}" containing two packages, one of which is exported. </p>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2184
     * <pre>{@code
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2185
     *     ModuleDescriptor descriptor = ModuleDescriptor.openModule("m")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2186
     *         .requires("java.base")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2187
     *         .exports("p")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2188
     *         .contains("q")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2189
     *         .build();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2190
     * }</pre>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2191
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2192
     * @param  name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2193
     *         The module name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2194
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2195
     * @return A new builder that builds an open module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2196
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2197
     * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2198
     *         If the module name is {@code null} or is not a legal Java
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2199
     *         identifier
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2200
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2201
    public static Builder openModule(String name) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2202
        return new Builder(name, true).open(true);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2203
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2204
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2205
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2206
     * Instantiates a builder to build a module descriptor for an automatic
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2207
     * module. Automatic modules receive special treatment during resolution
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2208
     * (see {@link Configuration}) so that they read all other modules. When
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2209
     * Instantiated in the Java virtual machine as a {@link java.lang.reflect.Module}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2210
     * then the Module reads every unnamed module in the Java virtual machine.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2211
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2212
     * @param  name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2213
     *         The module name
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2214
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2215
     * @return A new builder that builds an automatic module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2216
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2217
     * @throws IllegalArgumentException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2218
     *         If the module name is {@code null} or is not a legal Java
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2219
     *         identifier
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2220
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2221
     * @see ModuleFinder#of(Path[])
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2222
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2223
    public static Builder automaticModule(String name) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2224
        return new Builder(name, true).automatic(true);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2225
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2226
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2227
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2228
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2229
     * Reads the binary form of a module declaration from an input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2230
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2231
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2232
     * <p> If the descriptor encoded in the input stream does not indicate a
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2233
     * set of packages in the module then the {@code packageFinder} will be
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2234
     * invoked. If the {@code packageFinder} throws an {@link UncheckedIOException}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2235
     * then {@link IOException} cause will be re-thrown. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2236
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2237
     * <p> If there are bytes following the module descriptor then it is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2238
     * implementation specific as to whether those bytes are read, ignored,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2239
     * or reported as an {@code InvalidModuleDescriptorException}. If this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2240
     * method fails with an {@code InvalidModuleDescriptorException} or {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2241
     * IOException} then it may do so after some, but not all, bytes have
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2242
     * been read from the input stream. It is strongly recommended that the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2243
     * stream be promptly closed and discarded if an exception occurs. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2244
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2245
     * @apiNote The {@code packageFinder} parameter is for use when reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2246
     * module descriptors from legacy module-artifact formats that do not
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2247
     * record the set of packages in the descriptor itself.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2248
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2249
     * @param  in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2250
     *         The input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2251
     * @param  packageFinder
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2252
     *         A supplier that can produce the set of packages
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2253
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2254
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2255
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2256
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2257
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2258
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2259
     *         If an I/O error occurs reading from the input stream or {@code
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2260
     *         UncheckedIOException} is thrown by the package finder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2261
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2262
    public static ModuleDescriptor read(InputStream in,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2263
                                        Supplier<Set<String>> packageFinder)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2264
        throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2265
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2266
        return ModuleInfo.read(in, requireNonNull(packageFinder));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2267
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2268
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2269
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2270
     * Reads the binary form of a module declaration from an input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2271
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2272
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2273
     * @param  in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2274
     *         The input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2275
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2276
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2277
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2278
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2279
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2280
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2281
     *         If an I/O error occurs reading from the input stream
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2282
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2283
    public static ModuleDescriptor read(InputStream in) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2284
        return ModuleInfo.read(in, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2285
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2286
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2287
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2288
     * Reads the binary form of a module declaration from a byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2289
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2290
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2291
     * <p> If the descriptor encoded in the byte buffer does not indicate a
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2292
     * set of packages then the {@code packageFinder} will be invoked. </p>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2293
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2294
     * <p> The module descriptor is read from the buffer stating at index
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2295
     * {@code p}, where {@code p} is the buffer's {@link ByteBuffer#position()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2296
     * position} when this method is invoked. Upon return the buffer's position
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2297
     * will be equal to {@code p + n} where {@code n} is the number of bytes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2298
     * read from the buffer. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2299
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2300
     * <p> If there are bytes following the module descriptor then it is
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2301
     * implementation specific as to whether those bytes are read, ignored,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2302
     * or reported as an {@code InvalidModuleDescriptorException}. If this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2303
     * method fails with an {@code InvalidModuleDescriptorException} then it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2304
     * may do so after some, but not all, bytes have been read. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2305
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2306
     * @apiNote The {@code packageFinder} parameter is for use when reading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2307
     * module descriptors from legacy module-artifact formats that do not
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2308
     * record the set of packages in the descriptor itself.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2309
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2310
     * @param  bb
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2311
     *         The byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2312
     * @param  packageFinder
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2313
     *         A supplier that can produce the set of packages
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2314
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2315
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2316
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2317
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2318
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2319
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2320
    public static ModuleDescriptor read(ByteBuffer bb,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2321
                                        Supplier<Set<String>> packageFinder)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2322
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2323
        return ModuleInfo.read(bb, requireNonNull(packageFinder));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2324
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2325
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2326
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2327
     * Reads the binary form of a module declaration from a byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2328
     * as a module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2329
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2330
     * @param  bb
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2331
     *         The byte buffer
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2332
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2333
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2334
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2335
     * @throws InvalidModuleDescriptorException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2336
     *         If an invalid module descriptor is detected
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2337
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2338
    public static ModuleDescriptor read(ByteBuffer bb) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2339
        return ModuleInfo.read(bb, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2340
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2341
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2342
    private static <K,V> Map<K,V> emptyOrUnmodifiableMap(Map<K,V> map) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2343
        if (map.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2344
            return Collections.emptyMap();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2345
        } else if (map.size() == 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2346
            Map.Entry<K, V> entry = map.entrySet().iterator().next();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2347
            return Collections.singletonMap(entry.getKey(), entry.getValue());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2348
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2349
            return Collections.unmodifiableMap(map);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2350
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2351
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2352
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2353
    private static <T> Set<T> emptyOrUnmodifiableSet(Set<T> set) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2354
        if (set.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2355
            return Collections.emptySet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2356
        } else if (set.size() == 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2357
            return Collections.singleton(set.iterator().next());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2358
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2359
            return Collections.unmodifiableSet(set);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2360
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2361
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2362
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2363
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2364
     * Returns a string containing the given set of modifiers and label.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2365
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2366
    private static <M> String toString(Set<M> mods, String what) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2367
        return (Stream.concat(mods.stream().map(e -> e.toString().toLowerCase()),
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2368
                              Stream.of(what)))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2369
                .collect(Collectors.joining(" "));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2370
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2371
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2372
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2373
        /**
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2374
         * Setup the shared secret to allow code in other packages access
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2375
         * private package methods in java.lang.module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2376
         */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2377
        jdk.internal.misc.SharedSecrets
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2378
            .setJavaLangModuleAccess(new jdk.internal.misc.JavaLangModuleAccess() {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2379
                @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2380
                public Builder newModuleBuilder(String mn, boolean strict) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2381
                    return new Builder(mn, strict);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2382
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2383
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2384
                @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2385
                public Builder newOpenModuleBuilder(String mn, boolean strict) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2386
                    return new Builder(mn, strict).open(true);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2387
                }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2388
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2389
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2390
                public Requires newRequires(Set<Requires.Modifier> ms, String mn) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2391
                    return new Requires(ms, mn, true);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2392
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2393
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2394
                @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2395
                public Exports newExports(Set<Exports.Modifier> ms, String source) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2396
                    return new Exports(ms, source, Collections.emptySet(), true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2397
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2398
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2399
                @Override
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2400
                public Exports newExports(Set<Exports.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2401
                                          String source,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2402
                                          Set<String> targets) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2403
                    return new Exports(ms, source, targets, true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2404
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2405
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2406
                @Override
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2407
                public Opens newOpens(Set<Opens.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2408
                                      String source,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2409
                                      Set<String> targets) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2410
                    return new Opens(ms, source, targets, true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2411
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2412
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2413
                @Override
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2414
                public Opens newOpens(Set<Opens.Modifier> ms, String source) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2415
                    return new Opens(ms, source, Collections.emptySet(), true);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2416
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2417
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2418
                @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2419
                public Provides newProvides(String service, List<String> providers) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2420
                    return new Provides(service, providers, true);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2421
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2422
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2423
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2424
                public Version newVersion(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2425
                    return new Version(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2426
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2427
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2428
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2429
                public ModuleDescriptor newModuleDescriptor(ModuleDescriptor md,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2430
                                                            Set<String> pkgs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2431
                    return new ModuleDescriptor(md, pkgs);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2432
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2433
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2434
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2435
                public ModuleDescriptor newModuleDescriptor(String name,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2436
                                                            boolean open,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2437
                                                            boolean automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2438
                                                            boolean synthetic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2439
                                                            Set<Requires> requires,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2440
                                                            Set<Exports> exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2441
                                                            Set<Opens> opens,
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37880
diff changeset
  2442
                                                            Set<String> uses,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2443
                                                            Set<Provides> provides,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2444
                                                            Version version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2445
                                                            String mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2446
                                                            String osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2447
                                                            String osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2448
                                                            String osVersion,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2449
                                                            Set<String> packages,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2450
                                                            ModuleHashes hashes,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2451
                                                            int hashCode) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2452
                    return new ModuleDescriptor(name,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2453
                                                open,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2454
                                                automatic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2455
                                                synthetic,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2456
                                                requires,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2457
                                                exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2458
                                                opens,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2459
                                                uses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2460
                                                provides,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2461
                                                version,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2462
                                                mainClass,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2463
                                                osName,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2464
                                                osArch,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2465
                                                osVersion,
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2466
                                                packages,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2467
                                                hashes,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2468
                                                hashCode,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2469
                                                false);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2470
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2471
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2472
                @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2473
                public Optional<ModuleHashes> hashes(ModuleDescriptor descriptor) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41817
diff changeset
  2474
                    return descriptor.hashes();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2475
                }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2476
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2477
                @Override
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2478
                public Configuration resolveRequiresAndUses(ModuleFinder finder,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2479
                                                            Collection<String> roots,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2480
                                                            boolean check,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2481
                                                            PrintStream traceOutput)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2482
                {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2483
                    return Configuration.resolveRequiresAndUses(finder, roots, check, traceOutput);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2484
                }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2485
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2486
                @Override
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2487
                public ModuleReference newPatchedModule(ModuleDescriptor descriptor,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2488
                                                        URI location,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2489
                                                        Supplier<ModuleReader> s) {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2490
                    return new ModuleReference(descriptor, location, s, true, null);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2491
                }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2492
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
  2493
                @Override
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 39050
diff changeset
  2494
                public ModuleFinder newModulePath(Runtime.Version version,
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 39050
diff changeset
  2495
                                                  boolean isLinkPhase,
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 39050
diff changeset
  2496
                                                  Path... entries) {
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 39050
diff changeset
  2497
                    return new ModulePath(version, isLinkPhase, entries);
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 39050
diff changeset
  2498
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2499
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2500
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2501
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
  2502
}