Package org.openrewrite.java.internal
Class PackageNameUtils
java.lang.Object
org.openrewrite.java.internal.PackageNameUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetPackageName(org.openrewrite.java.tree.J.Package pkg) Returns the canonical dotted name of a package declaration (e.g.static booleanisPrefixOf(org.openrewrite.java.tree.J.Package pkg, String fullyQualifiedName) Whether the package declaration's name is a prefix offullyQualifiedNameat a package boundary — e.g.
-
Method Details
-
getPackageName
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 aCursor. 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 offullyQualifiedNameat a package boundary — e.g. packagea.bmatchesa.b.Cbut nota.bc.C. Useful as a cheap pre-filter for "could a type namedfullyQualifiedNamebe declared in this file?".The package name tree is matched against
fullyQualifiedNamesegment by segment without building an intermediate string, short-circuiting as soon as a segment diverges.- Parameters:
pkg- the package declarationfullyQualifiedName- the dotted name to test the package against- Returns:
- whether the package name is a package-boundary prefix of
fullyQualifiedName
-