org.hibernate.search.util.impl

## Class ClassLoaderHelper

- java.lang.Object
- - org.hibernate.search.util.impl.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, like the target class is missing a proper constructor, is an interface, is not found...
  Author:
    Sanne Grinovero, Hardy Ferentschik, Ales Justin

- - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static Analyzer` | `analyzerInstanceFromClass(Class<?> classToInstantiate, Version luceneMatchVersion)`
    `static <T> Class<? extends T>` | `classForName(Class<T> targetSuperType, String classNameToLoad, String componentDescription, ServiceManager serviceManager)`
    `static Class` | `classForName(String classNameToLoad, ServiceManager serviceManager)` Perform resolution of a class name.
    `static Class<?>` | `classForName(String classNameToLoad, String componentDescription, ServiceManager serviceManager)`
    `static <T> T` | `instanceFromClass(Class<T> targetSuperType, Class<?> classToLoad, String componentDescription)` Creates an instance of target class
    `static <T> T` | `instanceFromClass(Class<T> targetSuperType, Class<?> classToLoad, String componentDescription, Map<String,String> constructorParameter)` Creates an instance of target class having a Map of strings as constructor parameter.
    `static <T> T` | `instanceFromName(Class<T> targetSuperType, String classNameToLoad, String componentDescription, ServiceManager serviceManager)` 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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### instanceFromName

          public static <T> T instanceFromName(Class<T> targetSuperType,
                                               String classNameToLoad,
                                               String componentDescription,
                                               ServiceManager serviceManager)

      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
        `componentDescription` - a meaningful description of the role the instance will have, used to enrich error messages to describe the context of the error
        `serviceManager` - Service manager allowing access to the class loading service
      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,
                                                String componentDescription)

      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
        `componentDescription` - a role name/description to contextualize error messages
      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.

    - #### instanceFromClass

          public static <T> T instanceFromClass(Class<T> targetSuperType,
                                                Class<?> classToLoad,
                                                String componentDescription,
                                                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
        `componentDescription` - a role name/description to contextualize error messages
        `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.

    - #### analyzerInstanceFromClass

          public static Analyzer analyzerInstanceFromClass(Class<?> classToInstantiate,
                                                           Version luceneMatchVersion)

    - #### classForName

          public static Class<?> classForName(String classNameToLoad,
                                              String componentDescription,
                                              ServiceManager serviceManager)

    - #### classForName

          public static <T> Class<? extends T> classForName(Class<T> targetSuperType,
                                                            String classNameToLoad,
                                                            String componentDescription,
                                                            ServiceManager serviceManager)

    - #### classForName

          public static Class classForName(String classNameToLoad,
                                           ServiceManager serviceManager)

      Perform resolution of a class name.

      Here we first check the context classloader, if one, before delegating to `Class.forName(String, boolean, ClassLoader)` using the caller's classloader

      Parameters:
        `classNameToLoad` - The class name
        `serviceManager` - The service manager from which to retrieve the class loader service
      Returns:
        The class reference.
      Throws:
        `ClassLoadingException` - From `Class.forName(String, boolean, ClassLoader)`.

Copyright © 2006-2017 Red Hat, Inc. All Rights Reserved