Class ClassHelper


  • @Immutable
    public final class ClassHelper
    extends Object
    Class helper methods.
    Author:
    Philip Helger
    • Method Detail

      • isPublicClass

        public static boolean isPublicClass​(@Nullable
                                            Class<?> aClass)
      • 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)
      • 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)
      • isEnumClass

        public static boolean isEnumClass​(@Nullable
                                          Class<?> aClass)
      • 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)
      • isPrimitiveType

        public static boolean isPrimitiveType​(@Nullable
                                              Class<?> aClass)
      • isPrimitiveWrapperType

        public static boolean isPrimitiveWrapperType​(@Nullable
                                                     Class<?> aClass)
      • 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 be null.
        Returns:
        null if 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 be null.
        Returns:
        null if the passed class is not a primitive wrapper class.
      • isStringClass

        public static boolean isStringClass​(@Nullable
                                            Class<?> aClass)
      • isCharacterClass

        public static boolean isCharacterClass​(@Nullable
                                               Class<?> aClass)
      • isBooleanClass

        public static boolean isBooleanClass​(@Nullable
                                             Class<?> aClass)
      • isFloatingPointClass

        public static boolean isFloatingPointClass​(@Nullable
                                                   Class<?> aClass)
      • isIntegerClass

        public static boolean isIntegerClass​(@Nullable
                                             Class<?> aClass)
      • 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

        @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 be null.
        Returns:
        null if the passed object is null.
      • 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 be null.
        Returns:
        null if the passed object is null.
      • 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 be null .
        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 be null.
        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 be null.
        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 be null.
        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 be null.
        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 be null .
        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 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

        @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 be null.
        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 be null.
        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 be null.
        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 be null.
        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 be null.
        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 be null.
        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 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.
      • 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 around class.getResourceAsStream (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.