Package com.helger.base.lang.clazz
Class ClassHelper
java.lang.Object
com.helger.base.lang.clazz.ClassHelper
Helper class for common reflection and class-related operations such as primitive/wrapper type
mapping, class name handling, and class hierarchy inspection.
- Author:
- Philip Helger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareConvertibleClasses(@NonNull Class<?> aSrcClass, @NonNull Class<?> aDstClass) Check if the passed classes are convertible.static @Nullable Class<?> null-safe helper method to determine the class of an object.static @Nullable StringgetClassFromPath(@Nullable String sPath) Get the class name of the passed path.static @Nullable StringgetClassLocalName(@Nullable Class<?> aClass) Get the name of the class without the package.static @Nullable StringgetClassLocalName(@Nullable Object aObject) Get the name of the object's class without the package.static @Nullable StringgetClassLocalName(@Nullable String sClassName) Get the name of the class without the package.static @Nullable StringgetClassName(@Nullable Object aObject) null-safe helper method to determine the class name of an object.static @Nullable StringgetClassPackageName(@Nullable Class<?> aClass) Get the name of the package the passed class resides in.static @Nullable StringgetClassPackageName(@Nullable Object aObject) Get the name of the package the passed object resides in.static @Nullable StringgetClassPackageName(@Nullable String sClassName) Get the name of the package the passed class resides in.static @Nullable StringgetDirectoryFromPackage(@Nullable Package aPackage) Convert a package name to a relative directory name.static @Nullable StringgetDirectoryFromPackage(@Nullable String sPackage) Convert a package name to a relative directory name.static @Nullable StringgetPathFromClass(@Nullable Class<?> aClass) Get the path representation of the passed class.static @Nullable StringgetPathFromClass(@Nullable String sClassName) Get the path representation of the passed class name.static @Nullable Class<?> getPrimitiveClass(@Nullable Class<?> aClass) Get the primitive class of the passed primitive wrapper class.static @Nullable Class<?> getPrimitiveWrapperClass(@Nullable Class<?> aClass) Get the primitive wrapper class of the passed primitive class.static @Nullable URLgetResource(@NonNull Class<?> aClass, @NonNull @Nonempty String sPath) Get the URL of the passed resource using the class loader of the specified class only.static @NonNull @Nonempty StringgetSafeClassName(@Nullable Object aObject) Get the class name of the passed object.protected static @NonNull StringinternalGetPathWithLeadingSlash(@NonNull @Nonempty String sPath) static booleanisAbstractClass(@Nullable Class<?> aClass) Check if the passed class is abstract or not.static booleanisAnnotationClass(@Nullable Class<?> aClass) Check if the passed class is an annotation type.static booleanisArrayClass(@Nullable Class<?> aClass) Check if the passed class is an array type.static booleanisBooleanClass(@Nullable Class<?> aClass) Check if the passed class is a boolean class, either the primitivebooleanor the wrapper typeBoolean.static booleanisCharacterClass(@Nullable Class<?> aClass) Check if the passed class is a character class, either the primitivecharor the wrapper typeCharacter.static booleanisEnumClass(@Nullable Class<?> aClass) Check if the passed class is an enum type.static booleanisFloatingPointClass(@Nullable Class<?> aClass) Check if the passed class is a floating point class, including the primitive typesdoubleandfloat, their wrapper typesDoubleandFloat, as well asBigDecimal.static booleanisInstancableClass(@Nullable Class<?> aClass) Check if the passed class is public, instancable and has a no-argument constructor.static booleanisIntegerClass(@Nullable Class<?> aClass) Check if the passed class is an integer (whole number) class, including the primitive typesbyte,int,longandshort, their wrapper typesByte,Integer,LongandShort, as well asBigInteger.static booleanisInterface(@Nullable Class<?> aClass) Check if the passed class is an interface or not.static booleanisPrimitiveType(@Nullable Class<?> aClass) Check if the passed class is a primitive type (boolean, byte, char, double, float, int, long or short).static booleanisPrimitiveWrapperType(@Nullable Class<?> aClass) Check if the passed class is a primitive wrapper type (Boolean, Byte, Character, Double, Float, Integer, Long or Short).static booleanCheck if the passed class has the public modifier.static booleanisPublicClass(@Nullable Class<?> aClass) Check if the passed class is a public, non-abstract, non-interface, non-annotation class.static booleanisStringClass(@Nullable Class<?> aClass) Check if the passed class is a string-like class, meaning it is assignable toCharSequence(includes String, StringBuffer and StringBuilder).
-
Constructor Details
-
ClassHelper
protected ClassHelper()
-
-
Method Details
-
isPublicClass
Check if the passed class is a public, non-abstract, non-interface, non-annotation class.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is public, non-abstract, non-interface and non-annotation,falseif not or if the parameter isnull.
-
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
Check if the passed class has the public modifier.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is public,falseif not or if the parameter isnull.
-
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
Check if the passed class is an annotation type.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is an annotation type,falseif not or if the parameter isnull.
-
isEnumClass
Check if the passed class is an enum type.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is an enum type,falseif not or if the parameter isnull.
-
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
Check if the passed class is an array type.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is an array type,falseif not or if the parameter isnull.
-
isPrimitiveType
Check if the passed class is a primitive type (boolean, byte, char, double, float, int, long or short).- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is a primitive type,falseif not or if the parameter isnull.
-
isPrimitiveWrapperType
Check if the passed class is a primitive wrapper type (Boolean, Byte, Character, Double, Float, Integer, Long or Short).- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is a primitive wrapper type,falseif not or if the parameter isnull.
-
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
- Returns:
- A mutable copy of all known primitive classes. Never
null.
-
getAllPrimitiveWrapperClasses
- Returns:
- A mutable copy of all known primitive wrapper classes. Never
null.
-
isStringClass
Check if the passed class is a string-like class, meaning it is assignable toCharSequence(includes String, StringBuffer and StringBuilder).- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is assignable toCharSequence,falseif not or if the parameter isnull.
-
isCharacterClass
Check if the passed class is a character class, either the primitivecharor the wrapper typeCharacter.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is a character type,falseif not or if the parameter isnull.
-
isBooleanClass
Check if the passed class is a boolean class, either the primitivebooleanor the wrapper typeBoolean.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is a boolean type,falseif not or if the parameter isnull.
-
isFloatingPointClass
Check if the passed class is a floating point class, including the primitive typesdoubleandfloat, their wrapper typesDoubleandFloat, as well asBigDecimal.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is a floating point type,falseif not or if the parameter isnull.
-
isIntegerClass
Check if the passed class is an integer (whole number) class, including the primitive typesbyte,int,longandshort, their wrapper typesByte,Integer,LongandShort, as well asBigInteger.- Parameters:
aClass- The class to check. May benull.- Returns:
trueif the class is an integer type,falseif not or if the parameter isnull.
-
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.
-
internalGetPathWithLeadingSlash
-
getResource
public static @Nullable URL getResource(@NonNull Class<?> aClass, @Nonempty @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.
-