net.craftforge.reflection.utils
Class PackageUtils

java.lang.Object
  extended by net.craftforge.reflection.utils.PackageUtils

public class PackageUtils
extends Object

Provides utilities for package actions.

Since:
14.03.2011
Author:
Christian Bick

Constructor Summary
PackageUtils()
           
 
Method Summary
static List<Class<?>> findClasses(String packageName)
          Finds all classes accessible from the class loader and belonging to the given package and its sub-packages.
static List<Class<?>> findClassesInDir(File directory, String packageName)
          Recursive method used to find all classes in a given directory and sub directories.
static List<Class<?>> findClassesInJar(JarFile jarFile, String packagePath)
          Finds all classes in a the package path of the given jar file.
static String nameToPath(String packageName)
          Converts a package name to a package path.
static String pathToName(String packagePath)
          Converts a package path to a package name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PackageUtils

public PackageUtils()
Method Detail

findClasses

public static List<Class<?>> findClasses(String packageName)

Finds all classes accessible from the class loader and belonging to the given package and its sub-packages. Uses classes from the file system if the given package can be obtained as a system resource by the class loader.

Only if no such resource is available, the package will be loaded from a jar file resource if available.

Note: This behaviour is a consequence of the class loader behaviour and may differ in Java versions other then 1.6.

Parameters:
packageName - The package
Returns:
The classes

findClassesInDir

public static List<Class<?>> findClassesInDir(File directory,
                                              String packageName)
                                       throws ClassNotFoundException
Recursive method used to find all classes in a given directory and sub directories.

Parameters:
directory - The base directory
packageName - The package name for classes found inside the base directory
Returns:
The classes found
Throws:
ClassNotFoundException - if class loading fails

findClassesInJar

public static List<Class<?>> findClassesInJar(JarFile jarFile,
                                              String packagePath)
                                       throws ClassNotFoundException,
                                              IOException
Finds all classes in a the package path of the given jar file.

Parameters:
jarFile - The jar file.
packagePath - The package path
Returns:
The classes found
Throws:
ClassNotFoundException - if class loading fails
IOException - if the jar file cannot be read

nameToPath

public static String nameToPath(String packageName)

Converts a package name to a package path.

example: net.craftforge.essential -> net/craftforge/example

Parameters:
packageName - The package name
Returns:
The package path

pathToName

public static String pathToName(String packagePath)

Converts a package path to a package name.

example: net/craftforge/essential -> net.craftforge.example

Parameters:
packagePath - The package path
Returns:
The package name


Copyright © 2011. All Rights Reserved.