Class ConditionalInstantiator


  • public class ConditionalInstantiator
    extends Object
    Allows instantiation of classes that may or may not be present on the classpath.
    • Constructor Detail

      • ConditionalInstantiator

        public ConditionalInstantiator​(String fullyQualifiedClassName)
        Constructor.
        Parameters:
        fullyQualifiedClassName - The fully-qualified name of the class that we intend to instantiate.
      • ConditionalInstantiator

        public ConditionalInstantiator​(String fullyQualifiedClassName,
                                       boolean throwExceptions)
        Constructor.
        Parameters:
        fullyQualifiedClassName - The fully-qualified name of the class that we intend to instantiate.
        throwExceptions - Whether to throw exceptions when the class can't be instantiated.
    • Method Detail

      • resolve

        public <T> Class<T> resolve()
        Attempts to resolve the type.
        Type Parameters:
        T - The resolved type.
        Returns:
        The corresponding class object if the type exists; null otherwise.
      • instantiate

        public <T> T instantiate​(Class<?>[] paramTypes,
                                 Object[] paramValues)
        Attempts to instantiate the type.
        Type Parameters:
        T - The type to instantiate.
        Parameters:
        paramTypes - The types of the constructor parameters of the constructor that we want to call.
        paramValues - The values that we want to pass into the constructor.
        Returns:
        An instance of the type given in the constructor with the given parameter values, or null if the type does not exist.
        Throws:
        ReflectionException - If instantiation fails.
      • callFactory

        public <T> T callFactory​(String factoryMethod,
                                 Class<?>[] paramTypes,
                                 Object[] paramValues)
        Attempts to call a static factory method on the type.
        Type Parameters:
        T - The return type of the factory method.
        Parameters:
        factoryMethod - The name of the factory method.
        paramTypes - The types of the parameters of the specific overload of the factory method we want to call.
        paramValues - The values that we want to pass into the factory method.
        Returns:
        An instance of the type given by the factory method with the given parameter values, or null of the type does not exist.
        Throws:
        ReflectionException - If the call to the factory method fails.
      • callFactory

        public <T> T callFactory​(String factoryTypeName,
                                 String factoryMethod,
                                 Class<?>[] paramTypes,
                                 Object[] paramValues)
        Attempts to call a static factory method on a type.
        Type Parameters:
        T - The return type of the factory method.
        Parameters:
        factoryTypeName - The type that contains the factory method.
        factoryMethod - The name of the factory method.
        paramTypes - The types of the parameters of the specific overload of the factory method we want to call.
        paramValues - The values that we want to pass into the factory method.
        Returns:
        An instance of the type given by the factory method with the given parameter values, or null of the type does not exist.
        Throws:
        ReflectionException - If the call to the factory method fails.
      • returnConstant

        public <T> T returnConstant​(String constantName)
        Attempts to resolve a static constant on the type.
        Type Parameters:
        T - The type of the constant.
        Parameters:
        constantName - The name of the constant.
        Returns:
        The value of the constant, or null if the type does not exist.
        Throws:
        ReflectionException - If resolving the constant fails.