Class ClassUtils
java.lang.Object
org.apache.commons.discovery.tools.ClassUtils
Various utilities to interact with
Class types.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic MethodfindPublicStaticMethod(Class<?> clazz, Class<?> returnType, String methodName, Class<?>[] paramTypes) Looks forpublic static returnType methodName(paramTypes).static StringgetPackageName(Class<?> clazz) Get package name.static <T> TnewInstance(Class<T> impl, Class<?>[] paramClasses, Object[] params) Creates a new instance of the input class using the following policy: ifparamClassesorparamsis null, the default constructor will be used; the public constructor withparamClassesarguments type, withparamsas arguments value, will be used.static voidsetLog(org.apache.commons.logging.Log _log) Deprecated.This method is not thread-safestatic voidverifyAncestory(Class<?> spi, Class<?> impl) Throws exception ifimpldoes not implement or extend the SPI.
-
Constructor Details
-
ClassUtils
public ClassUtils()
-
-
Method Details
-
setLog
Deprecated.This method is not thread-safeSets theLogfor this class.- Parameters:
_log- This classLog
-
getPackageName
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 forpublic static returnType methodName(paramTypes).- Parameters:
clazz- The class where looking for the methodreturnType- The method return typemethodName- The method nameparamTypes- 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:- if
paramClassesorparamsis null, the default constructor will be used; - the public constructor with
paramClassesarguments type, withparamsas arguments value, will be used.
- Type Parameters:
T- The class type has to be instantiated- Parameters:
impl- The class has to be instantiatedparamClasses- The constructor arguments types (can benull)params- The constructor arguments values (can benull)- 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 SPIInstantiationException- seeClass.newInstance()IllegalAccessException- seeClass.newInstance()NoSuchMethodException- seeClass.newInstance()InvocationTargetException- seeClass.newInstance()
- if
-
verifyAncestory
Throws exception ifimpldoes not implement or extend the SPI.- Parameters:
spi- The SPI typeimpl- The class has to be verified is a SPI implementation/extension- Throws:
DiscoveryException- if the input implementation class is not an SPI implementation
-