| java.lang.Object net.sourceforge.groboutils.util.classes.v1.ClassUtil
ClassUtil | public class ClassUtil (Code) | | 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
ClassUtil.getClass(String,String) , the
jarName may be a filename loadable from the
java.io.File class, or a proper URL loadable from the
java.net.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.
author: Matt Albrecht groboclown@users.sourceforge.net version: $Date: 2003/02/10 22:52:36 $ since: November 13, 2000 (GroboUtils Alpha 0.9.0) |
Method Summary | |
public Object | createObject(String className) Creates a new instance of the class with the given className
using the default constructor. | public Object | createObject(String className, String jarName) Creates a new instance of the class with the given className
using the default constructor, from the given URL. | public Object | createObject(Class c) Creates an Object from the given Class, using its default constructor.
All creation exceptions are swallowed. | public void | flush() Call this to flush out the cache. | public Class | getClass(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. | public Class | getClass(String name, String jarName) Either finds or loads from cache the given class.
Parameters: name - the name of the class to load. Parameters: jarName - the URL to load the class from - it may be null. | protected String | getClassHashName(String name, String jarName) Creates the name of the class for the hashtable lookup, which is
a junction of the jar name and the class name. | public String | getClassPackage(Class c) Discovers the package name for the given class. | public static ClassUtil | getInstance() Retrieve the shared instance of the utility class. | public boolean | isJdk2Compatible() Checks if the current JVM version is 1.2 compatible. | protected Class | loadClass(String className, 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. |
s_instance | protected static ClassUtil s_instance(Code) | | Subclasses must instantiate themselves in this variable in their
static initialization block, and overload the
ClassUtil.getInstance() static method.
|
ClassUtil | protected ClassUtil()(Code) | | Default constructor - made protected so
users won't instantiate the utility
|
createObject | public Object createObject(String className)(Code) | | 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. the new instance, or null if there was a problem. See Also: ClassUtil.getClass(String) See Also: ClassUtil.createObject(String,String) |
createObject | public Object createObject(String className, String jarName)(Code) | | 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. Parameters: jarName - the URL to load the class from - it may be null. the new instance, or null if there was a problem. See Also: ClassUtil.getClass(String,String) See Also: ClassUtil.createObject(String) |
createObject | public Object createObject(Class c)(Code) | | 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 createdusing its default constructor. the instantiated object, or null if c isnull, or if there was an error during initialization. |
flush | public void flush()(Code) | | 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 Class getClass(String name)(Code) | | Either finds or loads from cache the given class, using the
default class loader.
Parameters: name - the name of the class to load. the discovered Class, or null if it could not be found. See Also: ClassUtil.getClass(String,String) |
getClass | public Class getClass(String name, String jarName)(Code) | | Either finds or loads from cache the given class.
Parameters: name - the name of the class to load. Parameters: jarName - the URL to load the class from - it may be null. the discovered Class, or null if it could not be found. See Also: ClassUtil.getClass(String) |
getClassHashName | protected String getClassHashName(String name, String jarName)(Code) | | 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 Parameters: jarName - the jar name - may be null. the name for the hashtable lookup. |
getClassPackage | public String getClassPackage(Class c)(Code) | | 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. the package name, or null if c isnull. |
getInstance | public static ClassUtil getInstance()(Code) | | Retrieve the shared instance of the utility class.
the utility instance |
isJdk2Compatible | public boolean isJdk2Compatible()(Code) | | Checks if the current JVM version is 1.2 compatible. We check by
seeing if java.net.URLClassLoader exists.
true if java.net.URLClassLoader existsin the classpath, or false otherwise. |
loadClass | protected Class loadClass(String className, String baseJar)(Code) | | 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 Parameters: baseJar - the URL file to load the class from - may benull. the Class instance, or null if it was not found. |
|
|