src/java.base/share/classes/jdk/internal/module/Builder.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 51327 a19fda433921
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51327
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
36511
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
package jdk.internal.module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.lang.module.ModuleDescriptor.Exports;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    29
import java.lang.module.ModuleDescriptor.Opens;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.ModuleDescriptor.Provides;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleDescriptor.Requires;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleDescriptor.Version;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    33
import java.util.List;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51327
diff changeset
    36
import jdk.internal.access.JavaLangModuleAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 51327
diff changeset
    37
import jdk.internal.access.SharedSecrets;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
/**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
    40
 * This builder is optimized for reconstituting the {@code ModuleDescriptor}s
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    41
 * for system modules.  The validation should be done at jlink time.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 * 1. skip name validation
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * 2. ignores dependency hashes.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * 3. ModuleDescriptor skips the defensive copy and directly uses the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 *    sets/maps created in this Builder.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * SystemModules should contain modules for the boot layer.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
final class Builder {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    51
    private static final JavaLangModuleAccess JLMA =
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
        SharedSecrets.getJavaLangModuleAccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    // Static cache of the most recently seen Version to cheaply deduplicate
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    // most Version objects.  JDK modules have the same version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    static Version cachedVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    58
    /**
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    59
     * Returns a {@link Requires} for a dependence on a module with the given
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    60
     * (and possibly empty) set of modifiers, and optionally the version
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    61
     * recorded at compile time.
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    62
     */
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    63
    public static Requires newRequires(Set<Requires.Modifier> mods,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    64
                                       String mn,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    65
                                       String compiledVersion)
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    66
    {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    67
        Version version = null;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    68
        if (compiledVersion != null) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    69
            // use the cached version if the same version string
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    70
            Version ver = cachedVersion;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    71
            if (ver != null && compiledVersion.equals(ver.toString())) {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    72
                version = ver;
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    73
            } else {
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    74
                version = Version.parse(compiledVersion);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    75
            }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    76
        }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    77
        return JLMA.newRequires(mods, mn, version);
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    78
    }
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    79
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    80
    /**
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    81
     * Returns a {@link Requires} for a dependence on a module with the given
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    82
     * (and possibly empty) set of modifiers, and optionally the version
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    83
     * recorded at compile time.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    84
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    85
    public static Requires newRequires(Set<Requires.Modifier> mods,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    86
                                       String mn)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    87
    {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    88
        return newRequires(mods, mn, null);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    89
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    90
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    91
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    92
     * Returns a {@link Exports} for a qualified export, with
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    93
     * the given (and possibly empty) set of modifiers,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    94
     * to a set of target modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    95
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    96
    public static Exports newExports(Set<Exports.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    97
                                     String pn,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
    98
                                     Set<String> targets) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
    99
        return JLMA.newExports(ms, pn, targets);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   100
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   101
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   102
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   103
     * Returns an {@link Opens} for an unqualified open with a given set of
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   104
     * modifiers.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   105
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   106
    public static Opens newOpens(Set<Opens.Modifier> ms, String pn) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   107
        return JLMA.newOpens(ms, pn);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   108
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   109
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   110
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   111
     * Returns an {@link Opens} for a qualified opens, with
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   112
     * the given (and possibly empty) set of modifiers,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   113
     * to a set of target modules.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   114
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   115
    public static Opens newOpens(Set<Opens.Modifier> ms,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   116
                                 String pn,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   117
                                 Set<String> targets) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   118
        return JLMA.newOpens(ms, pn, targets);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   119
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   120
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   121
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   122
     * Returns a {@link Exports} for an unqualified export with a given set
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   123
     * of modifiers.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   124
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   125
    public static Exports newExports(Set<Exports.Modifier> ms, String pn) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   126
        return JLMA.newExports(ms, pn);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   127
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   128
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   129
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   130
     * Returns a {@link Provides} for a service with a given list of
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   131
     * implementation classes.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   132
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   133
    public static Provides newProvides(String st, List<String> pcs) {
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   134
        return JLMA.newProvides(st, pcs);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   135
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   136
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    final String name;
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   138
    boolean open, synthetic, mandated;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   139
    Set<Requires> requires;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   140
    Set<Exports> exports;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   141
    Set<Opens> opens;
38888
dd584fbea6a2 8159334: ModuleDescriptor retains overlapping sets for all and concealed packages
redestad
parents: 37779
diff changeset
   142
    Set<String> packages;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
    Set<String> uses;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   144
    Set<Provides> provides;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
    Version version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    String mainClass;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   148
    Builder(String name) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        this.name = name;
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 47216
diff changeset
   150
        this.requires = Set.of();
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 47216
diff changeset
   151
        this.exports = Set.of();
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 47216
diff changeset
   152
        this.opens = Set.of();
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 47216
diff changeset
   153
        this.provides = Set.of();
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 47216
diff changeset
   154
        this.uses = Set.of();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   157
    Builder open(boolean value) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   158
        this.open = value;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   159
        return this;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   160
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   161
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   162
    Builder synthetic(boolean value) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   163
        this.synthetic = value;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   164
        return this;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   165
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   166
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   167
    Builder mandated(boolean value) {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   168
        this.mandated = value;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   173
     * Sets module exports.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   175
    public Builder exports(Exports[] exports) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   176
        this.exports = Set.of(exports);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   181
     * Sets module opens.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   182
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   183
    public Builder opens(Opens[] opens) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   184
        this.opens = Set.of(opens);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   185
        return this;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   186
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   187
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   188
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   189
     * Sets module requires.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   191
    public Builder requires(Requires[] requires) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   192
        this.requires = Set.of(requires);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   193
        return this;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   194
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   195
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   196
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   197
     * Adds a set of (possible empty) packages.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   198
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   199
    public Builder packages(Set<String> packages) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   200
        this.packages = packages;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
     * Sets the set of service dependences.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
    public Builder uses(Set<String> uses) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
        this.uses = uses;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   213
     * Sets module provides.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   215
    public Builder provides(Provides[] provides) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   216
        this.provides = Set.of(provides);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
     * Sets the module version.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
     * @throws IllegalArgumentException if {@code v} is null or cannot be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
     *         parsed as a version string
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
     * @see Version#parse(String)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
    public Builder version(String v) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        Version ver = cachedVersion;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        if (ver != null && v.equals(ver.toString())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
            version = ver;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            cachedVersion = version = Version.parse(v);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
     * Sets the module main class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
    public Builder mainClass(String mc) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        mainClass = mc;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
    /**
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   247
     * Returns an immutable set of the module modifiers derived from the flags.
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   248
     */
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   249
    private Set<ModuleDescriptor.Modifier> modifiers() {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   250
        int n = 0;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   251
        if (open) n++;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   252
        if (synthetic) n++;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   253
        if (mandated) n++;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   254
        if (n == 0) {
51327
a19fda433921 8209003: Consolidate use of empty collections in java.lang.module
redestad
parents: 47216
diff changeset
   255
            return Set.of();
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   256
        } else {
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   257
            ModuleDescriptor.Modifier[] mods = new ModuleDescriptor.Modifier[n];
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   258
            if (open) mods[--n] = ModuleDescriptor.Modifier.OPEN;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   259
            if (synthetic) mods[--n] = ModuleDescriptor.Modifier.SYNTHETIC;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   260
            if (mandated) mods[--n] = ModuleDescriptor.Modifier.MANDATED;
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   261
            return Set.of(mods);
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   262
        }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   263
    }
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   264
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   265
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
     * Builds a {@code ModuleDescriptor} from the components.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   268
    public ModuleDescriptor build(int hashCode) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
        assert name != null;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   270
        return JLMA.newModuleDescriptor(name,
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   271
                                        version,
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42703
diff changeset
   272
                                        modifiers(),
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
                                        requires,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   274
                                        exports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   275
                                        opens,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
                                        uses,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
                                        provides,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42338
diff changeset
   278
                                        packages,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                                        mainClass,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38888
diff changeset
   280
                                        hashCode);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
}