Class Packages

java.lang.Object
com.globalmentor.java.Packages

public class Packages extends Object
Utilities for manipulating Java packages.
Author:
Garret Wilson
  • Method Details

    • asPackage

      public static Package asPackage(URI resourceURI) throws ClassNotFoundException
      Determines the Java package represented by the given URI. A URI represents a Java package if it has a "java" scheme in the form java:/com/example/package/.
      Parameters:
      resourceURI - The URI which is expected to represent a Java package, or null.
      Returns:
      The Java package represented by the given URI, or null if the URI is not a java: URI.
      Throws:
      IllegalArgumentException - if the given URI represents a Java package that does not have the correct syntax, e.g. it does not have an absolute collection path.
      ClassNotFoundException - if the package represented by the given URI could not be found.
      See Also:
    • createJavaURI

      public static URI createJavaURI(Package objectPackage)
      Creates a Java URI for a Java package using the "java" scheme in the form java:/com/example/package/.
      Parameters:
      objectPackage - The package to use in creating the java: URI.
      Returns:
      A java: URI based upon the given class.
      Throws:
      NullPointerException - if the given package name is null.
    • createJavaURI

      public static URI createJavaURI(String objectPackageName)
      Creates a Java URI for a named Java package using the "java" scheme in the form java:/com/example/package/.
      Parameters:
      objectPackageName - The name of the package to use in creating the java: URI.
      Returns:
      A java: URI based upon the given class.
      Throws:
      NullPointerException - if the given package name is null.
    • getFullName

      public static String getFullName(Package objectPackage, String localName)
      Creates a full name given package and a local name. For example, a package of com.example and a local name of Bar will result in a full name of com.example.Bar.
      Parameters:
      objectPackage - The object to supply the package name.
      localName - The local name for constructing the full name within the package.
      Returns:
      A full class name in the given package and the given local name.
      See Also:
    • isInsidePackage

      public static boolean isInsidePackage(Package parentPackage, Class<?> childClass)
      Determines if the given class is inside the named package.
      Parameters:
      parentPackage - The package against which to check.
      childClass - The class to check to see if it is inside the given package.
      Returns:
      true if the given class is inside the given package.
      Throws:
      NullPointerException - if the given package and/or class is null.
    • isInsidePackage

      public static boolean isInsidePackage(Package parentPackage, Package childPackage)
      Determines if the given package is inside the named package.
      Parameters:
      parentPackage - The package against which to check.
      childPackage - The package to check to see if it is inside the given package.
      Returns:
      true if the given child package is inside the given parent package.
      Throws:
      NullPointerException - if the given parent package and/or child package is null.
    • isInsidePackage

      public static boolean isInsidePackage(String packageName, String fullName)
      Determines if the given full name (which can represent the full name of a package or a class) is inside the named package.
      Parameters:
      packageName - The name of the package against which to check (e.g. com.example).
      fullName - The full package or class name to check to see if it is inside the named package (e.g. com.example.foo or com.example.Bar or com.example.foo.Bar).
      Returns:
      true if the full-named entity is inside the named package.
      Throws:
      NullPointerException - if the given package name and/or full name is null.
      See Also:
    • getPackageName

      public static String getPackageName(String fullName)
      Determines the package of the given full name (which can represent the full name of a package or a class).
      Parameters:
      fullName - The full package or class name (e.g. Example or com.example.foo or com.example.Bar or com.example.foo.Bar).
      Returns:
      The name of the direct enclosing package, or null if the full-named entity is not in a package.
      Throws:
      NullPointerException - if the given full name is null.
      See Also:
    • getParentPackageNames

      public static List<String> getParentPackageNames(String packageName)
      Retrieves a list of names of all parent packages of the given named package.
      Parameters:
      packageName - The name of the package for which parent packages should be listed.
      Returns:
      The list, possibly empty, of names of all parent packages, in order of child to parent packages.
      Throws:
      NullPointerException - if the given package name is null.
      See Also: