anon.util
Class ClassUtil

java.lang.Object
  extended by anon.util.ClassUtil

public final class ClassUtil
extends java.lang.Object

This class performs some basic operations related to Class objects.

Author:
Rolf Wendolsky

Nested Class Summary
private static class ClassUtil.ClassGetter
          This small inner class is needed to get information about static classes.
private static class ClassUtil.ClassInstantiator
           
static class ClassUtil.Package
          Represents a package name.
 
Field Summary
private static java.lang.String FILE
           
private static java.lang.String JAR_FILE
           
private static java.util.Hashtable ms_loadedClasses
          Stores all loaded classes.
private static java.util.Vector ms_loadedDirectories
          Stores all loaded directories.
 
Constructor Summary
private ClassUtil()
          This class works without being initialised and is completely static.
 
Method Summary
static void addFileToClasspath(java.io.File a_file)
           
static void addFileToClasspath(java.lang.String a_file)
           
static void addURLToClasspath(java.net.URL a_url)
           
static java.util.Vector findSubclasses(java.lang.Class a_class)
          Gets all classes that extend the given class or implement the given interface, including the class itself.
static java.lang.Class getCallingClassStatic()
          Returns the class that called the current method.
static java.io.File getClassDirectory(java.lang.Class a_class)
          Returns the class directory of the specified class.
static java.io.File getClassDirectory(java.lang.String a_className)
           
static java.lang.String getClassNameStatic()
          Returns the name, including the package, of the calling method's class.
static java.lang.String getClassPath()
          Returns the current java class path.
protected static java.lang.String getClassPath(boolean a_bPreventLoop)
          Returns the current java class path.
static java.lang.Class getClassStatic()
          Returns the current class from a static context.
static java.lang.Class getFirstClassFound(java.io.File a_file)
          Traverse a file, directory or zip/jar file recursive until a class file is found and instantiated or all files are traversed.
static java.lang.String getShortClassName(java.lang.Class a_class)
          Gets the name of a class without package (everything before the last "." is removed).
static java.lang.String getUserDir()
          Returns the content of the system property user.dir
static java.util.Enumeration loadClasses()
          Loads all classes into cache that are in the same file structure as this class and as the calling class.
static java.util.Enumeration loadClasses(java.lang.Class a_rootClass)
           
private static java.util.Enumeration loadClasses(java.lang.Class a_rootClass, java.io.File a_directory)
          Loads all classes into cache that are in the same file structure as the given class and as the calling class.
static java.util.Enumeration loadClasses(java.io.File a_classDirectory)
           
private static void loadClassesInternal(java.lang.Class a_rootClass, java.io.File a_directory)
          Loads all classes into cache that are in the same file structure as the given class.
private static java.lang.Class toClass(java.io.File a_classFile, java.io.File a_classDirectory)
          Turns class files into Class objects.
static java.lang.String toRelativeResourcePath(java.lang.Class a_class)
          Generates a relative resource path to the given class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JAR_FILE

private static final java.lang.String JAR_FILE
See Also:
Constant Field Values

FILE

private static final java.lang.String FILE
See Also:
Constant Field Values

ms_loadedClasses

private static java.util.Hashtable ms_loadedClasses
Stores all loaded classes.


ms_loadedDirectories

private static java.util.Vector ms_loadedDirectories
Stores all loaded directories.

Constructor Detail

ClassUtil

private ClassUtil()
This class works without being initialised and is completely static. Therefore, the constructor is not needed and private.

Method Detail

addFileToClasspath

public static void addFileToClasspath(java.lang.String a_file)
                               throws java.io.IOException,
                                      java.lang.IllegalAccessException
Throws:
java.io.IOException
java.lang.IllegalAccessException

addFileToClasspath

public static void addFileToClasspath(java.io.File a_file)
                               throws java.lang.IllegalAccessException
Throws:
java.lang.IllegalAccessException

addURLToClasspath

public static void addURLToClasspath(java.net.URL a_url)
                              throws java.lang.IllegalAccessException
Throws:
java.lang.IllegalAccessException

getShortClassName

public static java.lang.String getShortClassName(java.lang.Class a_class)
Gets the name of a class without package (everything before the last "." is removed).

Parameters:
a_class - a Class
Returns:
the name of the class without package

getClassStatic

public static java.lang.Class getClassStatic()
Returns the current class from a static context. This method is a replacement to this.getClass() in a static environment, as this is not available there.

Returns:
the current class

getClassNameStatic

public static java.lang.String getClassNameStatic()
Returns the name, including the package, of the calling method's class.

Returns:
the name, including the package, of the calling method's class

getCallingClassStatic

public static java.lang.Class getCallingClassStatic()
Returns the class that called the current method. This method is an alternative to Object.getClass(), as the caller is not needed to be an argument or an Object either.

Returns:
the class that called the current method

getUserDir

public static java.lang.String getUserDir()
Returns the content of the system property user.dir

Returns:
the content of the system property user.dir

getClassPath

public static java.lang.String getClassPath()
Returns the current java class path.

Returns:
the current java class path

findSubclasses

public static java.util.Vector findSubclasses(java.lang.Class a_class)
Gets all classes that extend the given class or implement the given interface, including the class itself. It is recommended to store this information somewhere and reuse it instead of calling this method again, as all known classes have to be searched at each call. If classes from special packages should be found, it is recommended to load those packages at program start. Otherwise, some classes in those packages, that inherit from classes in this or other packages, can not be found. WARNING: this may be slow at the first call, especially for large packages

Parameters:
a_class - a Class
Returns:
all known subclasses of the given class

loadClasses

public static java.util.Enumeration loadClasses()
Loads all classes into cache that are in the same file structure as this class and as the calling class. Recommended to be called at program start to initially fill the cache. WARNING: this may be slow at the first call, especially for large packages (like the JRE)

Returns:
all loaded classes

loadClasses

public static java.util.Enumeration loadClasses(java.lang.Class a_rootClass)

loadClasses

public static java.util.Enumeration loadClasses(java.io.File a_classDirectory)

loadClasses

private static java.util.Enumeration loadClasses(java.lang.Class a_rootClass,
                                                 java.io.File a_directory)
Loads all classes into cache that are in the same file structure as the given class and as the calling class. WARNING: this may be slow at the first call, especially for large packages (like the JRE)

Parameters:
a_rootClass - the class from that loading is started
Returns:
all loaded classes

getClassDirectory

public static java.io.File getClassDirectory(java.lang.String a_className)

getClassDirectory

public static java.io.File getClassDirectory(java.lang.Class a_class)
Returns the class directory of the specified class. The class directory is either the directory in that the highest package in the package structure of the class is contained, or the jar-File in that the class is contained. For extracting the contents of a jar-File, see ZipFile.

Parameters:
a_class - a class
Returns:
the class directory of the specified class, either a real directory or a Jar-file or null if the directory/jar-file does not exist

toRelativeResourcePath

public static java.lang.String toRelativeResourcePath(java.lang.Class a_class)
Generates a relative resource path to the given class.

Parameters:
a_class - Class
Returns:
URL

getFirstClassFound

public static java.lang.Class getFirstClassFound(java.io.File a_file)
Traverse a file, directory or zip/jar file recursive until a class file is found and instantiated or all files are traversed. The instantiated class is returned.

Parameters:
a_file - a file, directory or zip/jar file
Returns:
the first class that was instnatiated or null if no class could be found and instantiated

getClassPath

protected static java.lang.String getClassPath(boolean a_bPreventLoop)
Returns the current java class path.

Parameters:
a_bPreventLoop - true if a loop with calls in the ResourceLoader must be prevented
Returns:
the current java class path

loadClassesInternal

private static void loadClassesInternal(java.lang.Class a_rootClass,
                                        java.io.File a_directory)
                                 throws java.io.IOException
Loads all classes into cache that are in the same file structure as the given class. WARNING: this may be slow at the first call, especially for large packages (like the JRE)

Parameters:
a_rootClass - the class from that loading is started
an - optional directory or jar/zip file to load the classes from
Throws:
java.io.IOException - if an I/O error occurs

toClass

private static java.lang.Class toClass(java.io.File a_classFile,
                                       java.io.File a_classDirectory)
Turns class files into Class objects.

Parameters:
a_classFile - a class file with full directory path
a_classDirectory - the directory where all class files and class directories reside
Returns:
the class file as Class object