org.hibernate.search.util.impl
Class ClassLoaderHelper

java.lang.Object
  extended by 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
static org.apache.lucene.analysis.Analyzer analyzerInstanceFromClass(Class<?> classToInstantiate, org.apache.lucene.util.Version luceneMatchVersion)
           
static Class classForName(String name)
          Perform resolution of a class name.
static Class classForName(String name, ClassLoader classLoader)
          Perform resolution of a class name.
static Class<?> classForName(String classNameToLoad, ClassLoader classLoader, String componentDescription)
           
static Enumeration<URL> getResources(String resourceName, Class<?> caller)
          Load all resources matching a specific name
static
<T> T
instanceFromClass(Class<T> targetSuperType, Class<?> classToLoad, String componentDescription)
          Creates an instance of target class
static
<T> T
instanceFromName(Class<T> targetSuperType, String classNameToLoad, Class<?> caller, String componentDescription)
          Creates an instance of a target class designed by fully qualified name
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getResources

public static Enumeration<URL> getResources(String resourceName,
                                            Class<?> caller)
Load all resources matching a specific name

Parameters:
resourceName - the resource name
caller - the caller
Returns:
found resource URLs

instanceFromName

public static <T> T instanceFromName(Class<T> targetSuperType,
                                     String classNameToLoad,
                                     Class<?> caller,
                                     String componentDescription)
Creates an instance of a target class designed by fully qualified name

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
caller - the class of the caller, needed for classloading purposes
componentDescription - a meaningful description of the role the instance will have, used to enrich error messages to describe the context of the error
Returns:
a new instance of 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.

analyzerInstanceFromClass

public static org.apache.lucene.analysis.Analyzer analyzerInstanceFromClass(Class<?> classToInstantiate,
                                                                            org.apache.lucene.util.Version luceneMatchVersion)

classForName

public static Class<?> classForName(String classNameToLoad,
                                    ClassLoader classLoader,
                                    String componentDescription)

classForName

public static Class classForName(String name,
                                 ClassLoader classLoader)
                          throws ClassNotFoundException
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:
name - The class name
classLoader - The classloader from which this call originated.
Returns:
The class reference.
Throws:
ClassNotFoundException - From Class.forName(String, boolean, ClassLoader).

classForName

public static Class classForName(String name)
                          throws ClassNotFoundException
Perform resolution of a class name.

Same as classForName(String, ClassLoader) except that here we delegate to Class.forName(String) if the context classloader lookup is unsuccessful.

Parameters:
name - The class name
Returns:
The class reference.
Throws:
ClassNotFoundException - From Class.forName(String).


Copyright © 2006-2012 Red Hat Middleware, LLC. All Rights Reserved