Package java.lang
Class Package
- java.lang.Object
-
- java.lang.Package
-
- All Implemented Interfaces:
AnnotatedElement
public class Package extends Object implements AnnotatedElement
Contains information about a Java package. This includes implementation and specification versions. Typically this information is retrieved from the manifest.Packages are managed by class loaders. All classes loaded by the same loader from the same package share a
Packageinstance.- See Also:
ClassLoader
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <A extends Annotation>
AgetAnnotation(Class<A> annotationType)Returns the annotation associated with the specified annotation type and this package, if present.Annotation[]getAnnotations()Returns an array of this package's annotations.Annotation[]getDeclaredAnnotations()Returns an array of this package's declared annotations.StringgetImplementationTitle()Returns the title of the implementation of this package, ornullif this is unknown.StringgetImplementationVendor()Returns the name of the vendor or organization that provides this implementation of the package, ornullif this is unknown.StringgetImplementationVersion()Returns the version of the implementation of this package, ornullif this is unknown.StringgetName()Returns the name of this package in the standard dot notation; for example: "java.lang".static PackagegetPackage(String packageName)Attempts to locate the requested package in the caller's class loader.static Package[]getPackages()Returns all the packages known to the caller's class loader.StringgetSpecificationTitle()Returns the title of the specification this package implements, ornullif this is unknown.StringgetSpecificationVendor()Returns the name of the vendor or organization that owns and maintains the specification this package implements, ornullif this is unknown.StringgetSpecificationVersion()Returns the version of the specification this package implements, ornullif this is unknown.inthashCode()Returns an integer hash code for this object.booleanisAnnotationPresent(Class<? extends Annotation> annotationType)Indicates whether the specified annotation is present.booleanisCompatibleWith(String version)Indicates whether this package's specification version is compatible with the specified version string.booleanisSealed()Indicates whether this package is sealed.booleanisSealed(URL url)Indicates whether this package is sealed with respect to the specified URL.StringtoString()Returns a string containing a concise, human-readable description of this object.
-
-
-
Method Detail
-
getAnnotation
public <A extends Annotation> A getAnnotation(Class<A> annotationType)
Returns the annotation associated with the specified annotation type and this package, if present.- Specified by:
getAnnotationin interfaceAnnotatedElement- Parameters:
annotationType- the annotation type to look for.- Returns:
- an instance of
Annotationornull. - See Also:
AnnotatedElement.getAnnotation(java.lang.Class)
-
getAnnotations
public Annotation[] getAnnotations()
Returns an array of this package's annotations.- Specified by:
getAnnotationsin interfaceAnnotatedElement- Returns:
- an array of all annotations for this element
-
getDeclaredAnnotations
public Annotation[] getDeclaredAnnotations()
Returns an array of this package's declared annotations. Package annotations aren't inherited, so this is equivalent togetAnnotations().- Specified by:
getDeclaredAnnotationsin interfaceAnnotatedElement- Returns:
- an array of annotations declared for this element
-
isAnnotationPresent
public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
Indicates whether the specified annotation is present.- Specified by:
isAnnotationPresentin interfaceAnnotatedElement- Parameters:
annotationType- the annotation type to look for.- Returns:
trueif the annotation is present;falseotherwise.- See Also:
AnnotatedElement.isAnnotationPresent(java.lang.Class)
-
getImplementationTitle
public String getImplementationTitle()
Returns the title of the implementation of this package, ornullif this is unknown. The format of this string is unspecified.- Returns:
- the implementation title, may be
null.
-
getImplementationVendor
public String getImplementationVendor()
Returns the name of the vendor or organization that provides this implementation of the package, ornullif this is unknown. The format of this string is unspecified.- Returns:
- the implementation vendor name, may be
null.
-
getImplementationVersion
public String getImplementationVersion()
Returns the version of the implementation of this package, ornullif this is unknown. The format of this string is unspecified.- Returns:
- the implementation version, may be
null.
-
getName
public String getName()
Returns the name of this package in the standard dot notation; for example: "java.lang".- Returns:
- the name of this package.
-
getPackage
public static Package getPackage(String packageName)
Attempts to locate the requested package in the caller's class loader. If no package information can be located,nullis returned.- Parameters:
packageName- the name of the package to find.- Returns:
- the requested package, or
null. - See Also:
ClassLoader.getPackage(java.lang.String)
-
getPackages
public static Package[] getPackages()
Returns all the packages known to the caller's class loader.- Returns:
- all the packages known to the caller's class loader.
- See Also:
ClassLoader.getPackages()
-
getSpecificationTitle
public String getSpecificationTitle()
Returns the title of the specification this package implements, ornullif this is unknown.- Returns:
- the specification title, may be
null.
-
getSpecificationVendor
public String getSpecificationVendor()
Returns the name of the vendor or organization that owns and maintains the specification this package implements, ornullif this is unknown.- Returns:
- the specification vendor name, may be
null.
-
getSpecificationVersion
public String getSpecificationVersion()
Returns the version of the specification this package implements, ornullif this is unknown. The version string is a sequence of non-negative integers separated by dots; for example: "1.2.3".- Returns:
- the specification version string, may be
null.
-
hashCode
public int hashCode()
Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
isCompatibleWith
public boolean isCompatibleWith(String version) throws NumberFormatException
Indicates whether this package's specification version is compatible with the specified version string. Version strings are compared by comparing each dot separated part of the version as an integer.- Parameters:
version- the version string to compare against.- Returns:
trueif the package versions are compatible;falseotherwise.- Throws:
NumberFormatException- if this package's version string or the one provided are not in the correct format.
-
isSealed
public boolean isSealed()
Indicates whether this package is sealed.- Returns:
trueif this package is sealed;falseotherwise.
-
isSealed
public boolean isSealed(URL url)
Indicates whether this package is sealed with respect to the specified URL.- Parameters:
url- the URL to check.- Returns:
trueif this package is sealed withurl;falseotherwise
-
toString
public String toString()
Description copied from class:ObjectReturns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod.
-
-