org.apache.commons.discovery.tools
Class ClassUtils

java.lang.Object
  extended by org.apache.commons.discovery.tools.ClassUtils

public class ClassUtils
extends Object

Various utilities to interact with Class types.


Constructor Summary
ClassUtils()
           
 
Method Summary
static Method findPublicStaticMethod(Class<?> clazz, Class<?> returnType, String methodName, Class<?>[] paramTypes)
          Looks for public static returnType methodName(paramTypes).
static String getPackageName(Class<?> clazz)
          Get package name.
static
<T> T
newInstance(Class<T> impl, Class<?>[] paramClasses, Object[] params)
          Creates a new instance of the input class using the following policy: if paramClasses or params is null, the default constructor will be used; the public constructor with paramClasses arguments type, with params as arguments value, will be used.
static void setLog(org.apache.commons.logging.Log _log)
          Deprecated. This method is not thread-safe
static void verifyAncestory(Class<?> spi, Class<?> impl)
          Throws exception if impl does not implement or extend the SPI.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassUtils

public ClassUtils()
Method Detail

setLog

@Deprecated
public static void setLog(org.apache.commons.logging.Log _log)
Deprecated. This method is not thread-safe

Sets the Log for this class.

Parameters:
_log - This class Log

getPackageName

public static String getPackageName(Class<?> clazz)
Get package name. Not all class loaders 'keep' package information, in which case Class.getPackage() returns null. This means that calling Class.getPackage().getName() is unreliable at best.

Parameters:
clazz - The class from which the package has to be extracted
Returns:
The string representation of the input class package

findPublicStaticMethod

public static Method findPublicStaticMethod(Class<?> clazz,
                                            Class<?> returnType,
                                            String methodName,
                                            Class<?>[] paramTypes)
Looks for public static returnType methodName(paramTypes).

Parameters:
clazz - The class where looking for the method
returnType - The method return type
methodName - The method name
paramTypes - The method arguments types
Returns:
Method public static returnType methodName(paramTypes), if found to be directly implemented by clazz.

newInstance

public static <T> T newInstance(Class<T> impl,
                                Class<?>[] paramClasses,
                                Object[] params)
                     throws DiscoveryException,
                            InstantiationException,
                            IllegalAccessException,
                            NoSuchMethodException,
                            InvocationTargetException
Creates a new instance of the input class using the following policy:

Type Parameters:
T - The class type has to be instantiated
Parameters:
impl - The class has to be instantiated
paramClasses - The constructor arguments types (can be null)
params - The constructor arguments values (can be null)
Returns:
A new class instance
Throws:
DiscoveryException - if the class implementing the SPI cannot be found, cannot be loaded and instantiated, or if the resulting class does not implement (or extend) the SPI
InstantiationException - see Class.newInstance()
IllegalAccessException - see Class.newInstance()
NoSuchMethodException - see Class.newInstance()
InvocationTargetException - see Class.newInstance()

verifyAncestory

public static void verifyAncestory(Class<?> spi,
                                   Class<?> impl)
                            throws DiscoveryException
Throws exception if impl does not implement or extend the SPI.

Parameters:
spi - The SPI type
impl - The class has to be verified is a SPI implementation/extension
Throws:
DiscoveryException - if the input implementation class is not an SPI implementation


Copyright © 2002-2011 The Apache Software Foundation. All Rights Reserved.