ClassHelper 'hasInterface' now checks parents as well
This commit is contained in:
parent
8105582f47
commit
1abb0735ae
|
@ -17,7 +17,7 @@ package dorkbox.util;
|
||||||
|
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
|
|
||||||
public
|
public final
|
||||||
class ClassHelper {
|
class ClassHelper {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,6 +27,7 @@ class ClassHelper {
|
||||||
* @param clazz class to get the parameter from
|
* @param clazz class to get the parameter from
|
||||||
* @param genericParameterToGet 0-based index of parameter as class to get
|
* @param genericParameterToGet 0-based index of parameter as class to get
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({"StatementWithEmptyBody", "UnnecessaryLocalVariable"})
|
||||||
public static
|
public static
|
||||||
Class<?> getGenericParameterAsClassForSuperClass(Class<?> clazz, int genericParameterToGet) {
|
Class<?> getGenericParameterAsClassForSuperClass(Class<?> clazz, int genericParameterToGet) {
|
||||||
Class<?> classToCheck = clazz;
|
Class<?> classToCheck = clazz;
|
||||||
|
@ -40,7 +41,7 @@ class ClassHelper {
|
||||||
Type[] actualTypeArguments = ((ParameterizedType) superClassGeneric).getActualTypeArguments();
|
Type[] actualTypeArguments = ((ParameterizedType) superClassGeneric).getActualTypeArguments();
|
||||||
// is it possible?
|
// is it possible?
|
||||||
if (actualTypeArguments.length > genericParameterToGet) {
|
if (actualTypeArguments.length > genericParameterToGet) {
|
||||||
Class<?> rawTypeAsClass = ClassHelper.getRawTypeAsClass(actualTypeArguments[genericParameterToGet]);
|
Class<?> rawTypeAsClass = getRawTypeAsClass(actualTypeArguments[genericParameterToGet]);
|
||||||
return rawTypeAsClass;
|
return rawTypeAsClass;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -53,8 +54,6 @@ class ClassHelper {
|
||||||
classToCheck = classToCheck.getSuperclass();
|
classToCheck = classToCheck.getSuperclass();
|
||||||
}
|
}
|
||||||
|
|
||||||
classToCheck = clazz;
|
|
||||||
|
|
||||||
// NOTHING! now check interfaces!
|
// NOTHING! now check interfaces!
|
||||||
classToCheck = clazz;
|
classToCheck = clazz;
|
||||||
while (classToCheck != Object.class) {
|
while (classToCheck != Object.class) {
|
||||||
|
@ -70,7 +69,6 @@ class ClassHelper {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// record the parameters.
|
// record the parameters.
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -88,6 +86,7 @@ class ClassHelper {
|
||||||
/**
|
/**
|
||||||
* Return the class that is this type.
|
* Return the class that is this type.
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("UnnecessaryLocalVariable")
|
||||||
public static
|
public static
|
||||||
Class<?> getRawTypeAsClass(Type type) {
|
Class<?> getRawTypeAsClass(Type type) {
|
||||||
if (type instanceof Class) {
|
if (type instanceof Class) {
|
||||||
|
@ -132,7 +131,7 @@ class ClassHelper {
|
||||||
/**
|
/**
|
||||||
* Check to see if clazz or interface directly has one of the interfaces defined by clazzItMustHave
|
* Check to see if clazz or interface directly has one of the interfaces defined by clazzItMustHave
|
||||||
* <p/>
|
* <p/>
|
||||||
* If the class DOES NOT directly have the interface it will fail. the PARENT class is not checked.
|
* If the class DOES NOT directly have the interface it will fail.
|
||||||
*/
|
*/
|
||||||
public static
|
public static
|
||||||
boolean hasInterface(Class<?> clazzItMustHave, Class<?> clazz) {
|
boolean hasInterface(Class<?> clazzItMustHave, Class<?> clazz) {
|
||||||
|
@ -148,7 +147,25 @@ class ClassHelper {
|
||||||
}
|
}
|
||||||
// now walk up to see if we can find it.
|
// now walk up to see if we can find it.
|
||||||
for (Class<?> iface : interfaces) {
|
for (Class<?> iface : interfaces) {
|
||||||
return hasInterface(clazzItMustHave, iface);
|
boolean b = hasInterface(clazzItMustHave, iface);
|
||||||
|
if (b) {
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// nothing, so now we check the PARENT of this class
|
||||||
|
Class<?> superClass = clazz.getSuperclass();
|
||||||
|
|
||||||
|
// case of multiple inheritance, we are trying to get the first available generic info
|
||||||
|
// don't check for Object.class (this is where superclass is null)
|
||||||
|
while (superClass != null && superClass != Object.class) {
|
||||||
|
// check to see if we have what we are looking for on our CURRENT class
|
||||||
|
if (hasInterface(clazzItMustHave, superClass)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// NO MATCH, so walk up.
|
||||||
|
superClass = superClass.getSuperclass();
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we don't find it.
|
// if we don't find it.
|
||||||
|
@ -157,10 +174,8 @@ class ClassHelper {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks to see if the clazz is a subclass of a parent class.
|
* Checks to see if the clazz is a subclass of a parent class.
|
||||||
*
|
|
||||||
* @param baseClass
|
|
||||||
* @param genericClass
|
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("SimplifiableIfStatement")
|
||||||
public static
|
public static
|
||||||
boolean hasParentClass(Class<?> parentClazz, Class<?> clazz) {
|
boolean hasParentClass(Class<?> parentClazz, Class<?> clazz) {
|
||||||
Class<?> superClass = clazz.getSuperclass();
|
Class<?> superClass = clazz.getSuperclass();
|
||||||
|
@ -174,4 +189,8 @@ class ClassHelper {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private
|
||||||
|
ClassHelper() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user