Method that checks for a class matching a predicate in hierarchy
This commit is contained in:
parent
48252c20b5
commit
f3c535c967
2 changed files with 83 additions and 3 deletions
src/main/java/info/ebenoit/ebul/reflection
|
@ -3,6 +3,7 @@ package info.ebenoit.ebul.reflection;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
|
||||
|
||||
|
@ -16,7 +17,7 @@ public final class Classes
|
|||
|
||||
/**
|
||||
* List all classes a class inherits from.
|
||||
*
|
||||
*
|
||||
* @param cls
|
||||
* the class to examine
|
||||
* @return the list of ancestors, including the class itself
|
||||
|
@ -35,7 +36,7 @@ public final class Classes
|
|||
|
||||
/**
|
||||
* Find all types a class is compatible with: itself, its ancestors, and all implemented interfaces.
|
||||
*
|
||||
*
|
||||
* @param cls
|
||||
* the class to examine
|
||||
* @return the set of compatible types
|
||||
|
@ -54,8 +55,43 @@ public final class Classes
|
|||
|
||||
|
||||
/**
|
||||
* Internal method used by {@link #getAllTypes(Class)} to add all interfaces recursively.
|
||||
* Checks for a class (or optionally an interface) matching a predicate in a class hierarchy.
|
||||
*
|
||||
* @param klass
|
||||
* the class to start checking from
|
||||
* @param test
|
||||
* the predicate
|
||||
* @param interfaces
|
||||
* <code>true</code> if interfaces will be tested, <code>false</code> if the test is to be applied to
|
||||
* actual classes only
|
||||
* @return <code>true</code> if any class or interface matches the test, <code>false</code> if none of them do.
|
||||
*/
|
||||
public static boolean hasAncestorMatching( final Class< ? > klass , final Predicate< Class< ? > > test ,
|
||||
final boolean interfaces )
|
||||
{
|
||||
if ( klass == null ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( test.test( klass ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( interfaces ) {
|
||||
for ( final Class< ? > itf : klass.getInterfaces( ) ) {
|
||||
if ( Classes.hasAncestorMatching( itf , test , true ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Classes.hasAncestorMatching( klass.getSuperclass( ) , test , interfaces );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Internal method used by {@link #getAllTypes(Class)} to add all interfaces recursively.
|
||||
*
|
||||
* @param current
|
||||
* the class or interface being examined
|
||||
* @param found
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue