Class ClassHelper

java.lang.Object
com.helger.base.lang.clazz.ClassHelper

@Immutable public class ClassHelper extends Object
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 Details

    • ClassHelper

      protected ClassHelper()
  • Method Details

    • isPublicClass

      public static boolean isPublicClass(@Nullable Class<?> aClass)
      Check if the passed class is a public, non-abstract, non-interface, non-annotation class.
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is public, non-abstract, non-interface and non-annotation, false if not or if the parameter is null.
    • 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 be null.
      Returns:
      true if the class is public, instancable and has a no-argument constructor that is public.
    • isPublic

      public static boolean isPublic(@Nullable Class<?> aClass)
      Check if the passed class has the public modifier.
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is public, false if not or if the parameter is null.
    • 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:
      true if the class is an interface (or an annotation)
    • isAnnotationClass

      public static boolean isAnnotationClass(@Nullable Class<?> aClass)
      Check if the passed class is an annotation type.
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is an annotation type, false if not or if the parameter is null.
    • isEnumClass

      public static boolean isEnumClass(@Nullable Class<?> aClass)
      Check if the passed class is an enum type.
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is an enum type, false if not or if the parameter is null.
    • 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:
      true if the passed class is abstract
    • isArrayClass

      public static boolean isArrayClass(@Nullable Class<?> aClass)
      Check if the passed class is an array type.
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is an array type, false if not or if the parameter is null.
    • isPrimitiveType

      public static boolean isPrimitiveType(@Nullable Class<?> aClass)
      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 be null.
      Returns:
      true if the class is a primitive type, false if not or if the parameter is null.
    • isPrimitiveWrapperType

      public static boolean isPrimitiveWrapperType(@Nullable Class<?> aClass)
      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 be null.
      Returns:
      true if the class is a primitive wrapper type, false if not or if the parameter is null.
    • getPrimitiveWrapperClass

      public static @Nullable Class<?> getPrimitiveWrapperClass(@Nullable Class<?> aClass)
      Get the primitive wrapper class of the passed primitive class.
      Parameters:
      aClass - The primitive class. May be null.
      Returns:
      null if the passed class is not a primitive class.
    • getPrimitiveClass

      public static @Nullable Class<?> getPrimitiveClass(@Nullable Class<?> aClass)
      Get the primitive class of the passed primitive wrapper class.
      Parameters:
      aClass - The primitive wrapper class. May be null.
      Returns:
      null if the passed class is not a primitive wrapper class.
    • getAllPrimitiveClasses

      @ReturnsMutableCopy public static @NonNull Set<Class<?>> getAllPrimitiveClasses()
      Returns:
      A mutable copy of all known primitive classes. Never null.
    • getAllPrimitiveWrapperClasses

      @ReturnsMutableCopy public static @NonNull Set<Class<?>> getAllPrimitiveWrapperClasses()
      Returns:
      A mutable copy of all known primitive wrapper classes. Never null.
    • isStringClass

      public static boolean isStringClass(@Nullable Class<?> aClass)
      Check if the passed class is a string-like class, meaning it is assignable to CharSequence (includes String, StringBuffer and StringBuilder).
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is assignable to CharSequence, false if not or if the parameter is null.
    • isCharacterClass

      public static boolean isCharacterClass(@Nullable Class<?> aClass)
      Check if the passed class is a character class, either the primitive char or the wrapper type Character.
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is a character type, false if not or if the parameter is null.
    • isBooleanClass

      public static boolean isBooleanClass(@Nullable Class<?> aClass)
      Check if the passed class is a boolean class, either the primitive boolean or the wrapper type Boolean.
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is a boolean type, false if not or if the parameter is null.
    • isFloatingPointClass

      public static boolean isFloatingPointClass(@Nullable Class<?> aClass)
      Check if the passed class is a floating point class, including the primitive types double and float, their wrapper types Double and Float, as well as BigDecimal.
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is a floating point type, false if not or if the parameter is null.
    • isIntegerClass

      public static boolean isIntegerClass(@Nullable Class<?> aClass)
      Check if the passed class is an integer (whole number) class, including the primitive types byte, int, long and short, their wrapper types Byte, Integer, Long and Short, as well as BigInteger.
      Parameters:
      aClass - The class to check. May be null.
      Returns:
      true if the class is an integer type, false if not or if the parameter is null.
    • 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 be null.
      aDstClass - Second class. May not be null.
      Returns:
      true if the classes are directly convertible.
    • getClass

      public static @Nullable Class<?> getClass(@Nullable Object aObject)
      null-safe helper method to determine the class of an object.
      Parameters:
      aObject - The object to query. May be null.
      Returns:
      null if the passed object is null.
    • getClassName

      public static @Nullable String getClassName(@Nullable Object aObject)
      null-safe helper method to determine the class name of an object.
      Parameters:
      aObject - The object to query. May be null.
      Returns:
      null if the passed object is null.
    • getClassLocalName

      public static @Nullable 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 be null .
      Returns:
      The local name of the passed object's class.
    • getClassLocalName

      public static @Nullable String getClassLocalName(@Nullable Class<?> aClass)
      Get the name of the class without the package.
      Parameters:
      aClass - The class to get the information from. May be null.
      Returns:
      The local name of the passed class.
    • getClassLocalName

      public static @Nullable String getClassLocalName(@Nullable String sClassName)
      Get the name of the class without the package.
      Parameters:
      sClassName - The fully qualified name of the class. May be null.
      Returns:
      The local name of the passed class. Never null.
    • getClassPackageName

      public static @Nullable 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 be null.
      Returns:
      The package name of the passed object.
    • getClassPackageName

      public static @Nullable 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 be null.
      Returns:
      The package name of the passed class.
    • getClassPackageName

      public static @Nullable 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 be null .
      Returns:
      The package name of the passed class.
    • getSafeClassName

      @Nonempty public static @NonNull @Nonempty String getSafeClassName(@Nullable Object aObject)
      Get the class name of the passed object. If the object itself is of type Class, its name is retrieved, other Object.getClass() is called.
      Parameters:
      aObject - The object who's class name is to be retrieved.
      Returns:
      "null" for a null parameter
    • getDirectoryFromPackage

      public static @Nullable String getDirectoryFromPackage(@Nullable Package aPackage)
      Convert a package name to a relative directory name.
      Parameters:
      aPackage - The package to be converted. May be null.
      Returns:
      The directory name using forward slashes (/) instead of the dots.
    • getDirectoryFromPackage

      public static @Nullable 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 be null.
      Returns:
      The directory name using forward slashes (/) instead of the dots.
    • getPathFromClass

      public static @Nullable 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 be null.
      Returns:
      The path representation. Never null.
    • getPathFromClass

      public static @Nullable 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 be null.
      Returns:
      The path representation
    • getClassFromPath

      public static @Nullable 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 be null.
      Returns:
      The class name.
    • internalGetPathWithLeadingSlash

      protected static @NonNull String internalGetPathWithLeadingSlash(@Nonempty @NonNull @Nonempty String sPath)
    • 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 around class.getResource (sPath).
      Parameters:
      aClass - The class to be used. May not be null.
      sPath - The path to be resolved. May neither be null nor empty. Internally it is ensured that the provided path does start with a slash.
      Returns:
      null if the path could not be resolved using the specified class loader.