Package com.helger.commons.lang
Class ClassHelper
- java.lang.Object
-
- com.helger.commons.lang.ClassHelper
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanareConvertibleClasses(Class<?> aSrcClass, Class<?> aDstClass)Check if the passed classes are convertible.static ICommonsSet<Class<?>>getAllPrimitiveClasses()static ICommonsSet<Class<?>>getAllPrimitiveWrapperClasses()static Class<?>getClass(Object aObject)null-safe helper method to determine the class of an object.static StringgetClassFromPath(String sPath)Get the class name of the passed path.static StringgetClassLocalName(Class<?> aClass)Get the name of the class without the package.static StringgetClassLocalName(Object aObject)Get the name of the object's class without the package.static StringgetClassLocalName(String sClassName)Get the name of the class without the package.static StringgetClassName(Object aObject)null-safe helper method to determine the class name of an object.static StringgetClassPackageName(Class<?> aClass)Get the name of the package the passed class resides in.static StringgetClassPackageName(Object aObject)Get the name of the package the passed object resides in.static StringgetClassPackageName(String sClassName)Get the name of the package the passed class resides in.static StringgetDirectoryFromPackage(Package aPackage)Convert a package name to a relative directory name.static StringgetDirectoryFromPackage(String sPackage)Convert a package name to a relative directory name.static StringgetObjectAddress(Object aObject)Get the hex representation of the passed object's address.static StringgetPathFromClass(Class<?> aClass)Get the path representation of the passed class.static StringgetPathFromClass(String sClassName)Get the path representation of the passed class name.static Class<?>getPrimitiveClass(Class<?> aClass)Get the primitive class of the passed primitive wrapper class.static Class<?>getPrimitiveWrapperClass(Class<?> aClass)Get the primitive wrapper class of the passed primitive class.static URLgetResource(Class<?> aClass, String sPath)Get the URL of the passed resource using the class loader of the specified class only.static InputStreamgetResourceAsStream(Class<?> aClass, String sPath)Get the input stream of the passed resource using the class loader of the specified class only.static StringgetSafeClassName(Object aObject)Get the class name of the passed object.static booleanisAbstractClass(Class<?> aClass)Check if the passed class is abstract or not.static booleanisAnnotationClass(Class<?> aClass)static booleanisArrayClass(Class<?> aClass)static booleanisBooleanClass(Class<?> aClass)static booleanisCharacterClass(Class<?> aClass)static booleanisEnumClass(Class<?> aClass)static booleanisFloatingPointClass(Class<?> aClass)static booleanisInstancableClass(Class<?> aClass)Check if the passed class is public, instancable and has a no-argument constructor.static booleanisIntegerClass(Class<?> aClass)static booleanisInterface(Class<?> aClass)Check if the passed class is an interface or not.static booleanisPrimitiveType(Class<?> aClass)static booleanisPrimitiveWrapperType(Class<?> aClass)static booleanisPublic(Class<?> aClass)static booleanisPublicClass(Class<?> aClass)static booleanisStringClass(Class<?> aClass)
-
-
-
Method Detail
-
isInstancableClass
public static boolean isInstancableClass(@Nullable Class<?> aClass)
Check if the passed class is public, instancable and has a no-argument constructor.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is public, instancable and has a no-argument constructor that is public.
-
isInterface
public static boolean isInterface(@Nullable Class<?> aClass)
Check if the passed class is an interface or not. Please note that annotations are also interfaces!- Parameters:
aClass- The class to check.- Returns:
trueif the class is an interface (or an annotation)
-
isAbstractClass
public static boolean isAbstractClass(@Nullable Class<?> aClass)
Check if the passed class is abstract or not. Note: interfaces and annotations are also considered as abstract whereas arrays are never abstract.- Parameters:
aClass- The class to check.- Returns:
trueif the passed class is abstract
-
getPrimitiveWrapperClass
@Nullable public static Class<?> getPrimitiveWrapperClass(@Nullable Class<?> aClass)
Get the primitive wrapper class of the passed primitive class.- Parameters:
aClass- The primitive class. May benull.- Returns:
nullif the passed class is not a primitive class.
-
getPrimitiveClass
@Nullable public static Class<?> getPrimitiveClass(@Nullable Class<?> aClass)
Get the primitive class of the passed primitive wrapper class.- Parameters:
aClass- The primitive wrapper class. May benull.- Returns:
nullif the passed class is not a primitive wrapper class.
-
getAllPrimitiveClasses
@Nonnull @ReturnsMutableCopy public static ICommonsSet<Class<?>> getAllPrimitiveClasses()
-
getAllPrimitiveWrapperClasses
@Nonnull @ReturnsMutableCopy public static ICommonsSet<Class<?>> getAllPrimitiveWrapperClasses()
-
areConvertibleClasses
public static boolean areConvertibleClasses(@Nonnull Class<?> aSrcClass, @Nonnull Class<?> aDstClass)
Check if the passed classes are convertible. Includes conversion checks between primitive types and primitive wrapper types.- Parameters:
aSrcClass- First class. May not benull.aDstClass- Second class. May not benull.- Returns:
trueif the classes are directly convertible.
-
getClass
@Nullable public static Class<?> getClass(@Nullable Object aObject)
null-safe helper method to determine the class of an object.- Parameters:
aObject- The object to query. May benull.- Returns:
nullif the passed object isnull.
-
getClassName
@Nullable public static String getClassName(@Nullable Object aObject)
null-safe helper method to determine the class name of an object.- Parameters:
aObject- The object to query. May benull.- Returns:
nullif the passed object isnull.
-
getClassLocalName
@Nullable public static String getClassLocalName(@Nullable Object aObject)
Get the name of the object's class without the package.- Parameters:
aObject- The object to get the information from. May benull.- Returns:
- The local name of the passed object's class.
-
getClassLocalName
@Nullable public static String getClassLocalName(@Nullable Class<?> aClass)
Get the name of the class without the package.- Parameters:
aClass- The class to get the information from. May benull.- Returns:
- The local name of the passed class.
-
getClassLocalName
@Nullable public static String getClassLocalName(@Nullable String sClassName)
Get the name of the class without the package.- Parameters:
sClassName- The fully qualified name of the class. May benull.- Returns:
- The local name of the passed class. Never
null.
-
getClassPackageName
@Nullable public static String getClassPackageName(@Nullable Object aObject)
Get the name of the package the passed object resides in.- Parameters:
aObject- The class to get the information from. May benull.- Returns:
- The package name of the passed object.
-
getClassPackageName
@Nullable public static String getClassPackageName(@Nullable Class<?> aClass)
Get the name of the package the passed class resides in.- Parameters:
aClass- The class to get the information from. May not benull.- Returns:
- The package name of the passed class.
-
getClassPackageName
@Nullable public static String getClassPackageName(@Nullable String sClassName)
Get the name of the package the passed class resides in.- Parameters:
sClassName- The name class to get the information from. May benull.- Returns:
- The package name of the passed class.
-
getSafeClassName
@Nonnull @Nonempty public static String getSafeClassName(@Nullable Object aObject)
Get the class name of the passed object. If the object itself is of typeClass, its name is retrieved, otherObject.getClass()is called.- Parameters:
aObject- The object who's class name is to be retrieved.- Returns:
"null"for anullparameter
-
getDirectoryFromPackage
@Nullable public static String getDirectoryFromPackage(@Nullable Package aPackage)
Convert a package name to a relative directory name.- Parameters:
aPackage- The package to be converted. May benull.- Returns:
- The directory name using forward slashes (/) instead of the dots.
-
getDirectoryFromPackage
@Nullable public static String getDirectoryFromPackage(@Nullable String sPackage)
Convert a package name to a relative directory name.- Parameters:
sPackage- The name of the package to be converted. May benull.- Returns:
- The directory name using forward slashes (/) instead of the dots.
-
getPathFromClass
@Nullable public static String getPathFromClass(@Nullable Class<?> aClass)
Get the path representation of the passed class. The path representation is achieved by replacing all dots (.) with forward slashes (/) in the class name.- Parameters:
aClass- The class of which the path is to be retrieved. May benull.- Returns:
- The path representation. Never
null.
-
getPathFromClass
@Nullable public static String getPathFromClass(@Nullable String sClassName)
Get the path representation of the passed class name. The path representation is achieved by replacing all dots (.) with forward slashes (/) in the class name.- Parameters:
sClassName- The class name of which the path is to be retrieved. May benull.- Returns:
- The path representation
-
getClassFromPath
@Nullable public static String getClassFromPath(@Nullable String sPath)
Get the class name of the passed path. The class name is retrieved by replacing all path separators (\ and /) with dots (.). This method does not handle the file extension, so it's up to the caller to skip of any file extension!- Parameters:
sPath- The path to be converted. May benull.- Returns:
- The class name.
-
getObjectAddress
@Nonnull @Nonempty public static String getObjectAddress(@Nullable Object aObject)
Get the hex representation of the passed object's address. Note that this method makes no differentiation between 32 and 64 bit architectures. The result is always a hexadecimal value preceded by "0x" and followed by exactly 8 characters.- Parameters:
aObject- The object who's address is to be retrieved. May benull.- Returns:
- Depending on the current architecture. Always starting with "0x" and than containing the address.
- See Also:
System.identityHashCode(Object)
-
getResource
@Nullable public static URL getResource(@Nonnull Class<?> aClass, @Nonnull @Nonempty String sPath)
Get the URL of the passed resource using the class loader of the specified class only. This is a sanity wrapper aroundclass.getResource (sPath).- Parameters:
aClass- The class to be used. May not benull.sPath- The path to be resolved. May neither benullnor empty. Internally it is ensured that the provided path does start with a slash.- Returns:
nullif the path could not be resolved using the specified class loader.
-
getResourceAsStream
@Nullable public static InputStream getResourceAsStream(@Nonnull Class<?> aClass, @Nonnull @Nonempty String sPath)
Get the input stream of the passed resource using the class loader of the specified class only. This is a sanity wrapper aroundclass.getResourceAsStream (sPath).- Parameters:
aClass- The class to be used. May not benull.sPath- The path to be resolved. May neither benullnor empty. Internally it is ensured that the provided path does start with a slash.- Returns:
nullif the path could not be resolved using the specified class loader.
-
-