Algernon Software

org.algernon.util
Class ReflectionUtil

java.lang.Object
  extended byorg.algernon.util.ReflectionUtil

public class ReflectionUtil
extends Object

ReflectionUtil contains static methods that are useful when dealing with reflection and packages. User: hewett Date: Nov 25, 2003 Time: 3:38:31 PM


Constructor Summary
ReflectionUtil()
           
 
Method Summary
static Method findMethod(Method[] methods, String name)
          Finds a method with the given name from the set of methods.
static String findMethodName(Method[] methods, String name)
          Finds a method with the given name from the set of methods.
private static Object findPackageLocation(String pkgname)
          If the package is not found, returns null.
static Collection getClassesFromDirectory(String pkgname, File directory)
          Given a directory that is the root of a class hierarchy, and a package name, retrieves the Java Classes from the package in the jar file.
static Collection getClassesFromJar(String pkgname, URL url)
          Given a URL specifying a jar file and a package name, retrieves the Java Classes from the package in the jar file.
static Collection getClassesOfPackage(String pkgname)
          Returns all Java Classes in the given package.
static Collection getClassesOfPackages(Collection packages)
          Returns all of the Java Classes in all of the packages presented.
static String getPackageName(Class aClass)
          Returns the name of the package of the class.
static String getShortName(Class aClass)
          Returns the short name of a Java class (without the package name).
static boolean isRootClassOfItsPackage(Class aClass)
          Returns true if the given class does not have a superclass in the given package.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtil

public ReflectionUtil()
Method Detail

isRootClassOfItsPackage

public static boolean isRootClassOfItsPackage(Class aClass)
Returns true if the given class does not have a superclass in the given package. Package is a string giving a full package name.


getClassesOfPackages

public static Collection getClassesOfPackages(Collection packages)
Returns all of the Java Classes in all of the packages presented.


getClassesOfPackage

public static Collection getClassesOfPackage(String pkgname)
Returns all Java Classes in the given package. The package must be in the classpath. The package can be in a jar file or in a directory.

Parameters:
pkgname -
Returns:
a Collection of Java Classes in the order returned by ClassLoader.

getClassesFromJar

public static Collection getClassesFromJar(String pkgname,
                                           URL url)
Given a URL specifying a jar file and a package name, retrieves the Java Classes from the package in the jar file.

Parameters:
pkgname - the name of the package, e.g. com.bigbiz.util
url - the jar location
Returns:
a Collection of java Classes in the order returned by ClassLoader.

getClassesFromDirectory

public static Collection getClassesFromDirectory(String pkgname,
                                                 File directory)
Given a directory that is the root of a class hierarchy, and a package name, retrieves the Java Classes from the package in the jar file.

Parameters:
pkgname - the name of the package, e.g. com.bigbiz.util
directory - the root of the class hierarchy.
Returns:
a Collection of java Classes in the order returned by directory.list()
See Also:
File.list()

findPackageLocation

private static Object findPackageLocation(String pkgname)
If the package is not found, returns null. Returns a File, which is actually a Directory, or a URL, which is actually a jar:file:... path. Returns null if it can't be found.

Parameters:
pkgname - the name of the package to find.
Returns:
a File or a URL representing the package, or null

findMethodName

public static String findMethodName(Method[] methods,
                                    String name)
Finds a method with the given name from the set of methods. Returns its name, or null if it isn't found.

See Also:
findMethod(java.lang.reflect.Method[], java.lang.String)

findMethod

public static Method findMethod(Method[] methods,
                                String name)
Finds a method with the given name from the set of methods. Beware that different classes can contain a method with the same name. Returns its name, or null if it isn't found.


getShortName

public static String getShortName(Class aClass)
Returns the short name of a Java class (without the package name). Some class names, for some reason, contain a trailing semi-colon. This method looks for that and strips any trailing semi-colons. Returns an empty string if it can't find a package name.

Parameters:
aClass -

getPackageName

public static String getPackageName(Class aClass)
Returns the name of the package of the class. Uses Class.getPackage().getName() when possible. However, Class.getPackage() sometimes returns null. If so, this tries to get the package name from the long class name.

Parameters:
aClass -

Algernon Software