jdk/src/java.base/share/classes/java/lang/module/ModuleReference.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 40261 86a49ba76f52
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
/*
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
     2
 * Copyright (c) 2014, 2016, 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
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.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.Objects;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.function.Supplier;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    35
import jdk.internal.module.ModuleHashes.HashSupplier;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * A reference to a module's content.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * <p> A module reference contains the module's descriptor and its location, if
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * known.  It also has the ability to create a {@link ModuleReader} in order to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 * access the module's content, which may be inside the Java run-time system
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * itself or in an artifact such as a modular JAR file.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * @see ModuleFinder
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 * @see ModuleReader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * @since 9
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
public final class ModuleReference {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    private final ModuleDescriptor descriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    private final URI location;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    private final Supplier<ModuleReader> readerSupplier;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    57
    // true if this is a reference to a patched module
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    58
    private boolean patched;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    59
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    // the function that computes the hash of this module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    private final HashSupplier hasher;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
    63
    // cached hash to avoid needing to compute it many times
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
    64
    private byte[] cachedHash;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    66
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    67
    /**
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    68
     * Constructs a new instance of this class.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    69
     */
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    70
    ModuleReference(ModuleDescriptor descriptor,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    71
                    URI location,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    72
                    Supplier<ModuleReader> readerSupplier,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    73
                    boolean patched,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    74
                    HashSupplier hasher)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    75
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    76
    {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    77
        this.descriptor = Objects.requireNonNull(descriptor);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    78
        this.location = location;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    79
        this.readerSupplier = Objects.requireNonNull(readerSupplier);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    80
        this.patched = patched;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    81
        this.hasher = hasher;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    82
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    83
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
     * Constructs a new instance of this class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    ModuleReference(ModuleDescriptor descriptor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                    URI location,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                    Supplier<ModuleReader> readerSupplier,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                    HashSupplier hasher)
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    91
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
    93
        this(descriptor, location, readerSupplier, false, hasher);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
     * Constructs a new instance of this class.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     * <p> The {@code readSupplier} parameter is the supplier of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     * ModuleReader} that may be used to read the module content. Its {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
     * Supplier#get() get()} method throws {@link UncheckedIOException} if an
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
     * I/O error occurs opening the module content. The {@code get()} method
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
     * throws {@link SecurityException} if opening the module is denied by the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
     * security manager.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
     * @param descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
     *        The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
     * @param location
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
     *        The module location or {@code null} if not known
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
     * @param readerSupplier
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     *        The {@code Supplier} of the {@code ModuleReader}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    public ModuleReference(ModuleDescriptor descriptor,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                           URI location,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                           Supplier<ModuleReader> readerSupplier)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
    {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   118
        this(descriptor, location, readerSupplier, false, null);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
     * Returns the module descriptor.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
     * @return The module descriptor
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    public ModuleDescriptor descriptor() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        return descriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
     * Returns the location of this module's content, if known.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
     * <p> This URI, when present, is used as the {@linkplain
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
     * java.security.CodeSource#getLocation location} value of a {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
     * java.security.CodeSource CodeSource} so that a module's classes can be
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
     * granted specific permissions when loaded by a {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
     * java.security.SecureClassLoader SecureClassLoader}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
     * @return The location or an empty {@code Optional} if not known
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    public Optional<URI> location() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        return Optional.ofNullable(location);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
     * Opens the module content for reading.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
     * <p> This method opens the module content by invoking the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
     * Supplier#get() get()} method of the {@code readSupplier} specified at
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
     * construction time. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
     * @return A {@code ModuleReader} to read the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
     * @throws IOException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
     *         If an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
     *         If denied by the security manager
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
    public ModuleReader open() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
            return readerSupplier.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        } catch (UncheckedIOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            throw e.getCause();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
    /**
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   172
     * Returns {@code true} if this module has been patched via --patch-module.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   173
     */
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   174
    boolean isPatched() {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   175
        return patched;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   176
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   177
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   178
    /**
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   179
     * Returns the hash supplier for this module.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   180
     */
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   181
    HashSupplier hasher() {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   182
        return hasher;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   183
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   184
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   185
    /**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
   186
     * Computes the hash of this module. Returns {@code null} if the hash
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
   187
     * cannot be computed.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
     * @throws java.io.UncheckedIOException if an I/O error occurs
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
     */
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
   191
    byte[] computeHash(String algorithm) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
   192
        byte[] result = cachedHash;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
        if (result != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
            return result;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        if (hasher == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        cachedHash = result = hasher.generate(algorithm);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        return result;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
     * Computes a hash code for this module reference.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
     * <p> The hash code is based upon the components of the reference, and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
     * satisfies the general contract of the {@link Object#hashCode
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
     * Object.hashCode} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
     * @return The hash-code value for this module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
    public int hashCode() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
        int hc = hash;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        if (hc == 0) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
   214
            hc = descriptor.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
   215
            hc = 43 * hc + readerSupplier.hashCode();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
   216
            hc = 43 * hc + Objects.hashCode(location);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
   217
            hc = 43 * hc + Objects.hashCode(hasher);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40261
diff changeset
   218
            hc = 43 * hc + Boolean.hashCode(patched);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   219
            if (hc == 0)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   220
                hc = -1;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   221
            hash = hc;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        return hc;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   226
    private int hash;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   227
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
     * Tests this module reference for equality with the given object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
     * <p> If the given object is not a {@code ModuleReference} then this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
     * method returns {@code false}. Two module references are equal if their
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
     * module descriptors are equal, their locations are equal or both unknown,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
     * and were created with equal supplier objects to access the module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
     * content. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
     * <p> This method satisfies the general contract of the {@link
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
     * java.lang.Object#equals(Object) Object.equals} method. </p>
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
     * @param   ob
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
     *          the object to which this object is to be compared
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
     * @return  {@code true} if, and only if, the given object is a module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
     *          reference that is equal to this module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
    public boolean equals(Object ob) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        if (!(ob instanceof ModuleReference))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        ModuleReference that = (ModuleReference)ob;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        return Objects.equals(this.descriptor, that.descriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                && Objects.equals(this.location, that.location)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                && Objects.equals(this.readerSupplier, that.readerSupplier)
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   255
                && Objects.equals(this.hasher, that.hasher)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   256
                && this.patched == that.patched;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
     * Returns a string describing this module reference.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
     * @return A string describing this module reference
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
    public String toString() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
        return ("[module " + descriptor().name()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                + ", location=" + location + "]");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
}