Team LiB
Previous Section Next Section


Java 1.2

This class represents a Java package. You can obtain the Package object for a given Class by calling the getPackage( ) method of the Class object. The static Package.getPackage( ) method returns a Package object for the named package, if any such package has been loaded by the current class loader. Similarly, the static Package.getPackages( ) returns all Package objects that have been loaded by the current class loader. Note that a Package object is not defined unless at least one class has been loaded from that package. Although you can obtain the Package of a given Class, you cannot obtain an array of Class objects contained in a specified Package.

If the classes that comprise a package are contained in a JAR file that has the appropriate attributes set in its manifest file, the Package object allows you to query the title, vendor, and version of both the package specification and the package implementation; all six values are strings. The specification version string has a special format. It consists of one or more integers, separated from each other by periods. Each integer can have leading zeros, but is not considered an octal digit. Increasing numbers indicate later versions. The isCompatibleWith( ) method calls getSpecificationVersion( ) to obtain the specification version and compares it with the version string supplied as an argument. If the package-specification version is the same as or greater than the specified string, isCompatibleWith( ) returns TRue. This allows you to test whether the version of a package (typically a standard extension) is new enough for the purposes of your application.

Packages may be sealed, which means that all classes in the package must come from the same JAR file. If a package is sealed, the no-argument version of isSealed( ) returns true. The one-argument version of isSealed( ) returns TRue if the specified URL represents the JAR file from which the package is loaded.

Figure 10-51. java.lang.Package

public class Package implements java.lang.reflect.AnnotatedElement {
// No Constructor
// Public Class Methods
     public static Package getPackage(String name);  
     public static Package[ ] getPackages( );  
// Public Instance Methods
     public String getImplementationTitle( );  
     public String getImplementationVendor( );  
     public String getImplementationVersion( );  
     public String getName( );  
     public String getSpecificationTitle( );  
     public String getSpecificationVendor( );  
     public String getSpecificationVersion( );  
     public boolean isCompatibleWith(String desired) throws NumberFormatException;  
     public boolean isSealed( );  
     public boolean isSealed( url);  
// Methods Implementing AnnotatedElement
5.0  public <A extends java.lang.annotation.Annotation> A getAnnotation(Class<A> 
5.0  public java.lang.annotation.Annotation[ ] getAnnotations( );  
5.0  public java.lang.annotation.Annotation[ ] getDeclaredAnnotations( );  
5.0  public boolean isAnnotationPresent(Class<? extends java.lang.annotation.
Annotation> annotationClass);  
// Public Methods Overriding Object
     public int hashCode( );  
     public String toString( );  

Returned By

Class.getPackage( ), ClassLoader.{definePackage( ), getPackage( ), getPackages( )}, )

    Team LiB
    Previous Section Next Section