| java.lang.Object net.sourceforge.groboutils.util.classes.v1.ClassLoadHelper
ClassLoadHelper | public class ClassLoadHelper (Code) | | Utility class for loading classes and creating instances. Much of the
basic operation have been ripped from
net.groboutils.util.classes.v1.ClassUtil in the GroboUtils package.
If the helper's class loader is null, then it will use the Thread's
context ClassLoader.
Note that resource loading is very tricky. Finding the right classloader
and right methods to invoke is JDK dependent.
author: Matt Albrecht groboclown@users.sourceforge.net version: $Date: 2003/02/10 22:52:36 $ since: March 16, 2002 |
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, boolean swallowExceptions) Creates a new instance of the class with the given className
using the default constructor. | public Object | createObject(Class c) Creates an Object from the given Class, using its default constructor.
All creation exceptions are swallowed. | public Object | createObject(Class c, boolean swallowExceptions) Creates an Object from the given Class, using its 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 an
IllegalStateException will be thrown only if swallowExceptions
is false; otherwise, null will be returned.
Parameters: c - the Class object from which a new instance will be createdusing its default constructor. Parameters: swallowExceptions - true if this method is to returnnull on any exceptions, or false if it shouldthrow an IllegalStateException on any error. | public Object | createObjectFromProperty(String propertyClassName, Class defaultClass, boolean swallowExceptions) Loads an object using the
ClassLoadHelper.createObject(String,boolean) method above, using the given System property's value as the
class name. | public Object | createObjectFromProperty(String propertyClassName, Class defaultClass, Hashtable properties, boolean swallowExceptions) Loads an object using the
ClassLoadHelper.createObject(String,boolean) method above, using the given Hashtable's property's value as the
class name. | protected static Method | discoverContextClassloaderMethod() | protected static Method | discoverGetResourcesMethod() | protected static Method | discoverGetSystemResourcesMethod() | protected String | getAbsoluteResourceName(String name) | public Class | getClass(String name) Loads the requested class from the helper's classloader, and returns
the Class instance, or null if the class could not be
found.
Parameters: name - the name of the class to load. | public Class | getClass(String name, boolean swallowExceptions) Loads the requested class from the helper's classloader, and returns
the Class instance, or null if the class could not be
found. | protected ClassLoader | getClassLoader() Gets the correct class loader. | public URL | getResource(String name) Loads a resource with the given name, using the correct ClassLoader.
Does not swallow exceptions. | public URL | getResource(String name, ClassLoader cl) Loads a resource with the given name, using the given ClassLoader.
Does not swallow exceptions. | public InputStream | getResourceAsStream(String name) Loads a resource with the given name, using the correct ClassLoader.
Does not swallow exceptions. | public Enumeration | getResources(String name) Loads a resource with the given name, using the correct ClassLoader.
Does not swallow exceptions. | public Enumeration | getResources(String name, ClassLoader cl) Loads a resource with the given name, using the correct ClassLoader.
Does not swallow exceptions. | public URL | getSystemResource(String name) Loads a resource with the given name, using the correct ClassLoader.
Does not swallow exceptions. | public Enumeration | getSystemResources(String name) Get the resource associated with the given name from the System
classloader. | protected static ClassLoader | getThreadClassLoader(Thread t) Use reflection to get the thread (context) class loader. | protected Class | loadClass(String name) Loads a class with the given name, using the correct ClassLoader. |
ClassLoadHelper | public ClassLoadHelper()(Code) | | Default constructor - will use the Thread's context class loader for
each class discovery.
|
ClassLoadHelper | public ClassLoadHelper(Class clazz)(Code) | | Use the given class's classloader.
Parameters: clazz - the class to pull the classloader from. exception: NullPointerException - if clazz is null. |
ClassLoadHelper | public ClassLoadHelper(ClassLoader cl)(Code) | | Loads the helper with the given class loader. If the given class loader
is null, then it will use the Thread's context class loader.
Parameters: cl - the classloader to pull all requested classes from, orit will use the thread's context class loader if cl isnull. See Also: java.lang.Thread.getContextClassLoader |
createObject | public Object createObject(String className, boolean swallowExceptions)(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 an
IllegalStateException will be thrown only if swallowExceptions
is false; otherwise, null will be returned.
Parameters: className - the name of the class to create an instance. Parameters: swallowExceptions - true if this method is to returnnull on any exceptions, or false if it shouldthrow an IllegalStateException on any error. the new instance. exception: IllegalStateException - if there was an error duringinitialization and swallowExceptions is false. See Also: ClassLoadHelper.getClass(String) See Also: ClassLoadHelper.createObject(String) See Also: ClassLoadHelper.createObject(Class) See Also: ClassLoadHelper.createObject(Class,boolean) |
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. |
createObject | public Object createObject(Class c, boolean swallowExceptions)(Code) | | Creates an Object from the given Class, using its 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 an
IllegalStateException will be thrown only if swallowExceptions
is false; otherwise, null will be returned.
Parameters: c - the Class object from which a new instance will be createdusing its default constructor. Parameters: swallowExceptions - true if this method is to returnnull on any exceptions, or false if it shouldthrow an IllegalStateException on any error. the instantiated object, or null if c isnull, or if there was an error during initialization andswallowExceptions is true. exception: IllegalStateException - if there was an error duringinitialization and swallowExceptions is false. |
createObjectFromProperty | public Object createObjectFromProperty(String propertyClassName, Class defaultClass, boolean swallowExceptions)(Code) | | Loads an object using the
ClassLoadHelper.createObject(String,boolean) method above, using the given System property's value as the
class name. If the System property is not defined, then it resorts to
the default class.
Parameters: propertyClassName - the System Property name, whose value will beused as a fully-qualified Class name to load and instantiate andreturn. Parameters: defaultClass - if the System Property propertyClassNameis not defined, then this will be the class to instantiate andreturn. Parameters: swallowExceptions - true if this method is to returnnull on any exceptions, or false if it shouldthrow an IllegalStateException on any error. the instantiated class. exception: IllegalStateException - if there was an error duringinitialization and swallowExceptions is false. |
createObjectFromProperty | public Object createObjectFromProperty(String propertyClassName, Class defaultClass, Hashtable properties, boolean swallowExceptions)(Code) | | Loads an object using the
ClassLoadHelper.createObject(String,boolean) method above, using the given Hashtable's property's value as the
class name. If the Hashtable property is not defined, then it resorts to
the default class. If the Hashtable is null, then the
System property will be used instead.
Parameters: propertyClassName - the System Property name, whose value will beused as a fully-qualified Class name to load and instantiate andreturn. Parameters: defaultClass - if the System Property propertyClassNameis not defined, then this will be the class to instantiate andreturn. Parameters: properties - a Hashtable of String -> String mappings. Parameters: swallowExceptions - true if this method is to returnnull on any exceptions, or false if it shouldthrow an IllegalStateException on any error. the instantiated class. exception: IllegalStateException - if there was an error duringinitialization and swallowExceptions is false. |
discoverContextClassloaderMethod | protected static Method discoverContextClassloaderMethod()(Code) | | |
discoverGetResourcesMethod | protected static Method discoverGetResourcesMethod()(Code) | | |
discoverGetSystemResourcesMethod | protected static Method discoverGetSystemResourcesMethod()(Code) | | |
getClass | public Class getClass(String name)(Code) | | Loads the requested class from the helper's classloader, and returns
the Class instance, or null if the class could not be
found.
Parameters: name - the name of the class to load. the discovered Class, or null if it could not be found. |
getClass | public Class getClass(String name, boolean swallowExceptions)(Code) | | Loads the requested class from the helper's classloader, and returns
the Class instance, or null if the class could not be
found.
Parameters: name - the name of the class to load. Parameters: swallowExceptions - true if this method is to return the discovered Class, or null if it could not be foundand swallowExceptions is true. exception: IllegalStateException - if there was an error duringinitialization and swallowExceptions is false. |
getClassLoader | protected ClassLoader getClassLoader()(Code) | | Gets the correct class loader. May return null.
the ClassLoader |
getResource | public URL getResource(String name) throws IOException(Code) | | Loads a resource with the given name, using the correct ClassLoader.
Does not swallow exceptions. See the JDK documentation on resources
(they are pretty much files that are in the classpath of the
classloader). Yes, this can be used successfully to get a class file
(well, JDK 1.1 throws a SecurityException if this is attempted).
Parameters: name - absolute referece to the expected resource. the resource name as an URL, which may possibly benull. exception: IOException - if an I/O error occurs. See Also: java.lang.ClassLoader.getResource(String) See Also: java.lang.ClassLoader.getResourceAsStream(String) |
getResource | public URL getResource(String name, ClassLoader cl) throws IOException(Code) | | Loads a resource with the given name, using the given ClassLoader.
Does not swallow exceptions. See the JDK documentation on resources
(they are pretty much files that are in the classpath of the
classloader). Yes, this can be used successfully to get a class file
(well, JDK 1.1 throws a SecurityException if this is attempted).
Parameters: name - absolute referece to the expected resource. Parameters: cl - the classloader to load the reference from. the resource name as an URL, which may possibly benull. exception: IOException - if an I/O error occurs. See Also: java.lang.ClassLoader.getResource(String) See Also: java.lang.ClassLoader.getResourceAsStream(String) |
getResourceAsStream | public InputStream getResourceAsStream(String name) throws IOException(Code) | | Loads a resource with the given name, using the correct ClassLoader.
Does not swallow exceptions. See the JDK documentation on resources
(they are pretty much files that are in the classpath of the
classloader). Yes, this can be used successfully to get a class file
(well, JDK 1.1 throws a SecurityException if this is attempted).
Parameters: name - absolute referece to the expected resource. the resource as an InputStream, which may possibly benull. exception: IOException - if an I/O error occurs. See Also: java.lang.ClassLoader.getResource(String) See Also: java.lang.ClassLoader.getResourceAsStream(String) |
getSystemResource | public URL getSystemResource(String name) throws IOException(Code) | | Loads a resource with the given name, using the correct ClassLoader.
Does not swallow exceptions. See the JDK documentation on resources
(they are pretty much files that are in the classpath of the
classloader). Yes, this can be used successfully to get a class file
(well, JDK 1.1 throws a SecurityException if this is attempted).
Parameters: name - absolute referece to the expected resource. the resource name as an URL, which may possibly benull. exception: IOException - if an I/O error occurs. See Also: java.lang.ClassLoader.getResource(String) See Also: java.lang.ClassLoader.getResourceAsStream(String) |
getThreadClassLoader | protected static ClassLoader getThreadClassLoader(Thread t)(Code) | | Use reflection to get the thread (context) class loader.
|
|
|