1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.lang; 26 27 import java.lang.module.Configuration; 28 import java.lang.module.ModuleReference; 29 import java.lang.reflect.Module; 30 import java.net.URI; 31 32 /** 33 * A NamedPackage represents a package by name in a specific module. 34 * 35 * A class loader will automatically create NamedPackage for each 36 * package when a class is defined. Package object is lazily 37 * defined until Class::getPackage, Package::getPackage(s), or 38 * ClassLoader::getDefinedPackage(s) method is called. 39 * 40 * NamedPackage allows ClassLoader to keep track of the runtime 41 * packages with minimal footprint and avoid constructing Package 42 * object. 43 */ 44 class NamedPackage { 45 private final String name; 46 private final Module module; 47 48 NamedPackage(String pn, Module module) { 49 if (pn.isEmpty() && module.isNamed()) { 50 throw new InternalError("unnamed package in " + module); 51 } 52 this.name = pn.intern(); 53 this.module = module; 54 } 55 56 /** 57 * Returns the name of this package. 58 */ 59 String packageName() { 60 return name; 61 } 62 63 /** 64 * Returns the module of this named package. 65 */ 66 Module module() { 67 return module; 68 } 69 70 /** 71 * Returns the location of the module if this named package is in 72 * a named module; otherwise, returns null. 73 */ 74 URI location() { 75 if (module.isNamed() && module.getLayer() != null) { 76 Configuration cf = module.getLayer().configuration(); 77 ModuleReference mref 78 = cf.findModule(module.getName()).getWhenPresent().reference(); 79 return mref.location().orElse(null); 80 } 81 return null; 82 } 83 84 /** 85 * Creates a Package object of the given name and module. 86 */ 87 static Package toPackage(String name, Module module) { 88 return new Package(name, module); 89 } 90 }