Package org.jorigin.lang
Class ClassUtil
- java.lang.Object
-
- org.jorigin.lang.ClassUtil
-
public class ClassUtil extends java.lang.ObjectThis class contains method for managing Java classes.- Since:
- 1.0.3
- Version:
- "1.0.8" - b201801141330L
- Author:
- Julien Seinturier - COMEX S.A. - contact@jorigin.org - https://github.com/jorigin/jeometry
-
-
Constructor Summary
Constructors Constructor Description ClassUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intdistanceToAncestor(java.lang.Class<?> entity, java.lang.Class<?> ancestor)Compute the hierarchical distance between the given class and an ancestor.static intdistanceToClass(java.lang.Class<?> theClass, java.lang.Class<?> theAncestor)Compute the hierarchical distance between the given class and an ancestor class.static intdistanceToInterface(java.lang.Class<?> theClass, java.lang.Class<?> theInterface)Compute the hierarchical distance between the given class and the given interface.static booleanisImplements(java.lang.Class<?> theClass, java.lang.Class<?> theInterface)Check if the givenclassimplements the giveninterface.static booleanisSubClass(java.lang.Class<?> theClass, java.lang.Class<?> theAncestor)Check if the class denotedtheClassis a sub class of the class denoted bytheAncestor.static booleanisSubEntity(java.lang.Class<?> entity, java.lang.Class<?> ancestor)Check if the givenentityis a sub entity ofancestor.static java.util.List<java.lang.Class<?>>listClasses(java.lang.String pckgname)List all the classes in the specified package as determined by the context class loader.
-
-
-
Method Detail
-
isImplements
public static boolean isImplements(java.lang.Class<?> theClass, java.lang.Class<?> theInterface)Check if the givenclassimplements the giveninterface. The interface implementation can come for theclasshierarchy. This method returnfalseif the parametertheInterfaceis a Java Class (ie iftheInterface.isInterface()returnfalse).
This method delegate computation todistanceToInterface(Class, Class)and return as resultdistanceToInterface(theClass, theInterface) > -1- Parameters:
theClass- the class to check.theInterface- the interface.- Returns:
trueif the given class implements the interface anffalseotherwise.- See Also:
isSubClass(Class, Class),isSubEntity(Class, Class)
-
isSubClass
public static boolean isSubClass(java.lang.Class<?> theClass, java.lang.Class<?> theAncestor)Check if the class denotedtheClassis a sub class of the class denoted bytheAncestor. The class relation can come for theclasshierarchy. This method returnfalseif the parametertheAncestoris a Java Interface (ie iftheInterface.isInterface()returntrue)
This method delegate computation todistanceToClass(Class, Class)and return as resultdistanceToClass(theClass, theAncestor) > -1- Parameters:
theClass- the class to check.theAncestor- the potential ancestor.- Returns:
trueif the given class implements the interface anffalseotherwise.- See Also:
isImplements(Class, Class),isSubEntity(Class, Class),distanceToClass(Class, Class)
-
isSubEntity
public static boolean isSubEntity(java.lang.Class<?> entity, java.lang.Class<?> ancestor)Check if the givenentityis a sub entity ofancestor. This method is a convenience method that can handle both classes and interfaces. If the givenancestoris an interface, this method delegates toisImplements(Class, Class)method the computation. On the other hand, if the givenancestoris a class, this method delegates toisSubClass(Class, Class)method the computation.- Parameters:
entity- the entity to check.ancestor- the potential ancestor.- Returns:
trueif the givenentityis a sub class or an implementation of the givenancestorandfalseotherwise.- See Also:
isImplements(Class, Class),isSubClass(Class, Class)
-
distanceToInterface
public static int distanceToInterface(java.lang.Class<?> theClass, java.lang.Class<?> theInterface)Compute the hierarchical distance between the given class and the given interface. The hierarchical distance is equals to0if the two parameters are equals, to1if the class directly implements the interface and is incremented by1for 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:
0if the two parameters are equals, to1if the class directly implements the interface and is incremented by1for each ancestor between the class and the interface.- See Also:
isImplements(Class, Class)
-
distanceToClass
public static int distanceToClass(java.lang.Class<?> theClass, java.lang.Class<?> theAncestor)Compute the hierarchical distance between the given class and an ancestor class. The hierarchical distance is equals to0if the two parameters are equals, to1if the class directly extends the ancestor and is incremented by1for 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:
0if the two parameters are equals, to1if the class directly extends the ancestor and is incremented by1for each ancestor between the class and its ancestor.- See Also:
isSubClass(Class, Class)
-
distanceToAncestor
public static int distanceToAncestor(java.lang.Class<?> entity, java.lang.Class<?> ancestor)Compute the hierarchical distance between the given class and an ancestor. The hierarchical distance is equals to0if the two parameters are equals, to1if the class directly extends or implements the ancestor and is incremented by1for 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
0if the two parameters are equals, to1if the class directly extends or implements the ancestor and is incremented by1for 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 java.util.List<java.lang.Class<?>> listClasses(java.lang.String pckgname) throws java.lang.ClassNotFoundExceptionList 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:
java.lang.ClassNotFoundException- if something went wrong
-
-