|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.groboutils.util.classes.v1.ClassLoadHelper
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.
Constructor Summary | |
ClassLoadHelper()
Default constructor - will use the Thread's context class loader for each class discovery. |
|
ClassLoadHelper(java.lang.Class clazz)
Use the given class's classloader. |
|
ClassLoadHelper(java.lang.ClassLoader cl)
Loads the helper with the given class loader. |
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.Class c,
boolean swallowExceptions)
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,
boolean swallowExceptions)
Creates a new instance of the class with the given className using the default constructor. |
java.lang.Object |
createObjectFromProperty(java.lang.String propertyClassName,
java.lang.Class defaultClass,
boolean swallowExceptions)
Loads an object using the createObject( String, boolean )
method above, using the given System property's value as the
class name. |
java.lang.Object |
createObjectFromProperty(java.lang.String propertyClassName,
java.lang.Class defaultClass,
java.util.Hashtable properties,
boolean swallowExceptions)
Loads an object using the createObject( String, boolean )
method above, using the given Hashtable's property's value as the
class name. |
protected static java.lang.reflect.Method |
discoverContextClassloaderMethod()
|
protected static java.lang.reflect.Method |
discoverGetResourcesMethod()
|
protected static java.lang.reflect.Method |
discoverGetSystemResourcesMethod()
|
protected java.lang.String |
getAbsoluteResourceName(java.lang.String name)
|
java.lang.Class |
getClass(java.lang.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. |
java.lang.Class |
getClass(java.lang.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 java.lang.ClassLoader |
getClassLoader()
Gets the correct class loader. |
java.net.URL |
getResource(java.lang.String name)
Loads a resource with the given name, using the correct ClassLoader. |
java.net.URL |
getResource(java.lang.String name,
java.lang.ClassLoader cl)
Loads a resource with the given name, using the given ClassLoader. |
java.io.InputStream |
getResourceAsStream(java.lang.String name)
Loads a resource with the given name, using the correct ClassLoader. |
java.util.Enumeration |
getResources(java.lang.String name)
Loads a resource with the given name, using the correct ClassLoader. |
java.util.Enumeration |
getResources(java.lang.String name,
java.lang.ClassLoader cl)
Loads a resource with the given name, using the correct ClassLoader. |
java.net.URL |
getSystemResource(java.lang.String name)
Loads a resource with the given name, using the correct ClassLoader. |
java.util.Enumeration |
getSystemResources(java.lang.String name)
Get the resource associated with the given name from the System classloader. |
protected static java.lang.ClassLoader |
getThreadClassLoader(java.lang.Thread t)
Use reflection to get the thread (context) class loader. |
protected java.lang.Class |
loadClass(java.lang.String name)
Loads a class with the given name, using the correct ClassLoader. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public ClassLoadHelper()
public ClassLoadHelper(java.lang.Class clazz)
clazz
- the class to pull the classloader from.
java.lang.NullPointerException
- if clazz is null.public ClassLoadHelper(java.lang.ClassLoader cl)
cl
- the classloader to pull all requested classes from, or
it will use the thread's context class loader if cl is
null.Thread.getContextClassLoader()
Method Detail |
public java.lang.Class getClass(java.lang.String name)
name
- the name of the class to load.
public java.lang.Class getClass(java.lang.String name, boolean swallowExceptions)
name
- the name of the class to load.swallowExceptions
- true if this method is to return
java.lang.IllegalStateException
- if there was an error during
initialization and swallowExceptions is false.public java.lang.Object createObject(java.lang.String className)
className
- the name of the class to create an instance.
getClass( String )
,
createObject( String, boolean )
,
createObject( Class )
,
createObject( Class, boolean )
public java.lang.Object createObject(java.lang.String className, boolean swallowExceptions)
className
- the name of the class to create an instance.swallowExceptions
- true if this method is to return
null on any exceptions, or false if it should
throw an IllegalStateException on any error.
java.lang.IllegalStateException
- if there was an error during
initialization and swallowExceptions is false.getClass( String )
,
createObject( String )
,
createObject( Class )
,
createObject( Class, boolean )
public java.lang.Object createObject(java.lang.Class c)
c
- the Class object from which a new instance will be created
using its default constructor.
public java.lang.Object createObject(java.lang.Class c, boolean swallowExceptions)
c
- the Class object from which a new instance will be created
using its default constructor.swallowExceptions
- true if this method is to return
null on any exceptions, or false if it should
throw an IllegalStateException on any error.
java.lang.IllegalStateException
- if there was an error during
initialization and swallowExceptions is false.public java.lang.Object createObjectFromProperty(java.lang.String propertyClassName, java.lang.Class defaultClass, boolean swallowExceptions)
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.
propertyClassName
- the System Property name, whose value will be
used as a fully-qualified Class name to load and instantiate and
return.defaultClass
- if the System Property propertyClassName
is not defined, then this will be the class to instantiate and
return.swallowExceptions
- true if this method is to return
null on any exceptions, or false if it should
throw an IllegalStateException on any error.
java.lang.IllegalStateException
- if there was an error during
initialization and swallowExceptions is false.public java.lang.Object createObjectFromProperty(java.lang.String propertyClassName, java.lang.Class defaultClass, java.util.Hashtable properties, boolean swallowExceptions)
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.
propertyClassName
- the System Property name, whose value will be
used as a fully-qualified Class name to load and instantiate and
return.defaultClass
- if the System Property propertyClassName
is not defined, then this will be the class to instantiate and
return.properties
- a Hashtable of String -> String mappings.swallowExceptions
- true if this method is to return
null on any exceptions, or false if it should
throw an IllegalStateException on any error.
java.lang.IllegalStateException
- if there was an error during
initialization and swallowExceptions is false.public java.io.InputStream getResourceAsStream(java.lang.String name) throws java.io.IOException
name
- absolute referece to the expected resource.
java.io.IOException
- if an I/O error occurs.ClassLoader.getResource( String )
,
ClassLoader.getResourceAsStream( String )
public java.net.URL getResource(java.lang.String name) throws java.io.IOException
name
- absolute referece to the expected resource.
java.io.IOException
- if an I/O error occurs.ClassLoader.getResource( String )
,
ClassLoader.getResourceAsStream( String )
public java.net.URL getResource(java.lang.String name, java.lang.ClassLoader cl) throws java.io.IOException
name
- absolute referece to the expected resource.cl
- the classloader to load the reference from.
java.io.IOException
- if an I/O error occurs.ClassLoader.getResource( String )
,
ClassLoader.getResourceAsStream( String )
public java.net.URL getSystemResource(java.lang.String name) throws java.io.IOException
name
- absolute referece to the expected resource.
java.io.IOException
- if an I/O error occurs.ClassLoader.getResource( String )
,
ClassLoader.getResourceAsStream( String )
public java.util.Enumeration getResources(java.lang.String name) throws java.io.IOException
name
- absolute referece to the expected resource.
java.io.IOException
- if an I/O error occurs.ClassLoader.getResource( String )
,
ClassLoader.getResources( String )
,
ClassLoader.getResourceAsStream( String )
public java.util.Enumeration getResources(java.lang.String name, java.lang.ClassLoader cl) throws java.io.IOException
name
- absolute referece to the expected resource.cl
- the classloader to load the references from.
java.io.IOException
- if an I/O error occurs.ClassLoader.getResource( String )
,
ClassLoader.getResources( String )
,
ClassLoader.getResourceAsStream( String )
public java.util.Enumeration getSystemResources(java.lang.String name) throws java.io.IOException
name
- absolute referece to the expected resource.
java.io.IOException
- if an I/O error occurs.ClassLoader.getResource( String )
,
ClassLoader.getResources( String )
,
ClassLoader.getResourceAsStream( String )
protected java.lang.ClassLoader getClassLoader()
protected java.lang.Class loadClass(java.lang.String name) throws java.lang.ClassNotFoundException, java.lang.LinkageError, java.lang.IllegalArgumentException
java.lang.ClassNotFoundException
- if the class name is not known by the
class loader.
java.lang.LinkageError
- if there was a basic class loader error.
java.lang.IllegalArgumentException
- if the class doesn't smell right to
JDK 1.1.protected static java.lang.ClassLoader getThreadClassLoader(java.lang.Thread t)
protected static java.lang.reflect.Method discoverContextClassloaderMethod()
protected static java.lang.reflect.Method discoverGetResourcesMethod()
protected static java.lang.reflect.Method discoverGetSystemResourcesMethod()
protected java.lang.String getAbsoluteResourceName(java.lang.String name)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |