1 /* 2 * Copyright (c) 1997, 2015, 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 26 package java.lang; 27 28 import java.lang.annotation.Annotation; 29 import java.lang.reflect.AnnotatedElement; 30 import java.lang.reflect.Module; 31 import java.net.MalformedURLException; 32 import java.net.URI; 33 import java.net.URL; 34 import java.security.AccessController; 35 import java.security.PrivilegedAction; 36 import java.util.Objects; 37 38 import jdk.internal.loader.BootLoader; 39 import jdk.internal.reflect.CallerSensitive; 40 import jdk.internal.reflect.Reflection; 41 42 43 /** 44 * Represents metadata about a run-time package associated with a class loader. 45 * Metadata includes annotations, versioning, and sealing. 46 * <p> 47 * Annotations for the run-time package are read from {@code package-info.class} 48 * at the same code source as classes in the run-time package. 49 * <p> 50 * The set of classes that make up the run-time package may implement a 51 * particular specification. The specification title, version, and vendor 52 * (indicating the owner/maintainer of the specification) can be provided 53 * when the {@code Package} is defined. An application can ask if the 54 * {@code Package} is compatible with a particular specification version 55 * by using the {@link #isCompatibleWith Package.isCompatibleWith(String)} 56 * method. In addition, information about the actual classes that make up the 57 * run-time package can be provided when the Package is defined. 58 * This information consists of an implementation title, version, and vendor 59 * (indicating the supplier of the classes). 60 * <p> 61 * A {@code Package} may be explicitly defined with 62 * the {@link ClassLoader#definePackage(String, String, String, String, 63 * String, String, String, URL)} method. 64 * The caller supplies the specification and implementation titles, versions, and 65 * vendors. The caller also indicates whether the package is 66 * {@linkplain java.util.jar.Attributes.Name#SEALED sealed}. 67 * If a {@code Package} is not explicitly defined for a run-time package when 68 * a class in that run-time package is defined, then a {@code Package} is 69 * automatically defined by the class's defining class loader, as follows. 70 * <p> 71 * A {@code Package} automatically defined for classes in a named module has 72 * the following properties: 73 * <ul> 74 * <li>The name of the package is derived from the {@linkplain Class#getName() binary names} 75 * of the classes. Since classes in a named module must be in a named package, 76 * the derived name is never empty.</li> 77 * <li>The package is sealed with the {@linkplain java.lang.module.ModuleReference#location() 78 * module location} as the code source, if known.</li> 79 * <li>The specification and implementation titles, versions, and vendors 80 * are unspecified.</li> 81 * <li>Any annotations on the package are read from {@code package-info.class} 82 * as specified above.</li> 83 * </ul> 84 * <p> 85 * A {@code Package} automatically defined for classes in an unnamed module 86 * has the following properties: 87 * <ul> 88 * <li>The name of the package is either {@code ""} (for classes in an unnamed package) 89 * or derived from the {@linkplain Class#getName() binary names} of the classes 90 * (for classes in a named package).</li> 91 * <li>The package is not sealed.</li> 92 * <li>The specification and implementation titles, versions, and vendors 93 * are unspecified.</li> 94 * <li>Any annotations on the package are read from {@code package-info.class} 95 * as specified above.</li> 96 * </ul> 97 * 98 * <p> 99 * A {@code Package} can be obtained with the {@link Package#getPackage 100 * Package.getPackage(String)} and {@link ClassLoader#getDefinedPackage 101 * ClassLoader.getDefinedPackage(String)} methods. 102 * Every {@code Package} defined by a class loader can be obtained 103 * with the {@link Package#getPackages Package.getPackages()} and 104 * {@link ClassLoader#getDefinedPackages} methods. 105 * 106 * @jvms 5.3 Run-time package 107 * @see <a href="../../../technotes/guides/jar/jar.html#versioning"> 108 * The JAR File Specification: Package Versioning</a> 109 * @see <a href="../../../technotes/guides/jar/jar.html#sealing"> 110 * The JAR File Specification: Package Sealing</a> 111 * @see ClassLoader#definePackage(String, String, String, String, String, String, String, URL) 112 * 113 * @since 1.2 114 */ 115 public class Package extends NamedPackage implements java.lang.reflect.AnnotatedElement { 116 /** 117 * Return the name of this package. 118 * 119 * @return The fully-qualified name of this package as defined in section 6.5.3 of 120 * <cite>The Java™ Language Specification</cite>, 121 * for example, {@code java.lang} 122 */ 123 public String getName() { 124 return packageName(); 125 } 126 127 /** 128 * Return the title of the specification that this package implements. 129 * @return the specification title, {@code null} is returned if it is not known. 130 */ 131 public String getSpecificationTitle() { 132 return versionInfo.specTitle; 133 } 134 135 /** 136 * Returns the version number of the specification 137 * that this package implements. 138 * This version string must be a sequence of non-negative decimal 139 * integers separated by "."'s and may have leading zeros. 140 * When version strings are compared the most significant 141 * numbers are compared. 142 * 143 * 144 * <p>Specification version numbers use a syntax that consists of non-negative 145 * decimal integers separated by periods ".", for example "2.0" or 146 * "1.2.3.4.5.6.7". This allows an extensible number to be used to represent 147 * major, minor, micro, etc. versions. The version specification is described 148 * by the following formal grammar: 149 * <blockquote> 150 * <dl> 151 * <dt><i>SpecificationVersion:</i> 152 * <dd><i>Digits RefinedVersion<sub>opt</sub></i> 153 154 * <dt><i>RefinedVersion:</i> 155 * <dd>{@code .} <i>Digits</i> 156 * <dd>{@code .} <i>Digits RefinedVersion</i> 157 * 158 * <dt><i>Digits:</i> 159 * <dd><i>Digit</i> 160 * <dd><i>Digits</i> 161 * 162 * <dt><i>Digit:</i> 163 * <dd>any character for which {@link Character#isDigit} returns {@code true}, 164 * e.g. 0, 1, 2, ... 165 * </dl> 166 * </blockquote> 167 * 168 * @return the specification version, {@code null} is returned if it is not known. 169 */ 170 public String getSpecificationVersion() { 171 return versionInfo.specVersion; 172 } 173 174 /** 175 * Return the name of the organization, vendor, 176 * or company that owns and maintains the specification 177 * of the classes that implement this package. 178 * @return the specification vendor, {@code null} is returned if it is not known. 179 */ 180 public String getSpecificationVendor() { 181 return versionInfo.specVendor; 182 } 183 184 /** 185 * Return the title of this package. 186 * @return the title of the implementation, {@code null} is returned if it is not known. 187 */ 188 public String getImplementationTitle() { 189 return versionInfo.implTitle; 190 } 191 192 /** 193 * Return the version of this implementation. It consists of any string 194 * assigned by the vendor of this implementation and does 195 * not have any particular syntax specified or expected by the Java 196 * runtime. It may be compared for equality with other 197 * package version strings used for this implementation 198 * by this vendor for this package. 199 * @return the version of the implementation, {@code null} is returned if it is not known. 200 */ 201 public String getImplementationVersion() { 202 return versionInfo.implVersion; 203 } 204 205 /** 206 * Returns the vendor that implemented this package, {@code null} 207 * is returned if it is not known. 208 * @return the vendor that implemented this package, {@code null} 209 * is returned if it is not known. 210 */ 211 public String getImplementationVendor() { 212 return versionInfo.implVendor; 213 } 214 215 /** 216 * Returns true if this package is sealed. 217 * 218 * @return true if the package is sealed, false otherwise 219 */ 220 public boolean isSealed() { 221 return module().isNamed() || versionInfo.sealBase != null; 222 } 223 224 /** 225 * Returns true if this package is sealed with respect to the specified 226 * code source {@code url}. 227 * 228 * @param url the code source URL 229 * @return true if this package is sealed with respect to the given {@code url} 230 */ 231 public boolean isSealed(URL url) { 232 Objects.requireNonNull(url); 233 234 URL sealBase = null; 235 if (versionInfo != VersionInfo.NULL_VERSION_INFO) { 236 sealBase = versionInfo.sealBase; 237 } else { 238 try { 239 URI uri = location(); 240 sealBase = uri != null ? uri.toURL() : null; 241 } catch (MalformedURLException e) { 242 } 243 } 244 return url.equals(sealBase); 245 } 246 247 /** 248 * Compare this package's specification version with a 249 * desired version. It returns true if 250 * this packages specification version number is greater than or equal 251 * to the desired version number. <p> 252 * 253 * Version numbers are compared by sequentially comparing corresponding 254 * components of the desired and specification strings. 255 * Each component is converted as a decimal integer and the values 256 * compared. 257 * If the specification value is greater than the desired 258 * value true is returned. If the value is less false is returned. 259 * If the values are equal the period is skipped and the next pair of 260 * components is compared. 261 * 262 * @param desired the version string of the desired version. 263 * @return true if this package's version number is greater 264 * than or equal to the desired version number 265 * 266 * @exception NumberFormatException if the current version is not known or 267 * the desired or current version is not of the correct dotted form. 268 */ 269 public boolean isCompatibleWith(String desired) 270 throws NumberFormatException 271 { 272 if (versionInfo.specVersion == null || versionInfo.specVersion.length() < 1) { 273 throw new NumberFormatException("Empty version string"); 274 } 275 276 String [] sa = versionInfo.specVersion.split("\\.", -1); 277 int [] si = new int[sa.length]; 278 for (int i = 0; i < sa.length; i++) { 279 si[i] = Integer.parseInt(sa[i]); 280 if (si[i] < 0) 281 throw NumberFormatException.forInputString("" + si[i]); 282 } 283 284 String [] da = desired.split("\\.", -1); 285 int [] di = new int[da.length]; 286 for (int i = 0; i < da.length; i++) { 287 di[i] = Integer.parseInt(da[i]); 288 if (di[i] < 0) 289 throw NumberFormatException.forInputString("" + di[i]); 290 } 291 292 int len = Math.max(di.length, si.length); 293 for (int i = 0; i < len; i++) { 294 int d = (i < di.length ? di[i] : 0); 295 int s = (i < si.length ? si[i] : 0); 296 if (s < d) 297 return false; 298 if (s > d) 299 return true; 300 } 301 return true; 302 } 303 304 /** 305 * Finds a package by name in the caller's class loader and its 306 * ancestors. 307 * <p> 308 * If the caller's class loader defines a {@code Package} of the given name, 309 * the {@code Package} is returned. Otherwise, the ancestors of the 310 * caller's class loader are searched recursively (parent by parent) 311 * for a {@code Package} of the given name. 312 * <p> 313 * Calling this method is equivalent to calling {@link ClassLoader#getPackage} 314 * on a {@code ClassLoader} instance which is the caller's class loader. 315 * 316 * @param name A package name, such as "{@code java.lang}". 317 * @return The {@code Package} of the given name defined by the caller's 318 * class loader or its ancestors, or {@code null} if not found. 319 * 320 * @throws NullPointerException 321 * if {@code name} is {@code null}. 322 * 323 * @deprecated 324 * If multiple class loaders delegate to each other and define classes 325 * with the same package name, and one such loader relies on the lookup 326 * behavior of {@code getPackage} to return a {@code Package} from 327 * a parent loader, then the properties exposed by the {@code Package} 328 * may not be as expected in the rest of the program. 329 * For example, the {@code Package} will only expose annotations from the 330 * {@code package-info.class} file defined by the parent loader, even if 331 * annotations exist in a {@code package-info.class} file defined by 332 * a child loader. A more robust approach is to use the 333 * {@link ClassLoader#getDefinedPackage} method which returns 334 * a {@code Package} for the specified class loader. 335 * 336 * @see ClassLoader#getDefinedPackage 337 */ 338 @CallerSensitive 339 @Deprecated(since="9") 340 @SuppressWarnings("deprecation") 341 public static Package getPackage(String name) { 342 ClassLoader l = ClassLoader.getClassLoader(Reflection.getCallerClass()); 343 return l != null ? l.getPackage(name) : BootLoader.getDefinedPackage(name); 344 } 345 346 /** 347 * Returns all of the {@code Package}s defined by the caller's class loader 348 * and its ancestors. The returned array may contain more than one 349 * {@code Package} object of the same package name, each defined by 350 * a different class loader in the class loader hierarchy. 351 * <p> 352 * Calling this method is equivalent to calling {@link ClassLoader#getPackages} 353 * on a {@code ClassLoader} instance which is the caller's class loader. 354 * 355 * @return The array of {@code Package} objects defined by this 356 * class loader and its ancestors 357 * 358 * @see ClassLoader#getDefinedPackages 359 */ 360 @CallerSensitive 361 public static Package[] getPackages() { 362 ClassLoader cl = ClassLoader.getClassLoader(Reflection.getCallerClass()); 363 return cl != null ? cl.getPackages() : BootLoader.packages().toArray(Package[]::new); 364 } 365 366 /** 367 * Return the hash code computed from the package name. 368 * @return the hash code computed from the package name. 369 */ 370 @Override 371 public int hashCode(){ 372 return packageName().hashCode(); 373 } 374 375 /** 376 * Returns the string representation of this Package. 377 * Its value is the string "package " and the package name. 378 * If the package title is defined it is appended. 379 * If the package version is defined it is appended. 380 * @return the string representation of the package. 381 */ 382 @Override 383 public String toString() { 384 String spec = versionInfo.specTitle; 385 String ver = versionInfo.specVersion; 386 if (spec != null && spec.length() > 0) 387 spec = ", " + spec; 388 else 389 spec = ""; 390 if (ver != null && ver.length() > 0) 391 ver = ", version " + ver; 392 else 393 ver = ""; 394 return "package " + packageName() + spec + ver; 395 } 396 397 private Class<?> getPackageInfo() { 398 if (packageInfo == null) { 399 // find package-info.class defined by loader 400 String cn = packageName() + ".package-info"; 401 Module module = module(); 402 PrivilegedAction<ClassLoader> pa = module::getClassLoader; 403 ClassLoader loader = AccessController.doPrivileged(pa); 404 Class<?> c; 405 if (loader != null) { 406 c = loader.loadClass(module, cn); 407 } else { 408 c = BootLoader.loadClass(module, cn); 409 } 410 411 if (c != null) { 412 packageInfo = c; 413 } else { 414 // store a proxy for the package info that has no annotations 415 class PackageInfoProxy {} 416 packageInfo = PackageInfoProxy.class; 417 } 418 } 419 return packageInfo; 420 } 421 422 /** 423 * @throws NullPointerException {@inheritDoc} 424 * @since 1.5 425 */ 426 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { 427 return getPackageInfo().getAnnotation(annotationClass); 428 } 429 430 /** 431 * {@inheritDoc} 432 * @throws NullPointerException {@inheritDoc} 433 * @since 1.5 434 */ 435 @Override 436 public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { 437 return AnnotatedElement.super.isAnnotationPresent(annotationClass); 438 } 439 440 /** 441 * @throws NullPointerException {@inheritDoc} 442 * @since 1.8 443 */ 444 @Override 445 public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) { 446 return getPackageInfo().getAnnotationsByType(annotationClass); 447 } 448 449 /** 450 * @since 1.5 451 */ 452 public Annotation[] getAnnotations() { 453 return getPackageInfo().getAnnotations(); 454 } 455 456 /** 457 * @throws NullPointerException {@inheritDoc} 458 * @since 1.8 459 */ 460 @Override 461 public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) { 462 return getPackageInfo().getDeclaredAnnotation(annotationClass); 463 } 464 465 /** 466 * @throws NullPointerException {@inheritDoc} 467 * @since 1.8 468 */ 469 @Override 470 public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass) { 471 return getPackageInfo().getDeclaredAnnotationsByType(annotationClass); 472 } 473 474 /** 475 * @since 1.5 476 */ 477 public Annotation[] getDeclaredAnnotations() { 478 return getPackageInfo().getDeclaredAnnotations(); 479 } 480 481 /** 482 * Construct a package instance for an unnamed module 483 * with the specified version information. 484 * 485 * @apiNote 486 * This method should not be called to define a Package for named module. 487 * 488 * @param name the name of the package 489 * @param spectitle the title of the specification 490 * @param specversion the version of the specification 491 * @param specvendor the organization that maintains the specification 492 * @param impltitle the title of the implementation 493 * @param implversion the version of the implementation 494 * @param implvendor the organization that maintains the implementation 495 * @param sealbase code source where this Package comes from 496 * @param loader defining class loader 497 */ 498 Package(String name, 499 String spectitle, String specversion, String specvendor, 500 String impltitle, String implversion, String implvendor, 501 URL sealbase, ClassLoader loader) 502 { 503 super(Objects.requireNonNull(name), 504 loader != null ? loader.getUnnamedModule() 505 : BootLoader.getUnnamedModule()); 506 507 this.versionInfo = VersionInfo.getInstance(spectitle, specversion, 508 specvendor, impltitle, 509 implversion, implvendor, 510 sealbase); 511 } 512 513 Package(String name, Module module) { 514 super(name, module); 515 this.versionInfo = VersionInfo.NULL_VERSION_INFO; 516 } 517 518 /* 519 * Versioning information. Only for packages in unnamed modules. 520 */ 521 static class VersionInfo { 522 static final VersionInfo NULL_VERSION_INFO 523 = new VersionInfo(null, null, null, null, null, null, null); 524 525 private final String specTitle; 526 private final String specVersion; 527 private final String specVendor; 528 private final String implTitle; 529 private final String implVersion; 530 private final String implVendor; 531 private final URL sealBase; 532 533 static VersionInfo getInstance(String spectitle, String specversion, 534 String specvendor, String impltitle, 535 String implversion, String implvendor, 536 URL sealbase) { 537 if (spectitle == null && specversion == null && 538 specvendor == null && impltitle == null && 539 implvendor == null && sealbase == null) { 540 return NULL_VERSION_INFO; 541 } 542 return new VersionInfo(spectitle, specversion, specvendor, 543 impltitle, implversion, implvendor, 544 sealbase); 545 } 546 547 private VersionInfo(String spectitle, String specversion, 548 String specvendor, String impltitle, 549 String implversion, String implvendor, 550 URL sealbase) 551 { 552 this.implTitle = impltitle; 553 this.implVersion = implversion; 554 this.implVendor = implvendor; 555 this.specTitle = spectitle; 556 this.specVersion = specversion; 557 this.specVendor = specvendor; 558 this.sealBase = sealbase; 559 } 560 } 561 562 private final VersionInfo versionInfo; 563 private Class<?> packageInfo; 564 }