edu.isi.pegasus.common.util
public class DynamicLoader extends Object
Integer i = null; DynamicLoader dl = new DynamicLoader( "java.lang.Integer" ); try { // instantiate as Integer("42") String arg[] = new String[1]; arg[0] = "42"; i = (Integer) dl.instantiate(arg); } catch ( Exception e ) { System.err.println( dl.convertException(e) ); System.exit(1); }Similarily, to obtain an instance of a class through a static method provided by the same class, or another class:
Integer i = null; DynamicLoader dl = new DynamicLoader( "java.lang.Integer" ); try { // instantiate as Integer("42") String arg[] = new String[1]; arg[0] = "42"; i = (Integer) dl.static_method( "valueOf", arg ); } catch ( Exception e ) { System.err.println( dl.convertException(e) ); System.exit(1); }
Modifier and Type | Field and Description |
---|---|
private String |
m_classname
Stores the fully qualified class name to dynamically instantiate.
|
Constructor and Description |
---|
DynamicLoader(String classname) |
Modifier and Type | Method and Description |
---|---|
String |
convertException(Exception e)
Converts an exception from the class loader into an error message.
|
static String |
convertException(String classname,
Exception e)
Converts an exception from the class loader into an error message.
|
static String |
convertExceptionToString(String classname,
Throwable e)
Converts an exception from the class loader into an error message.
|
String |
getClassName()
Obtains the fully-qualified class name that this instance works with.
|
Object |
instantiate(Class[] classes,
Object[] arguments)
Dynamically instantiates a class from a contructor.
|
Object |
instantiate(Object[] arguments)
Dynamically instantiates a class from a contructor.
|
void |
setClassName(String classname)
Sets the fully-qualified class name to load.
|
Object |
static_method(String method,
Object[] arguments)
Dynamically instantiates a class from a static method which
constructs the resulting object.
|
private String m_classname
public DynamicLoader(String classname)
public void setClassName(String classname)
classname
- is the new class name.getClassName()
public String getClassName()
setClassName( String )
public Object instantiate(Object[] arguments) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException
InvocationTargetException
.arguments
- are arguments to the constructor of the class
to load. Please use "new Object[0]" for the argumentless default
constructor.ClassNotFoundException
- if the driver for the database
cannot be loaded. You might want to check your CLASSPATH, too.NoSuchMethodException
- if the driver's constructor interface
does not comply with the database driver API.InstantiationException
- if the driver class is an abstract
class instead of a concrete implementation.IllegalAccessException
- if the constructor for the driver
class it not publicly accessible to this package.InvocationTargetException
- if the constructor of the driver
throws an exception while being dynamically loaded.SQLException
- if the driver for the database can be
loaded, but faults when initially accessing the databasesetClassName( String )
public Object instantiate(Class[] classes, Object[] arguments) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException
InvocationTargetException
.This method should be invoked, if the constructor declares interface types as formal arguments, but the actual arguments are implementation classes.
classes
- is a vector of the classes involved. Each item
in the classes vector matches the item in the arguments vector.
The classes vector will be used to select the correct constructor.
Please use "new Class[0]" for the argumentless default ctor.arguments
- are arguments to the constructor of the class
to load. Please use "new Object[0]" for the argumentless default
constructor.ClassNotFoundException
- if the driver for the database
cannot be loaded. You might want to check your CLASSPATH, too.NoSuchMethodException
- if the driver's constructor interface
does not comply with the database driver API.IllegalArgumentException
- is thrown, if the number of
arguments do not match the number of types, ie the vector have
mismatching sizes.InstantiationException
- if the driver class is an abstract
class instead of a concrete implementation.IllegalAccessException
- if the constructor for the driver
class it not publicly accessible to this package.InvocationTargetException
- if the constructor of the driver
throws an exception while being dynamically loaded.SQLException
- if the driver for the database can be
loaded, but faults when initially accessing the databasesetClassName( String )
public Object static_method(String method, Object[] arguments) throws ClassNotFoundException, SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NullPointerException, IllegalArgumentException
InvocationTargetException
.method
- is the name of the static method to call.arguments
- are arguments to the constructor of the class
to load. Please use "new Object[0]" for the argumentless default
constructor.ClassNotFoundException
- if the driver for the database
cannot be loaded. You might want to check your CLASSPATH, too.NoSuchMethodException
- if the driver's constructor interface
does not comply with the database driver API.InstantiationException
- if the driver class is an abstract
class instead of a concrete implementation.IllegalAccessException
- if the constructor for the driver
class it not publicly accessible to this package.InvocationTargetException
- if the constructor of the driver
throws an exception while being dynamically loaded.SQLException
- if the driver for the database can be
loaded, but faults when initially accessing the databaseSecurityException
- if you are not permitted to invoke the
method, or even list the methods provided by the class.NullPointerException
- if the method name is
null
.IllegalArgumentException
- if the number of actual and
formal parameter differ, unwrapping a primitive type failed, or
a parameter value cannot be converted to the formal argument type.setClassName( String )
public static String convertException(String classname, Exception e)
classname
- is the name or some other class signifier.e
- is the exception thrown by the class loader.public String convertException(Exception e)
e
- is the exception thrown by the class loader.public static String convertExceptionToString(String classname, Throwable e)
classname
- is the name or some other class signifier.e
- is the exception thrown by the class loader.