Class ClassLoaderHelper


  • public class ClassLoaderHelper
    extends Object
    Utility class to load instances of other classes by using a fully qualified name, or from a class type. Uses reflection and throws SearchException(s) with proper descriptions of the error, such as the target class is missing a proper constructor, is an interface, is not found...
    Author:
    Sanne Grinovero, Hardy Ferentschik, Ales Justin
    • Method Detail

      • instanceFromName

        public static <T> T instanceFromName​(Class<T> targetSuperType,
                                             String classNameToLoad,
                                             ClassResolver classResolver)
        Creates an instance of a target class specified by the fully qualified class name using a ClassLoader as fallback when the class cannot be found in the context one.
        Type Parameters:
        T - matches the type of targetSuperType: defines the return type
        Parameters:
        targetSuperType - the return type of the function, the classNameToLoad will be checked to be assignable to this type.
        classNameToLoad - a fully qualified class name, whose type is assignable to targetSuperType
        classResolver - the ClassResolver to use to load classes
        Returns:
        a new instance of the type given by classNameToLoad
        Throws:
        SearchException - wrapping other error types with a proper error message for all kind of problems, like classNotFound, missing proper constructor, wrong type, security errors.
      • instanceFromClass

        public static <T> T instanceFromClass​(Class<T> targetSuperType,
                                              Class<?> classToLoad)
        Creates an instance of target class
        Type Parameters:
        T - the type of targetSuperType: defines the return type
        Parameters:
        targetSuperType - the created instance will be checked to be assignable to this type
        classToLoad - the class to be instantiated
        Returns:
        a new instance of classToLoad
        Throws:
        SearchException - wrapping other error types with a proper error message for all kind of problems, like missing proper constructor, wrong type, securitymanager errors.
      • untypedInstanceFromClass

        public static <T> T untypedInstanceFromClass​(Class<T> classToLoad)
        Creates an instance of target class. Similar to instanceFromClass(Class, Class) but not checking the created instance will be of any specific type: using instanceFromClass(Class, Class) should be preferred whenever possible.
        Type Parameters:
        T - the type of targetSuperType: defines the return type
        Parameters:
        classToLoad - the class to be instantiated
        Returns:
        a new instance of classToLoad
        Throws:
        SearchException - wrapping other error types with a proper error message for all kind of problems, like missing proper constructor, securitymanager errors.
      • instanceFromClass

        public static <T> T instanceFromClass​(Class<T> targetSuperType,
                                              Class<?> classToLoad,
                                              Map<String,​String> constructorParameter)
        Creates an instance of target class having a Map of strings as constructor parameter. Most of the Analyzer SPIs provided by Lucene have such a constructor.
        Type Parameters:
        T - the type of targetSuperType: defines the return type
        Parameters:
        targetSuperType - the created instance will be checked to be assignable to this type
        classToLoad - the class to be instantiated
        constructorParameter - a Map to be passed to the constructor. The loaded type must have such a constructor.
        Returns:
        a new instance of classToLoad
        Throws:
        SearchException - wrapping other error types with a proper error message for all kind of problems, like missing proper constructor, wrong type, security errors.
      • classForName

        public static <T> Class<? extends T> classForName​(Class<T> targetSuperType,
                                                          String classNameToLoad,
                                                          ClassResolver classResolver)