src/java.base/share/classes/java/lang/NamedPackage.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44545 jdk/src/java.base/share/classes/java/lang/NamedPackage.java@83b611b88ac8
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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) 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
package java.lang;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.net.URI;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * A NamedPackage represents a package by name in a specific module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 * A class loader will automatically create NamedPackage for each
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 * package when a class is defined.  Package object is lazily
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 * defined until Class::getPackage, Package::getPackage(s), or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 * ClassLoader::getDefinedPackage(s) method is called.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * NamedPackage allows ClassLoader to keep track of the runtime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * packages with minimal footprint and avoid constructing Package
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * object.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
class NamedPackage {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
    private final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
    private final Module module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
    NamedPackage(String pn, Module module) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        if (pn.isEmpty() && module.isNamed()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
            throw new InternalError("unnamed package in  " + module);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        this.name = pn.intern();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
        this.module = module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
     * Returns the name of this package.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    String packageName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        return name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
     * Returns the module of this named package.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    Module module() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        return module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
     * Returns the location of the module if this named package is in
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
     * a named module; otherwise, returns null.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
    URI location() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        if (module.isNamed() && module.getLayer() != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
            Configuration cf = module.getLayer().configuration();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            ModuleReference mref
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                = cf.findModule(module.getName()).get().reference();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            return mref.location().orElse(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
     * Creates a Package object of the given name and module.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    static Package toPackage(String name, Module module) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        return new Package(name, module);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
}