Package com.aspectran.utils
Class ClassUtils
java.lang.Object
com.aspectran.utils.ClassUtils
Miscellaneous class utility methods.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
The ".class" file suffixstatic final char
The package separator character '.' -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>
classForName
(String name) static Class<?>
classForName
(String name, ClassLoader classLoader) static <T> T
createInstance
(Class<T> clazz) Method that can be called to try to create an instance of specified type.static <T> T
createInstance
(Class<T> clazz, Object... args) Method that can be called to try to create an instantiate of specified type.static <T> T
createInstance
(Class<T> clazz, Object[] args, Class<?>[] argTypes) Method that can be called to try to create an instance of specified type.static <T> Constructor<T>
findConstructor
(Class<T> clazz, Class<?>... argTypes) Obtain an accessible constructor for the given class and parameters.static ClassLoader
Returns the default class loader within the current context.static boolean
isLambdaClass
(Class<?> clazz) Determine if the suppliedClass
is a JVM-generated implementation class for a lambda expression or method reference.static boolean
isVisible
(Class<?> clazz, ClassLoader classLoader) Check whether the given class is visible in the given ClassLoader.static Class<?>
static ClassLoader
overrideThreadContextClassLoader
(ClassLoader classLoaderToUse) Override the thread context ClassLoader with the environment's bean ClassLoader if necessary, i.e.static void
restoreThreadContextClassLoader
(ClassLoader classLoader)
-
Field Details
-
PACKAGE_SEPARATOR_CHAR
public static final char PACKAGE_SEPARATOR_CHARThe package separator character '.'- See Also:
-
CLASS_FILE_SUFFIX
The ".class" file suffix- See Also:
-
-
Constructor Details
-
ClassUtils
public ClassUtils()
-
-
Method Details
-
createInstance
Method that can be called to try to create an instance of specified type. Instantiation is done using default no-argument constructor.- Type Parameters:
T
- the generic type- Parameters:
clazz
- the class to check- Returns:
- an instantiated object
- Throws:
IllegalArgumentException
- if instantiation fails for any reason; except for cases where constructor throws an unchecked exception (which will be passed as is)
-
createInstance
Method that can be called to try to create an instantiate of specified type.- Type Parameters:
T
- the generic type- Parameters:
clazz
- the class to checkargs
- the arguments- Returns:
- an instantiated object
- Throws:
IllegalArgumentException
- if instantiation fails for any reason; except for cases where constructor throws an unchecked exception (which will be passed as is)
-
createInstance
Method that can be called to try to create an instance of specified type.- Type Parameters:
T
- the generic type- Parameters:
clazz
- the class to checkargs
- the argumentsargTypes
- the argument types of the desired constructor- Returns:
- an instantiated object
- Throws:
IllegalArgumentException
- if instantiation fails for any reason; except for cases where constructor throws an unchecked exception (which will be passed as is)
-
findConstructor
@NonNull public static <T> Constructor<T> findConstructor(Class<T> clazz, Class<?>... argTypes) throws NoSuchMethodException Obtain an accessible constructor for the given class and parameters.- Type Parameters:
T
- the generic type- Parameters:
clazz
- the class to checkargTypes
- the argument types of the desired constructor- Returns:
- the constructor reference
- Throws:
NoSuchMethodException
- if no such constructor exists
-
isVisible
Check whether the given class is visible in the given ClassLoader.- Parameters:
clazz
- the class to check (typically an interface)classLoader
- the ClassLoader to check against (may benull
in which case this method will always returntrue
)- Returns:
- true if the given class is visible; otherwise false
- Since:
- 6.0.0
-
isLambdaClass
Determine if the suppliedClass
is a JVM-generated implementation class for a lambda expression or method reference.This method makes a best-effort attempt at determining this, based on checks that work on modern, mainstream JVMs.
- Parameters:
clazz
- the class to check- Returns:
true
if the class is a lambda implementation class
-
classForName
- Throws:
ClassNotFoundException
-
classForName
@NonNull public static Class<?> classForName(String name, ClassLoader classLoader) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
loadClass
- Throws:
ClassNotFoundException
-
getDefaultClassLoader
Returns the default class loader within the current context. If there is a context classloader it is returned, otherwise the classloader which loaded the ClassUtils Class is returned.- Returns:
- the appropriate default classloader which is guaranteed to be non-null
-
overrideThreadContextClassLoader
@Nullable public static ClassLoader overrideThreadContextClassLoader(@Nullable ClassLoader classLoaderToUse) Override the thread context ClassLoader with the environment's bean ClassLoader if necessary, i.e. if the bean ClassLoader is not equivalent to the thread context ClassLoader already.- Parameters:
classLoaderToUse
- the actual ClassLoader to use for the thread context- Returns:
- the original thread context ClassLoader, or
null
if not overridden
-
restoreThreadContextClassLoader
-