net.sourceforge.groboutils.util.classes.v1
Class ClassUtil

java.lang.Object
  extended bynet.sourceforge.groboutils.util.classes.v1.ClassUtil

public class ClassUtil
extends java.lang.Object

Utility class for loading classes and creating instances. It decides which JDK version to use as the loader. Class instances are put into a Hashtable, based on URL and classname. When the Java-Doc defines a jarName, as in getClass( String, String ), the jarName may be a filename loadable from the File class, or a proper URL loadable from the URL class. If the jarName does not have a protocol, then it is assumed to be a file, otherwise, it is used as a URL.

Note that this class is not thread safe. It is assumed that applications will use the ClassUtil only during initialization times, since dynamic class loading can be very expensive. If you need thread safety, then you will need to ensure that either all class loading is done in a single thread, or that your application properly synchronizes the method calls.

Update v0.9.1: the constructor will now check for jdk2 first like before, but now if it is not found, it will try to use jdk0 compatibility - before, it would just fail out. This allows the libraries to be repackaged to contain only the jdk0 classes if so desired.

Since:
November 13, 2000 (GroboUtils Alpha 0.9.0)
Version:
$Date: 2003/02/10 22:52:36 $
Author:
Matt Albrecht groboclown@users.sourceforge.net

Field Summary
protected static ClassUtil s_instance
          Subclasses must instantiate themselves in this variable in their static initialization block, and overload the getInstance() static method.
 
Constructor Summary
protected ClassUtil()
          Default constructor - made protected so users won't instantiate the utility
 
Method Summary
 java.lang.Object createObject(java.lang.Class c)
          Creates an Object from the given Class, using its default constructor.
 java.lang.Object createObject(java.lang.String className)
          Creates a new instance of the class with the given className using the default constructor.
 java.lang.Object createObject(java.lang.String className, java.lang.String jarName)
          Creates a new instance of the class with the given className using the default constructor, from the given URL.
 void flush()
          Call this to flush out the cache.
 java.lang.Class getClass(java.lang.String name)
          Either finds or loads from cache the given class, using the default class loader.
 java.lang.Class getClass(java.lang.String name, java.lang.String jarName)
          Either finds or loads from cache the given class.
protected  java.lang.String getClassHashName(java.lang.String name, java.lang.String jarName)
          Creates the name of the class for the hashtable lookup, which is a junction of the jar name and the class name.
 java.lang.String getClassPackage(java.lang.Class c)
          Discovers the package name for the given class.
static ClassUtil getInstance()
          Retrieve the shared instance of the utility class.
 boolean isJdk2Compatible()
          Checks if the current JVM version is 1.2 compatible.
protected  java.lang.Class loadClass(java.lang.String className, java.lang.String baseJar)
          Attempts to load the class from the current classpath if baseJar is null, or from the appropriate class loader if it is not null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

s_instance

protected static ClassUtil s_instance
Subclasses must instantiate themselves in this variable in their static initialization block, and overload the getInstance() static method.

Constructor Detail

ClassUtil

protected ClassUtil()
Default constructor - made protected so users won't instantiate the utility

Method Detail

getInstance

public static ClassUtil getInstance()
Retrieve the shared instance of the utility class.

Returns:
the utility instance

flush

public void flush()
Call this to flush out the cache. The cache may be huge, depending on the Jar files loaded, as well as the Class instances. This should be called whenever a mass class instantiation process is finished.


getClass

public java.lang.Class getClass(java.lang.String name)
Either finds or loads from cache the given class, using the default class loader.

Parameters:
name - the name of the class to load.
Returns:
the discovered Class, or null if it could not be found.
See Also:
getClass( String, String )

getClass

public java.lang.Class getClass(java.lang.String name,
                                java.lang.String jarName)
Either finds or loads from cache the given class.

Parameters:
name - the name of the class to load.
jarName - the URL to load the class from - it may be null.
Returns:
the discovered Class, or null if it could not be found.
See Also:
getClass( String )

createObject

public java.lang.Object createObject(java.lang.String className)
Creates a new instance of the class with the given className using the default constructor. If there was an error during the creation, such as the class was not found, the class does not have a default constructor, or the constructor threw an exception, then null is returned.

Parameters:
className - the name of the class to create an instance.
Returns:
the new instance, or null if there was a problem.
See Also:
getClass( String ), createObject( String, String )

createObject

public java.lang.Object createObject(java.lang.String className,
                                     java.lang.String jarName)
Creates a new instance of the class with the given className using the default constructor, from the given URL. If there was an error during the creation, such as the class was not found, the class does not have a default constructor, or the constructor threw an exception, then null is returned.

Parameters:
className - the name of the class to create an instance.
jarName - the URL to load the class from - it may be null.
Returns:
the new instance, or null if there was a problem.
See Also:
getClass( String, String ), createObject( String )

createObject

public java.lang.Object createObject(java.lang.Class c)
Creates an Object from the given Class, using its default constructor. All creation exceptions are swallowed. If the object could not be created, then null is returned.

Parameters:
c - the Class object from which a new instance will be created using its default constructor.
Returns:
the instantiated object, or null if c is null, or if there was an error during initialization.

isJdk2Compatible

public boolean isJdk2Compatible()
Checks if the current JVM version is 1.2 compatible. We check by seeing if java.net.URLClassLoader exists.

Returns:
true if URLClassLoader exists in the classpath, or false otherwise.

getClassPackage

public java.lang.String getClassPackage(java.lang.Class c)
Discovers the package name for the given class. The package name will not have a final '.'.

Parameters:
c - the class to find the package name.
Returns:
the package name, or null if c is null.

getClassHashName

protected java.lang.String getClassHashName(java.lang.String name,
                                            java.lang.String jarName)
Creates the name of the class for the hashtable lookup, which is a junction of the jar name and the class name. It allows for multiple classes with the same name.

Parameters:
name - the class name
jarName - the jar name - may be null.
Returns:
the name for the hashtable lookup.

loadClass

protected java.lang.Class loadClass(java.lang.String className,
                                    java.lang.String baseJar)
Attempts to load the class from the current classpath if baseJar is null, or from the appropriate class loader if it is not null. If the class is not found, then this returns null. This never throws an exception.

Parameters:
className - name of the class to load
baseJar - the URL file to load the class from - may be null.
Returns:
the Class instance, or null if it was not found.


Copyright © 2001-2003 by The GroboUtils Project