Package com.helger.commons.lang
Class ClassHelper
java.lang.Object
com.helger.commons.lang.ClassHelper
Class helper methods.- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareConvertibleClasses(Class<?> aSrcClass, Class<?> aDstClass) Check if the passed classes are convertible.static ICommonsSet<Class<?>> static ICommonsSet<Class<?>> static Class<?> 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 booleanstatic booleanisPublicClass(Class<?> aClass) static booleanisStringClass(Class<?> aClass)
-
Method Details
-
isPublicClass
-
isInstancableClass
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.
-
isPublic
-
isInterface
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)
-
isAnnotationClass
-
isEnumClass
-
isAbstractClass
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
-
isArrayClass
-
isPrimitiveType
-
isPrimitiveWrapperType
-
getPrimitiveWrapperClass
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
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
-
getAllPrimitiveWrapperClasses
-
isStringClass
-
isCharacterClass
-
isBooleanClass
-
isFloatingPointClass
-
isIntegerClass
-
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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:
-
getResource
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.
-