Class DynamicLoader


  • public class DynamicLoader
    extends java.lang.Object
    This class provides a dynamic class loading facility. It is tightly coupled to the property facility. To dynamically obtain an instance of a class through its constructor:
     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);
     }
     
    Version:
    $Revision$
    Author:
    Karan Vahi, Jens-S. Vöckler
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String m_classname
      Stores the fully qualified class name to dynamically instantiate.
    • Constructor Summary

      Constructors 
      Constructor Description
      DynamicLoader​(java.lang.String classname)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String convertException​(java.lang.Exception e)
      Converts an exception from the class loader into an error message.
      static java.lang.String convertException​(java.lang.String classname, java.lang.Exception e)
      Converts an exception from the class loader into an error message.
      static java.lang.String convertExceptionToString​(java.lang.String classname, java.lang.Throwable e)
      Converts an exception from the class loader into an error message.
      java.lang.String getClassName()
      Obtains the fully-qualified class name that this instance works with.
      java.lang.Object instantiate​(java.lang.Class[] classes, java.lang.Object[] arguments)
      Dynamically instantiates a class from a contructor.
      java.lang.Object instantiate​(java.lang.Object[] arguments)
      Dynamically instantiates a class from a contructor.
      void setClassName​(java.lang.String classname)
      Sets the fully-qualified class name to load.
      java.lang.Object static_method​(java.lang.String method, java.lang.Object[] arguments)
      Dynamically instantiates a class from a static method which constructs the resulting object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • m_classname

        private java.lang.String m_classname
        Stores the fully qualified class name to dynamically instantiate.
    • Constructor Detail

      • DynamicLoader

        public DynamicLoader​(java.lang.String classname)
    • Method Detail

      • setClassName

        public void setClassName​(java.lang.String classname)
        Sets the fully-qualified class name to load.
        Parameters:
        classname - is the new class name.
        See Also:
        getClassName()
      • getClassName

        public java.lang.String getClassName()
        Obtains the fully-qualified class name that this instance works with.
        Returns:
        the class name.
        See Also:
        setClassName( String )
      • instantiate

        public java.lang.Object instantiate​(java.lang.Object[] arguments)
                                     throws java.lang.ClassNotFoundException,
                                            java.lang.NoSuchMethodException,
                                            java.lang.InstantiationException,
                                            java.lang.IllegalAccessException,
                                            java.lang.reflect.InvocationTargetException
        Dynamically instantiates a class from a contructor. You must have set the class name before invoking this method. Please note that any exceptions thrown by the constructor will be wrapped into a InvocationTargetException.
        Parameters:
        arguments - are arguments to the constructor of the class to load. Please use "new Object[0]" for the argumentless default constructor.
        Returns:
        an instance that must be cast to the correct class.
        Throws:
        java.lang.ClassNotFoundException - if the driver for the database cannot be loaded. You might want to check your CLASSPATH, too.
        java.lang.NoSuchMethodException - if the driver's constructor interface does not comply with the database driver API.
        java.lang.InstantiationException - if the driver class is an abstract class instead of a concrete implementation.
        java.lang.IllegalAccessException - if the constructor for the driver class it not publicly accessible to this package.
        java.lang.reflect.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 database
        See Also:
        setClassName( String )
      • instantiate

        public java.lang.Object instantiate​(java.lang.Class[] classes,
                                            java.lang.Object[] arguments)
                                     throws java.lang.ClassNotFoundException,
                                            java.lang.NoSuchMethodException,
                                            java.lang.InstantiationException,
                                            java.lang.IllegalAccessException,
                                            java.lang.reflect.InvocationTargetException
        Dynamically instantiates a class from a contructor. You must have set the class name before invoking this method. Please note that any exceptions thrown by the constructor will be wrapped into a InvocationTargetException.

        This method should be invoked, if the constructor declares interface types as formal arguments, but the actual arguments are implementation classes.

        Parameters:
        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.
        Returns:
        an instance that must be cast to the correct class.
        Throws:
        java.lang.ClassNotFoundException - if the driver for the database cannot be loaded. You might want to check your CLASSPATH, too.
        java.lang.NoSuchMethodException - if the driver's constructor interface does not comply with the database driver API.
        java.lang.IllegalArgumentException - is thrown, if the number of arguments do not match the number of types, ie the vector have mismatching sizes.
        java.lang.InstantiationException - if the driver class is an abstract class instead of a concrete implementation.
        java.lang.IllegalAccessException - if the constructor for the driver class it not publicly accessible to this package.
        java.lang.reflect.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 database
        See Also:
        setClassName( String )
      • static_method

        public java.lang.Object static_method​(java.lang.String method,
                                              java.lang.Object[] arguments)
                                       throws java.lang.ClassNotFoundException,
                                              java.lang.SecurityException,
                                              java.lang.NoSuchMethodException,
                                              java.lang.InstantiationException,
                                              java.lang.IllegalAccessException,
                                              java.lang.reflect.InvocationTargetException,
                                              java.lang.NullPointerException,
                                              java.lang.IllegalArgumentException
        Dynamically instantiates a class from a static method which constructs the resulting object. You must have set the class name before invoking this method. Please note that any exceptions thrown by the constructor will be wrapped into a InvocationTargetException.
        Parameters:
        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.
        Returns:
        an instance that must be cast to the correct class.
        Throws:
        java.lang.ClassNotFoundException - if the driver for the database cannot be loaded. You might want to check your CLASSPATH, too.
        java.lang.NoSuchMethodException - if the driver's constructor interface does not comply with the database driver API.
        java.lang.InstantiationException - if the driver class is an abstract class instead of a concrete implementation.
        java.lang.IllegalAccessException - if the constructor for the driver class it not publicly accessible to this package.
        java.lang.reflect.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 database
        java.lang.SecurityException - if you are not permitted to invoke the method, or even list the methods provided by the class.
        java.lang.NullPointerException - if the method name is null.
        java.lang.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.
        See Also:
        setClassName( String )
      • convertException

        public static java.lang.String convertException​(java.lang.String classname,
                                                        java.lang.Exception e)
        Converts an exception from the class loader into an error message.
        Parameters:
        classname - is the name or some other class signifier.
        e - is the exception thrown by the class loader.
        Returns:
        a string that tries to describe what went wrong.
      • convertException

        public java.lang.String convertException​(java.lang.Exception e)
        Converts an exception from the class loader into an error message.
        Parameters:
        e - is the exception thrown by the class loader.
        Returns:
        a string that tries to describe what went wrong.
      • convertExceptionToString

        public static java.lang.String convertExceptionToString​(java.lang.String classname,
                                                                java.lang.Throwable e)
        Converts an exception from the class loader into an error message. Note: It does not convert any cause messages.
        Parameters:
        classname - is the name or some other class signifier.
        e - is the exception thrown by the class loader.
        Returns:
        a string that tries to describe what went wrong.