Class PackageNameUtils

java.lang.Object
org.openrewrite.java.internal.PackageNameUtils

public final class PackageNameUtils extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    getPackageName(org.openrewrite.java.tree.J.Package pkg)
    Returns the canonical dotted name of a package declaration (e.g.
    static boolean
    isPrefixOf(org.openrewrite.java.tree.J.Package pkg, String fullyQualifiedName)
    Whether the package declaration's name is a prefix of fullyQualifiedName at a package boundary — e.g.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getPackageName

      public static String getPackageName(org.openrewrite.java.tree.J.Package pkg)
      Returns the canonical dotted name of a package declaration (e.g. "org.openrewrite.java").

      The name is reconstructed by walking the declaration's name tree (J.Identifier / J.FieldAccess) rather than printing it, which is comparatively expensive and requires a Cursor. Any whitespace a source file may place around the . separators is dropped, so the result is always a canonical fully qualified package name.

      Parameters:
      pkg - the package declaration
      Returns:
      the dotted package name
    • isPrefixOf

      public static boolean isPrefixOf(org.openrewrite.java.tree.J.Package pkg, String fullyQualifiedName)
      Whether the package declaration's name is a prefix of fullyQualifiedName at a package boundary — e.g. package a.b matches a.b.C but not a.bc.C. Useful as a cheap pre-filter for "could a type named fullyQualifiedName be declared in this file?".

      The package name tree is matched against fullyQualifiedName segment by segment without building an intermediate string, short-circuiting as soon as a segment diverges.

      Parameters:
      pkg - the package declaration
      fullyQualifiedName - the dotted name to test the package against
      Returns:
      whether the package name is a package-boundary prefix of fullyQualifiedName