Package org.apache.deltaspike.core.util
Class ClassUtils
- java.lang.Object
-
- org.apache.deltaspike.core.util.ClassUtils
-
@Typed public abstract class ClassUtils extends Object
Util methods for classes,ClassLoaderandManifesthandling
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancontainsMethod(Class<?> targetClass, Method method)Checks if the given class contains a method with the same signature.static booleancontainsPossiblyGenericMethod(Class<?> targetClass, Method method)Checks if the given class has a method with the same signature, taking in to account generic typesstatic MethodextractMethod(Class<?> clazz, Method sourceMethod)Extracts a method with same signature as the source method.static MethodextractPossiblyGenericMethod(Class<?> clazz, Method sourceMethod)Extracts a method matching the source method, allowing generic type parameters to be substituted as long as they are properly castable.static ClassLoadergetClassLoader(Object o)Detect the right ClassLoader.static StringgetJarVersion(Class targetClass)Reads the version of the jar which contains the given classstatic StringgetRevision(Class targetClass)Reads the VCS revision which was used for creating the jarstatic ObjectinstantiateClassForName(String className)Creates an instance for the given class-namestatic booleanisProxyableClass(Type type)Checks whether the CDI rules for proxyable beans are met.static ClassloadClassForName(String name)Loads class for the given namestatic booleanreturns(Method method, Class<?> clazz)returnsstatic <T> TtryToInstantiateClass(Class<T> targetClass)Instantiates a given class via the default constructorstatic ObjecttryToInstantiateClassForName(String className)Tries to instantiate a class for the given name via the default constructorstatic <T> TtryToInstantiateClassForName(String className, Class<T> targetType)Tries to instantiate a class for the given name and type via the default constructorstatic ClasstryToLoadClassForName(String name)Tries to load a class based on the given namestatic <T> Class<T>tryToLoadClassForName(String name, Class<T> targetType)Tries to load a class based on the given name and interface or abstract class.static <T> Class<T>tryToLoadClassForName(String name, Class<T> targetType, ClassLoader classLoader)Tries to load a class based on the given name and interface or abstract class.static ClasstryToLoadClassForName(String name, ClassLoader classLoader)Tries to load a class based on the given name
-
-
-
Method Detail
-
getClassLoader
public static ClassLoader getClassLoader(Object o)
Detect the right ClassLoader. The lookup order is determined by:- ContextClassLoader of the current Thread
- ClassLoader of the given Object 'o'
- ClassLoader of this very ClassUtils class
- Parameters:
o- if notnullit may get used to detect the classloader.- Returns:
- The
ClassLoaderwhich should get used to create new instances
-
isProxyableClass
public static boolean isProxyableClass(Type type)
Checks whether the CDI rules for proxyable beans are met. See CDI spec unproxyable bean types- Parameters:
type- type- Returns:
trueif all proxy conditions are met,falseotherwise
-
tryToLoadClassForName
public static <T> Class<T> tryToLoadClassForName(String name, Class<T> targetType)
Tries to load a class based on the given name and interface or abstract class.- Type Parameters:
T- current type- Parameters:
name- name of the concrete classtargetType- target type (interface or abstract class)- Returns:
- loaded class or null if it isn't in the classpath
-
tryToLoadClassForName
public static <T> Class<T> tryToLoadClassForName(String name, Class<T> targetType, ClassLoader classLoader)
Tries to load a class based on the given name and interface or abstract class.- Type Parameters:
T- current type- Parameters:
name- name of the concrete classtargetType- target type (interface or abstract class)classLoader- TheClassLoader.- Returns:
- loaded class or null if it isn't in the classpath
-
tryToLoadClassForName
public static Class tryToLoadClassForName(String name)
Tries to load a class based on the given name- Parameters:
name- name of the class- Returns:
- loaded class or
nullif it isn't in the classpath
-
tryToLoadClassForName
public static Class tryToLoadClassForName(String name, ClassLoader classLoader)
Tries to load a class based on the given name- Parameters:
name- name of the classclassLoader- TheClassLoader.- Returns:
- loaded class or
nullif it isn't in the classpath
-
loadClassForName
public static Class loadClassForName(String name) throws ClassNotFoundException
Loads class for the given name- Parameters:
name- name of the class- Returns:
- loaded class
- Throws:
ClassNotFoundException- if the class can't be loaded
-
tryToInstantiateClass
public static <T> T tryToInstantiateClass(Class<T> targetClass)
Instantiates a given class via the default constructor- Type Parameters:
T- current type- Parameters:
targetClass- class which should be instantiated- Returns:
- created instance or null if the instantiation failed
-
tryToInstantiateClassForName
public static <T> T tryToInstantiateClassForName(String className, Class<T> targetType)
Tries to instantiate a class for the given name and type via the default constructor- Type Parameters:
T- current type- Parameters:
className- name of the classtargetType- target type- Returns:
- created instance or null if the instantiation failed
-
tryToInstantiateClassForName
public static Object tryToInstantiateClassForName(String className)
Tries to instantiate a class for the given name via the default constructor- Parameters:
className- name of the class- Returns:
- created instance or null if the instantiation failed
-
instantiateClassForName
public static Object instantiateClassForName(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException
Creates an instance for the given class-name- Parameters:
className- name of the class which should be instantiated- Returns:
- created instance
- Throws:
ClassNotFoundException- if the instantiation failedIllegalAccessException- if the instantiation failedInstantiationException- if the instantiation failed
-
getJarVersion
public static String getJarVersion(Class targetClass)
Reads the version of the jar which contains the given class- Parameters:
targetClass- class within the jar- Returns:
- version-string which has been found in the manifest or null if there is no version information available
-
getRevision
public static String getRevision(Class targetClass)
Reads the VCS revision which was used for creating the jar- Parameters:
targetClass- class within the jar- Returns:
- revision-string which has been found in the manifest or null if there is no information available
-
containsMethod
public static boolean containsMethod(Class<?> targetClass, Method method)
Checks if the given class contains a method with the same signature.- Parameters:
targetClass- The class to checkmethod- The source method- Returns:
- if it contains a method with the same signature.
-
extractMethod
public static Method extractMethod(Class<?> clazz, Method sourceMethod)
Extracts a method with same signature as the source method.- Parameters:
clazz- The target classsourceMethod- The source method.- Returns:
- the extracted method or
null
-
containsPossiblyGenericMethod
public static boolean containsPossiblyGenericMethod(Class<?> targetClass, Method method)
Checks if the given class has a method with the same signature, taking in to account generic types- Parameters:
targetClass- classmethod- method- Returns:
- if it contains a method with the same signature.
-
extractPossiblyGenericMethod
public static Method extractPossiblyGenericMethod(Class<?> clazz, Method sourceMethod)
Extracts a method matching the source method, allowing generic type parameters to be substituted as long as they are properly castable.- Parameters:
clazz- The target classsourceMethod- The source method.- Returns:
- the extracted method or
null
-
-