Class ClassUtil

java.lang.Object
org.jorigin.lang.ClassUtil

public class ClassUtil extends Object
This class contains method for managing Java classes.
Since:
1.0.3
Version:
"1.0.14" - b202111241200L
Author:
Julien SEINTURIER - Université de Toulon / CNRS LIS umr 7020 - github.com/jorigin/jcommon (contact@jorigin.org)
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    distanceToAncestor(Class<?> entity, Class<?> ancestor)
    Compute the hierarchical distance between the given class and an ancestor.
    static int
    distanceToClass(Class<?> theClass, Class<?> theAncestor)
    Compute the hierarchical distance between the given class and an ancestor class.
    static int
    distanceToInterface(Class<?> theClass, Class<?> theInterface)
    Compute the hierarchical distance between the given class and the given interface.
    static boolean
    isImplements(Class<?> theClass, Class<?> theInterface)
    Check if the given class implements the given interface.
    static boolean
    isSubClass(Class<?> theClass, Class<?> theAncestor)
    Check if the class denoted theClass is a sub class of the class denoted by theAncestor.
    static boolean
    isSubEntity(Class<?> entity, Class<?> ancestor)
    Check if the given entity is a sub entity of ancestor.
    static List<Class<?>>
    listClasses(String pckgname)
    List all the classes in the specified package as determined by the context class loader.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ClassUtil

      public ClassUtil()
  • Method Details

    • 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

      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:
    • 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:
    • 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:
    • 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:
    • 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:
    • 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