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 } 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} 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. | 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 * @revised 9 115 * @spec JPMS 116 */ 117 public class Package extends NamedPackage implements java.lang.reflect.AnnotatedElement { 118 /** 119 * Return the name of this package. 120 * 121 * @return The fully-qualified name of this package as defined in section 6.5.3 of 122 * <cite>The Java™ Language Specification</cite>, 123 * for example, {@code java.lang} 124 */ 125 public String getName() { 126 return packageName(); 127 } 128 129 /** 130 * Return the title of the specification that this package implements. 131 * @return the specification title, {@code null} is returned if it is not known. 132 */ 133 public String getSpecificationTitle() { 134 return versionInfo.specTitle; 135 } 192 } 193 194 /** 195 * Return the version of this implementation. It consists of any string 196 * assigned by the vendor of this implementation and does 197 * not have any particular syntax specified or expected by the Java 198 * runtime. It may be compared for equality with other 199 * package version strings used for this implementation 200 * by this vendor for this package. 201 * @return the version of the implementation, {@code null} is returned if it is not known. 202 */ 203 public String getImplementationVersion() { 204 return versionInfo.implVersion; 205 } 206 207 /** 208 * Returns the vendor that implemented this package, {@code null} 209 * is returned if it is not known. 210 * @return the vendor that implemented this package, {@code null} 211 * is returned if it is not known. 212 * 213 * @revised 9 214 * @spec JPMS 215 */ 216 public String getImplementationVendor() { 217 return versionInfo.implVendor; 218 } 219 220 /** 221 * Returns true if this package is sealed. 222 * 223 * @return true if the package is sealed, false otherwise 224 */ 225 public boolean isSealed() { 226 return module().isNamed() || versionInfo.sealBase != null; 227 } 228 229 /** 230 * Returns true if this package is sealed with respect to the specified 231 * code source {@code url}. 232 * 233 * @param url the code source URL 234 * @return true if this package is sealed with respect to the given {@code url} 322 * @return The {@code Package} of the given name defined by the caller's 323 * class loader or its ancestors, or {@code null} if not found. 324 * 325 * @throws NullPointerException 326 * if {@code name} is {@code null}. 327 * 328 * @deprecated 329 * If multiple class loaders delegate to each other and define classes 330 * with the same package name, and one such loader relies on the lookup 331 * behavior of {@code getPackage} to return a {@code Package} from 332 * a parent loader, then the properties exposed by the {@code Package} 333 * may not be as expected in the rest of the program. 334 * For example, the {@code Package} will only expose annotations from the 335 * {@code package-info.class} file defined by the parent loader, even if 336 * annotations exist in a {@code package-info.class} file defined by 337 * a child loader. A more robust approach is to use the 338 * {@link ClassLoader#getDefinedPackage} method which returns 339 * a {@code Package} for the specified class loader. 340 * 341 * @see ClassLoader#getDefinedPackage 342 * 343 * @revised 9 344 * @spec JPMS 345 */ 346 @CallerSensitive 347 @Deprecated(since="9") 348 @SuppressWarnings("deprecation") 349 public static Package getPackage(String name) { 350 ClassLoader l = ClassLoader.getClassLoader(Reflection.getCallerClass()); 351 return l != null ? l.getPackage(name) : BootLoader.getDefinedPackage(name); 352 } 353 354 /** 355 * Returns all of the {@code Package}s defined by the caller's class loader 356 * and its ancestors. The returned array may contain more than one 357 * {@code Package} object of the same package name, each defined by 358 * a different class loader in the class loader hierarchy. 359 * <p> 360 * Calling this method is equivalent to calling {@link ClassLoader#getPackages} 361 * on a {@code ClassLoader} instance which is the caller's class loader. 362 * 363 * @return The array of {@code Package} objects defined by this 364 * class loader and its ancestors 365 * 366 * @see ClassLoader#getDefinedPackages 367 * 368 * @revised 9 369 * @spec JPMS 370 */ 371 @CallerSensitive 372 public static Package[] getPackages() { 373 ClassLoader cl = ClassLoader.getClassLoader(Reflection.getCallerClass()); 374 return cl != null ? cl.getPackages() : BootLoader.packages().toArray(Package[]::new); 375 } 376 377 /** 378 * Return the hash code computed from the package name. 379 * @return the hash code computed from the package name. 380 */ 381 @Override 382 public int hashCode(){ 383 return packageName().hashCode(); 384 } 385 386 /** 387 * Returns the string representation of this Package. 388 * Its value is the string "package " and the package name. 389 * If the package title is defined it is appended. |