public class PathFinder
extends java.lang.Object
getSourceRootPaths()
. By default, this list includes "src"
, "source"
, and "bin"
. Additional paths can be added using
addSourceRootPath(java.lang.String)
, or the entire list can be set using
setSourceRootPaths(java.util.Collection<java.lang.String>)
.Modifier and Type | Method and Description |
---|---|
static void |
addSourceRootPath(java.lang.String path)
Adds a path to the list used for detecting root source directories
relative to the parent of the root class directory.
|
static java.lang.String |
convertToLocalSeparators(java.lang.String pathName)
Tries to convert file separation characters in a path name to ones
appropriate for the local system.
|
static java.lang.String |
convertToUnixSeparators(java.lang.String pathName)
Tries to convert file separation characters in a path name to ones
appropriate for Unix.
|
static java.lang.String |
convertToWindowsSeparators(java.lang.String pathName)
Tries to convert file separation characters in a path name to ones
appropriate for Windows.
|
static java.lang.String |
expand(java.lang.String path)
Expands a path name to include environment and special variables
identified by a dollar sign (
'$' ). |
static java.lang.String |
findClassDir(java.lang.Class<?> cls)
Returns the path of the class directory for a specific class.
|
static java.lang.String |
findClassDir(java.lang.Object classObj)
Returns the path of the class directory for a Java class identified by
classObj . |
static java.lang.String |
findClassDir(java.lang.String className)
Returns the path of the class directory for a class identified by name.
|
static java.lang.String |
findPackageSourceDir(java.lang.String pkgName)
Returns the path name of the source directory for a specific package, or
null if no such directory is found. |
static java.lang.String |
findSourceDir(java.lang.Class<?> cls)
Returns the path of the source directory for a specific class.
|
static java.lang.String |
findSourceDir(java.lang.Object classObj)
Returns the path of the source directory for a Java class identified by
classObj . |
static java.lang.String |
findSourceDir(java.lang.String className)
Returns the path of the source directory for a class identified by name.
|
static java.lang.String |
getBaseName(java.io.File file)
Returns the base name of a file.
|
static java.lang.String |
getBaseName(java.lang.String pathname)
Returns the base name of a file given a pathname.
|
static java.lang.String |
getClassRelativePath(java.lang.Object classObj,
java.lang.String relpath)
Returns a complete file path from a path relative to the class
directory for a specified class.
|
static java.lang.String |
getSourceRelativePath(java.lang.Object classObj,
java.lang.String relpath)
Returns a complete file path from a path relative to the source directory
for a specified class.
|
static java.util.ArrayList<java.lang.String> |
getSourceRootPaths()
Returns a list of the paths used for detecting root source
directories relative to the parent of the root class directory.
|
static void |
setSourceRootPaths(java.util.Collection<java.lang.String> paths)
Sets the list of the paths used for detecting root source
directories relative to the parent of the root class directory.
|
public static java.util.ArrayList<java.lang.String> getSourceRootPaths()
public static void setSourceRootPaths(java.util.Collection<java.lang.String> paths)
paths
- new list of the source root pathspublic static void addSourceRootPath(java.lang.String path)
path
- source root path to addpublic static java.lang.String findSourceDir(java.lang.String className)
Package.getPackages()
.
This method works by climbing the class's resource hierarchy, and
assumes that the root source directory is located relative to the parent
of the root class directory by one of the paths listed in getSourceRootPaths()
.
className
- name of the classpublic static java.lang.String findSourceDir(java.lang.Object classObj)
classObj
. The class is determined either directly, if
classObj
is an instance of Class
; by name, if
it is a String
; or otherwise by calling
classObj.getClass()
. The class must be associated with a
package.
This method works by climbing the class's resource hierarchy, and
assumes that the root source directory is located relative to the parent
of the root class directory by one of the paths listed in getSourceRootPaths()
.
classObj
- identifies the Java classpublic static java.lang.String findSourceDir(java.lang.Class<?> cls)
This method works by climbing the class's resource hierarchy, and
assumes that the root source directory is located relative to the parent
of the root class directory by one of the paths listed in getSourceRootPaths()
.
cls
- identifies the classpublic static java.lang.String findPackageSourceDir(java.lang.String pkgName)
null
if no such directory is found.
This method works by first finding the class directory for the
package. If no such directory is found (e.g., because the class is loaded
from a JAR file), the method returns null
. Otherwise, it climbs
the resource hierarchy and attempts to find a matching source directory
based on the the paths listed in getSourceRootPaths()
. If no
source directory is not found, it is assumed that the class and source
directories are the same, and so the class directory is returned.
pkgName
- name of the packagenull
if not foundpublic static java.lang.String findClassDir(java.lang.String className)
Package.getPackages()
.
This method works by climbing the class's resource hierarchy.
className
- name of the classpublic static java.lang.String findClassDir(java.lang.Class<?> cls)
This method works by climbing the class's resource hierarchy.
cls
- identifies the classpublic static java.lang.String findClassDir(java.lang.Object classObj)
classObj
. The class is determined either directly, if
classObj
is an instance of Class
; by name, if
it is a String
; or otherwise by calling
classObj.getClass()
. The class must be associated with a
package.
This method works by climbing the class's resource hierarchy.
classObj
- identifies the Java classpublic static java.lang.String getSourceRelativePath(java.lang.Object classObj, java.lang.String relpath)
classObj
, either directly, if classObj
is an
instance of Class
; by name, if it is a String
; or
otherwise by calling classObj.getClass()
. The class must be
associated with a package.
This method works by climbing the class's resource hierarchy, and
assumes that the root source directory is located relative to the parent
of the root class directory by one of the paths listed in getSourceRootPaths()
.
If the relative path specified by relpath
is
null
, then the path for the source directory itself is
returned. Within the returned file path, instances of either the Unix or
Windows file separator characters (i.e., '/'
or
'\'
) are mapped to the file separator character for the
current system.
classObj
- object used to deterine the classrelpath
- path relative to class's source directorypublic static java.lang.String getClassRelativePath(java.lang.Object classObj, java.lang.String relpath)
classObj
, either directly, if classObj
is an
instance of Class
, or by calling
classObj.getClass()
; by name, if it is a String
; or
otherwise by calling classObj.getClass()
. The class must be
associated with a package.
This method works by climbing the class's resource hierarchy.
If the relative path specified by relpath
is
null
, then the path for the class directory itself is
returned. Within the returned file path, instances of either the Unix or
Windows file separator characters (i.e., '/'
or
'\'
) are mapped to the file separator character for the
current system.
classObj
- object used to deterine the classrelpath
- path relative to class's class directorypublic static java.lang.String expand(java.lang.String path)
'$'
). Valid expansions include:
$ENV_VAR - value of the environment variable ENV_VAR ${srcdir CLASS} - source directory of class identified by CLASS ${classdir CLASS} - class directory of class identified by CLASS $$ - a dollar sign '$'In the above description,
CLASS
is either a fully qualified
class name, or a simple name that can be located with respect to
the packages obtained via Package.getPackages()
.path
- path name to expandpublic static java.lang.String convertToLocalSeparators(java.lang.String pathName)
'\'
will be converted to
'/'
, and on Windows, '/'
will be converted
to a '\'
.public static java.lang.String convertToUnixSeparators(java.lang.String pathName)
'\'
will be converted to '/'
.public static java.lang.String convertToWindowsSeparators(java.lang.String pathName)
'/'
will be converted to '\'
.public static java.lang.String getBaseName(java.io.File file)
file
- file to find base name forpublic static java.lang.String getBaseName(java.lang.String pathname)
pathname
- path name of the file to find base name for