Package Reflection

The java.lang.Package class provides access to information about a package.

Package objects contain version information about a Java package.

Get package for a name

static Package getPackage(String name)
Find a package by name in the callers ClassLoader instance.

Get all packages

static Package[] getPackages()
Get all the packages currently known for the caller's ClassLoader instance.

Get the annotations

<A extends Annotation>A getAnnotation(Class<A> annotationClass)
Returns this element's annotation for the specified type if such an annotation is present, else null.
Annotation[] getAnnotations()
Returns all annotations present on this element.
Annotation[] getDeclaredAnnotations()
Returns all annotations that are directly present on this element.

Get implementation title, vendor and version

String getImplementationTitle()
Return the title of this package.
String getImplementationVendor()
Returns the name of the organization, vendor or company that provided this implementation.
String getImplementationVersion()
Return the version of this implementation.

Get the package name

String getName()
Return the name of this package.

Get the specification title, vendor and version

String getSpecificationTitle()
Return the title of the specification.
String getSpecificationVendor()
Return the name of the organization, vendor.
String getSpecificationVersion()
Returns the version number.

Is Annotation present

boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
Returns true if an annotation for the specified type is present on this element, else false.

Is this Package compatible with passed in package

boolean isCompatibleWith(String desired)
Compare this package's specification version with a desired version.

Is this Package sealed

boolean isSealed()
Returns true if this package is sealed.
boolean isSealed(URL url)
Returns true if this package is sealed with respect to the specified code source url.

Get the string representation of the Package

String toString()
Returns the string representation of this Package.

Revised from Open JDK source code


public class Main {
  public static void main(String[] args) {
    Package pkg = Package.getPackage("java.lang");
    System.out.println("Name: " + pkg.getName());
    System.out.println("Implementation title: " + pkg.getImplementationTitle());
    System.out.println("Implementation vendor: " + pkg.getImplementationVendor());
    System.out.println("Implementation version: " + pkg.getImplementationVersion());
    System.out.println("Specification title: " + pkg.getSpecificationTitle());
    System.out.println("Specification vendor: " + pkg.getSpecificationVendor());
    System.out.println("Specification version: " + pkg.getSpecificationVersion());
    System.out.println("Sealed: " + pkg.isSealed());
   System.out.println("Compatible: " + pkg.isCompatibleWith("1.3"));
  }
}
Home 
  Java Book 
    Reflection  

Package:
  1. Package Reflection
  2. Get package for a name
  3. Get all packages
  4. Get the annotations
  5. Get implementation title, vendor and version
  6. Get the package name
  7. Get the specification title, vendor and version
  8. Get the string representation of the Package