Module hw06

Class AObjectLoader<ReturnT>

java.lang.Object
provided.utils.loader.impl.AObjectLoader<ReturnT>
Type Parameters:
ReturnT - The type of object to be created.
All Implemented Interfaces:
IObjectLoader<ReturnT>
Direct Known Subclasses:
ObjectLoader

public abstract class AObjectLoader<ReturnT> extends Object implements IObjectLoader<ReturnT>
Factory that dynamically class loads and instantiates an object of type ReturnT
Author:
Stephen Wong
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected ILogger
    Logger for this and subclasses to use
    private Map<Class<?>,​Class<?>>
    Maps the primitive type class to the corresponding wrapper class, e.g.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the class.
    Constructor for the class.
  • Method Summary

    Modifier and Type
    Method
    Description
    private boolean
    checkTypes​(Class<?>[] paramTypes, Object[] args)
    Utility method to check if the types of the elements in the given args array match the types in the given paramTypes array.
    protected abstract ReturnT
    errorHandler​(Exception ex, String className, Object... args)
    Abstract loading exception handler method
    private Constructor<?>
    getConstructor​(Class<?> aClass, Object[] args)
    Proper form of Class.getConstructor(Class<?>[] parameterTypes) that returns a constructor that will return a constructor of the given class that will accept the given argument list.
    loadInstance​(String className, Object... args)
    Uses dynamic class loading to load and instantiate a subclass or implementation of ReturnT given a fully-qualified class name and an array (vararg) of input parameters.

    Methods inherited from class java.lang.Object

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

    • logger

      protected ILogger logger
      Logger for this and subclasses to use
    • primitiveTypes

      private Map<Class<?>,​Class<?>> primitiveTypes
      Maps the primitive type class to the corresponding wrapper class, e.g. Integer.TYPE which is the class representation of "int", to Class<Integer>. This is needed when a primitive input parameter type is encountered. An anonymous inner class is used here so that the map can be loaded at instantiation time.
  • Constructor Details

    • AObjectLoader

      public AObjectLoader()
      Constructor for the class. The given errorFac is used to generate instances when the loadInstance() method is otherwise unable to do so because of a processing error. Uses the shared logger from ILoggerControl.
    • AObjectLoader

      public AObjectLoader(ILogger logger)
      Constructor for the class. The given errorFac is used to generate instances when the loadInstance() method is otherwise unable to do so because of a processing error.
      Parameters:
      logger - The logger to use
  • Method Details

    • loadInstance

      public ReturnT loadInstance(String className, Object... args)
      Description copied from interface: IObjectLoader
      Uses dynamic class loading to load and instantiate a subclass or implementation of ReturnT given a fully-qualified class name and an array (vararg) of input parameters. Note that primitive types get auto-boxed into regular classes, e.g. int becomes Integer. Concrete implementations of this interface may have the ability to instantiate well-defined return instances in the event that an error occurs during the loading process. The returned object is instantiated using the constructor of the class corresponding to the given classname whose signature matches the number, order and types of the given constructor parameters.
      Specified by:
      loadInstance in interface IObjectLoader<ReturnT>
      Parameters:
      className - The fully-qualified name of the desired class which must be assignable to the ReturnT type.
      args - a varargs of input parameter values for the constructor of className
      Returns:
      An instance of the desired class, as ReturnT
    • errorHandler

      protected abstract ReturnT errorHandler(Exception ex, String className, Object... args)
      Abstract loading exception handler method
      Parameters:
      ex - The Exception that was generated
      className - The fully qualified name of the class that was attempted to be loaded
      args - The constructor parameters for the given class name.
      Returns:
      An "error" object
    • getConstructor

      private Constructor<?> getConstructor(Class<?> aClass, Object[] args) throws IllegalArgumentException
      Proper form of Class.getConstructor(Class<?>[] parameterTypes) that returns a constructor that will return a constructor of the given class that will accept the given argument list. Correctly finds a constructor even if the constructor types are superclasses of the given arguments except that it will not necessarily choose the more narrowed choice if more than one possibility exists.
      Parameters:
      aClass - A class object whose constructor is being searched for.
      args - a list of input values for the constructor
      Returns:
      a Constructor<?> that will work with the given set of input parameter values.
      Throws:
      IllegalArgumentException - when no constructor can be found that would accept the given parameters.
    • checkTypes

      private boolean checkTypes(Class<?>[] paramTypes, Object[] args)
      Utility method to check if the types of the elements in the given args array match the types in the given paramTypes array. That is, if a method takes input parameters of types paramTypes, would args be an allowable input?
      Parameters:
      paramTypes - An array of Class objects representing an ordered list of types
      args - An array of input parameter values, whose types are being checked against paramTypes.
      Returns:
      true if all arg elements are subclasses (assignable to) their corresponding paramTypes elements. Returns false otherwise.