public class ClassUtil extends Object
| Constructor and Description |
|---|
ClassUtil() |
| Modifier and Type | Method and Description |
|---|---|
static String |
buildClassPath(Class<?>... classes) |
static String |
buildClassPath(String... paths) |
static String |
extractPackageName(String className)
Extracts the package name part of a qualified class name.
|
static String |
extractShortClassName(String className)
Extracts the class name part of a qualified class name.
|
static Class<?> |
getClass(Object obj)
Gets the runtime class of a given object.
|
static Class<?> |
getClass(String className)
Gets the class reference for a given qualified class name.
|
static String |
getClassFilePath(Class<?> cls) |
static String |
getClassName(Class<?> classRef)
Gets the non-qualified (w/o package name prefix) class name of a given class reference.
|
static String |
getClassName(Object obj)
Gets the non-qualified (without package name prefix) class name for a given object.
|
static String |
getPackageName(Class<?> classRef)
Gets the package name of a given class reference.
|
static String |
getPackageName(Object obj)
Gets the name of the package containing a given object's class.
|
static String |
getPath(Class<?> cls) |
static String |
getQualifiedClassName(Class<?> classRef)
Gets the qualified class name of a given class reference.
|
static String |
getQualifiedClassName(Object obj)
Gets the qualified class name of a given object.
|
static String |
getShortClassName(Class<?> classRef)
Gets the non-qualified class name (w/o package name prefix) of a given class reference.
|
static String |
getShortClassName(Object obj)
Gets the non-qualified class name (w/o package name prefix) of a given object.
|
static <T> T |
instantiateClass(Class<T> classRef)
Creates a new instance of a given class.
|
static <T> T |
instantiateClass(Class<T> classRef,
Object... constructorParameters)
Creates a new instance of the class using a given class loader and initialization parameters.
|
static Object |
instantiateClass(String className)
Creates a new instance of the class with a given class name.
|
static Object |
instantiateClass(String className,
ClassLoader classLoader)
Creates a new instance of the class with a given class name using a given class loader.
|
static Object |
instantiateClass(String className,
ClassLoader classLoader,
Object... constructorParameters)
Creates a new instance of the class with a given class name and initialization parameters using a given class loader.
|
static Object |
instantiateClass(String className,
Object... constructorParameters)
Creates a new instance of the class with a given class name and initialization parameters.
|
static boolean |
isInJar(Class<?> cls,
String resPath)
Detect if a given class is located in a JAR.
|
static String |
qualifyName(Class<?> siblingClassRef,
String shortClassName)
Builds a qualified class name for the given class name.
|
static String |
qualifyName(Object sibling,
String shortClassName)
Builds a qualified class name for the given class name.
|
static String |
qualifyName(String packageName,
String shortClassName)
Builds a qualified class name from given parts.
|
public static Class<?> getClass(Object obj)
obj - The given objectpublic static Class<?> getClass(String className)
className - The given class namepublic static String getClassName(Object obj)
obj - The given objectpublic static String getClassName(Class<?> classRef)
classRef - The given class referencepublic static String getQualifiedClassName(Object obj)
obj - The given objectpublic static String getQualifiedClassName(Class<?> classRef)
classRef - The given class referencepublic static String getShortClassName(Class<?> classRef)
classRef - The given class referencepublic static String getShortClassName(Object obj)
obj - The given objectpublic static String getPackageName(Object obj)
obj - The given objectpublic static String getPackageName(Class<?> classRef)
classRef - The given class referencepublic static String extractPackageName(String className)
className - Qualified class namepublic static String extractShortClassName(String className)
className - Qualified class namepublic static String qualifyName(String packageName, String shortClassName)
packageName - Package nameshortClassName - Class namepublic static String qualifyName(Class<?> siblingClassRef, String shortClassName)
siblingClassRef - Reference to another class in the same packageshortClassName - Non-qualified name of the class to get a qualified name forpublic static String qualifyName(Object sibling, String shortClassName)
sibling - Existing object, an instance of another class in the same packageshortClassName - Non-qualified name of the class to get a qualified name forpublic static <T> T instantiateClass(Class<T> classRef) throws InstantiationException, IllegalAccessException
T - the type of the given classclassRef - The given classInstantiationException - when we fail to create an instance.IllegalAccessException - when trying to create an instance but don't have the right.public static Object instantiateClass(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException
className - The given class nameInstantiationException - when we fail to create an instance.IllegalAccessException - when trying to create an instance but don't have the right.ClassNotFoundException - when no definition for the class was foundpublic static Object instantiateClass(String className, ClassLoader classLoader) throws InstantiationException, IllegalAccessException, ClassNotFoundException
className - The given class nameclassLoader - The class loader from which the class must be loadedInstantiationException - when we fail to create an instance.IllegalAccessException - when trying to create an instance but don't have the right.ClassNotFoundException - when no definition for the class was foundpublic static <T> T instantiateClass(Class<T> classRef, Object... constructorParameters) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException
T - the type of the class to createclassRef - The given classconstructorParameters - The initialization parameters for the class constructorSecurityException - if we don't have the right to create the classNoSuchMethodException - when a constructor cannot be foundIllegalArgumentException - if the classRef was null.InstantiationException - when we fail to create an instance.IllegalAccessException - when trying to create an instance but don't have the right.InvocationTargetException - when the constructor throws an exception.public static Object instantiateClass(String className, Object... constructorParameters) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
className - The given class nameconstructorParameters - The initialization parameters for the class constructorSecurityException - if we don't have the right to create the classNoSuchMethodException - when a constructor cannot be foundIllegalArgumentException - if the class name is null or emptyInstantiationException - when we fail to create an instance.IllegalAccessException - when trying to create an instance but don't have the right.InvocationTargetException - when the constructor throws an exception.ClassNotFoundException - when no definition for the class was foundpublic static Object instantiateClass(String className, ClassLoader classLoader, Object... constructorParameters) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
className - The given class nameclassLoader - The given class loaderconstructorParameters - The initialization parameters for the class constructorSecurityException - if we don't have the right to create the classNoSuchMethodException - when a constructor cannot be foundIllegalArgumentException - if the class name is null or empty, or if the classLoader is null.InstantiationException - when we fail to create an instance.IllegalAccessException - when trying to create an instance but don't have the right.InvocationTargetException - when the constructor throws an exception.ClassNotFoundException - when no definition for the class was foundpublic static boolean isInJar(Class<?> cls, String resPath)
cls - the given class.resPath - resource path inside the jarCopyright © 2022. All rights reserved.