Module org.jcommon

Class ClassUtil

    • Constructor Detail

      • ClassUtil

        public ClassUtil()
    • Method Detail

      • isImplements

        public static boolean isImplements​(Class<?> theClass,
                                           Class<?> theInterface)
        Check if the given class implements the given interface. The interface implementation can come for the class hierarchy. This method return false if the parameter theInterface is a Java Class (ie if theInterface.isInterface() return false).

        This method delegate computation to distanceToInterface(Class, Class) and return as result distanceToInterface(theClass, theInterface) > -1
        Parameters:
        theClass - the class to check.
        theInterface - the interface.
        Returns:
        true if the given class implements the interface anf false otherwise.
        See Also:
        isSubClass(Class, Class), isSubEntity(Class, Class)
      • isSubClass

        public static boolean isSubClass​(Class<?> theClass,
                                         Class<?> theAncestor)
        Check if the class denoted theClass is a sub class of the class denoted by theAncestor. The class relation can come for the class hierarchy. This method return false if the parameter theAncestor is a Java Interface (ie if theInterface.isInterface() return true)

        This method delegate computation to distanceToClass(Class, Class) and return as result distanceToClass(theClass, theAncestor) > -1
        Parameters:
        theClass - the class to check.
        theAncestor - the potential ancestor.
        Returns:
        true if the given class implements the interface anf false otherwise.
        See Also:
        isImplements(Class, Class), isSubEntity(Class, Class), distanceToClass(Class, Class)
      • isSubEntity

        public static boolean isSubEntity​(Class<?> entity,
                                          Class<?> ancestor)
        Check if the given entity is a sub entity of ancestor. This method is a convenience method that can handle both classes and interfaces. If the given ancestor is an interface, this method delegates to isImplements(Class, Class) method the computation. On the other hand, if the given ancestor is a class, this method delegates to isSubClass(Class, Class) method the computation.
        Parameters:
        entity - the entity to check.
        ancestor - the potential ancestor.
        Returns:
        true if the given entity is a sub class or an implementation of the given ancestor and false otherwise.
        See Also:
        isImplements(Class, Class), isSubClass(Class, Class)
      • distanceToInterface

        public static int distanceToInterface​(Class<?> theClass,
                                              Class<?> theInterface)
        Compute the hierarchical distance between the given class and the given interface. The hierarchical distance is equals to 0 if the two parameters are equals, to 1 if the class directly implements the interface and is incremented by 1 for each ancestor between the class and the interface.
        If the given class do not implements the given interface, the result is -1.
        Parameters:
        theClass - the class to check.
        theInterface - the interface.
        Returns:
        0 if the two parameters are equals, to 1 if the class directly implements the interface and is incremented by 1 for each ancestor between the class and the interface.
        See Also:
        isImplements(Class, Class)
      • distanceToClass

        public static int distanceToClass​(Class<?> theClass,
                                          Class<?> theAncestor)
        Compute the hierarchical distance between the given class and an ancestor class. The hierarchical distance is equals to 0 if the two parameters are equals, to 1 if the class directly extends the ancestor and is incremented by 1 for each ancestor between the class and its ancestor.
        If the given class is not a sub class of the ancestor, the result is -1.
        Parameters:
        theClass - the class to check.
        theAncestor - the ancestor class.
        Returns:
        0 if the two parameters are equals, to 1 if the class directly extends the ancestor and is incremented by 1 for each ancestor between the class and its ancestor.
        See Also:
        isSubClass(Class, Class)
      • distanceToAncestor

        public static int distanceToAncestor​(Class<?> entity,
                                             Class<?> ancestor)
        Compute the hierarchical distance between the given class and an ancestor. The hierarchical distance is equals to 0 if the two parameters are equals, to 1 if the class directly extends or implements the ancestor and is incremented by 1 for each ancestor between the class and its ancestor.
        If the given class is not a sub class nor an implementation of the ancestor, the result is -1.
        Parameters:
        entity - the entity to check.
        ancestor - the ancestor.
        Returns:
        The hierarchical distance is equals to 0 if the two parameters are equals, to 1 if the class directly extends or implements the ancestor and is incremented by 1 for each ancestor between the class and its ancestor.
        If the given class is not a sub class nor an implementation of the ancestor, the result is -1.
        See Also:
        distanceToClass(Class, Class), distanceToInterface(Class, Class)
      • listClasses

        public static List<Class<?>> listClasses​(String pckgname)
                                          throws ClassNotFoundException
        List all the classes in the specified package as determined by the context class loader.
        Parameters:
        pckgname - the package name to search
        Returns:
        a list of classes that exist within that package
        Throws:
        ClassNotFoundException - if something went wrong