Class PackageUtils


  • @API(status=INTERNAL,
         since="1.0")
    public final class PackageUtils
    extends java.lang.Object
    Collection of utilities for working with packages.

    DISCLAIMER

    These utilities are intended solely for usage within the JUnit framework itself. Any usage by external parties is not supported. Use at your own risk!

    Since:
    1.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_PACKAGE_NAME  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Optional<java.lang.String> getAttribute​(java.lang.Class<?> type, java.lang.String name)
      Get the value of the specified attribute name, specified as a string, or an empty Optional if the attribute was not found.
      static java.util.Optional<java.lang.String> getAttribute​(java.lang.Class<?> type, java.util.function.Function<java.lang.Package,​java.lang.String> function)
      Get the package attribute for the supplied type using the supplied function.
      static java.util.Optional<java.lang.String> getModuleOrImplementationVersion​(java.lang.Class<?> type)
      Get the module or implementation version for the supplied type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_PACKAGE_NAME

        public static final java.lang.String DEFAULT_PACKAGE_NAME
        See Also:
        Constant Field Values
    • Method Detail

      • getAttribute

        public static java.util.Optional<java.lang.String> getAttribute​(java.lang.Class<?> type,
                                                                        java.util.function.Function<java.lang.Package,​java.lang.String> function)
        Get the package attribute for the supplied type using the supplied function.

        This method only returns a non-empty Optional value holder if the class loader for the supplied type created a Package object and the supplied function does not return null when applied.

        Parameters:
        type - the type to get the package attribute for
        function - a function that computes the package attribute value (e.g., Package::getImplementationTitle); never null
        Returns:
        an Optional containing the attribute value; never null but potentially empty
        Throws:
        PreconditionViolationException - if the supplied type or function is null
        See Also:
        Class.getPackage(), Package.getImplementationTitle(), Package.getImplementationVersion()
      • getAttribute

        public static java.util.Optional<java.lang.String> getAttribute​(java.lang.Class<?> type,
                                                                        java.lang.String name)
        Get the value of the specified attribute name, specified as a string, or an empty Optional if the attribute was not found. The attribute name is case-insensitive.

        This method also returns an empty Optional value holder if any exception is caught while loading the manifest file via the JAR file of the specified type.

        Parameters:
        type - the type to get the attribute for
        name - the attribute name as a string
        Returns:
        an Optional containing the attribute value; never null but potentially empty
        Throws:
        PreconditionViolationException - if the supplied type is null or the specified name is blank
        See Also:
        Manifest.getMainAttributes()
      • getModuleOrImplementationVersion

        @API(status=INTERNAL,
             since="1.11")
        public static java.util.Optional<java.lang.String> getModuleOrImplementationVersion​(java.lang.Class<?> type)
        Get the module or implementation version for the supplied type.

        The former is only available if the type is part of a versioned module on the module path; the latter only if the type is part of a JAR file with a manifest that contains an Implementation-Version attribute.

        Since:
        1.11