Class ClassUtil


  • public class ClassUtil
    extends Object
    Collection of helper functions for working with classes.
    • Constructor Detail

      • ClassUtil

        public ClassUtil()
    • Method Detail

      • getClass

        public static Class<?> getClass​(Object obj)
        Gets the runtime class of a given object.
        Parameters:
        obj - The given object
        Returns:
        The object's runtime class
      • getClass

        public static Class<?> getClass​(String className)
        Gets the class reference for a given qualified class name.
        Parameters:
        className - The given class name
        Returns:
        Class reference
      • getClassName

        public static String getClassName​(Object obj)
        Gets the non-qualified (without package name prefix) class name for a given object.
        Parameters:
        obj - The given object
        Returns:
        The object's class name (w/o package name prefix)
      • getClassName

        public static String getClassName​(Class<?> classRef)
        Gets the non-qualified (w/o package name prefix) class name of a given class reference.
        Parameters:
        classRef - The given class reference
        Returns:
        The name of the class (w/o package name prefix)
      • getQualifiedClassName

        public static String getQualifiedClassName​(Object obj)
        Gets the qualified class name of a given object.
        Parameters:
        obj - The given object
        Returns:
        Qualified class name
      • getQualifiedClassName

        public static String getQualifiedClassName​(Class<?> classRef)
        Gets the qualified class name of a given class reference.
        Parameters:
        classRef - The given class reference
        Returns:
        Qualified class name
      • getShortClassName

        public static String getShortClassName​(Class<?> classRef)
        Gets the non-qualified class name (w/o package name prefix) of a given class reference.
        Parameters:
        classRef - The given class reference
        Returns:
        Non-qualified class name
      • getShortClassName

        public static String getShortClassName​(Object obj)
        Gets the non-qualified class name (w/o package name prefix) of a given object.
        Parameters:
        obj - The given object
        Returns:
        Non-qualified class name
      • getPackageName

        public static String getPackageName​(Object obj)
        Gets the name of the package containing a given object's class.
        Parameters:
        obj - The given object
        Returns:
        Package name of the given object's class (w/o the trailing dot), or an empty string
      • getPackageName

        public static String getPackageName​(Class<?> classRef)
        Gets the package name of a given class reference.
        Parameters:
        classRef - The given class reference
        Returns:
        Package name of the given class reference (w/o the trailing dot), or an empty string
      • extractPackageName

        public static String extractPackageName​(String className)
        Extracts the package name part of a qualified class name.
        Parameters:
        className - Qualified class name
        Returns:
        Package name (w/o the trailing dot)
      • extractShortClassName

        public static String extractShortClassName​(String className)
        Extracts the class name part of a qualified class name.
        Parameters:
        className - Qualified class name
        Returns:
        Class name
      • qualifyName

        public static String qualifyName​(String packageName,
                                         String shortClassName)
        Builds a qualified class name from given parts.
        Parameters:
        packageName - Package name
        shortClassName - Class name
        Returns:
        Qualified class name
      • qualifyName

        public static String qualifyName​(Class<?> siblingClassRef,
                                         String shortClassName)
        Builds a qualified class name for the given class name. Package name is determined from a reference to another class in the same package.
        Parameters:
        siblingClassRef - Reference to another class in the same package
        shortClassName - Non-qualified name of the class to get a qualified name for
        Returns:
        Qualified class name
      • qualifyName

        public static String qualifyName​(Object sibling,
                                         String shortClassName)
        Builds a qualified class name for the given class name. Package name is determined from an instance of another class in the same package.
        Parameters:
        sibling - Existing object, an instance of another class in the same package
        shortClassName - Non-qualified name of the class to get a qualified name for
        Returns:
        Qualified class name
      • instantiateClass

        public static <T> T instantiateClass​(Class<T> classRef)
                                      throws InstantiationException,
                                             IllegalAccessException
        Creates a new instance of a given class.
        Type Parameters:
        T - the type of the given class
        Parameters:
        classRef - The given class
        Returns:
        a newly created instance of the given class
        Throws:
        InstantiationException - when we fail to create an instance.
        IllegalAccessException - when trying to create an instance but don't have the right.
      • isInJar

        public static boolean isInJar​(Class<?> cls,
                                      String resPath)
        Detect if a given class is located in a JAR.
        Parameters:
        cls - the given class.
        resPath - resource path inside the jar
        Returns:
        true if the class is located in a JAR.
      • buildClassPath

        public static String buildClassPath​(String... paths)
      • getPath

        public static String getPath​(Class<?> cls)
      • getClassFilePath

        public static String getClassFilePath​(Class<?> cls)
      • buildClassPath

        public static String buildClassPath​(Class<?>... classes)