From 1abb0735ae9bc754520222a53aa5dec30f3b762d Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 27 Jul 2015 18:22:34 +0200 Subject: [PATCH] ClassHelper 'hasInterface' now checks parents as well --- .../src/dorkbox/util/ClassHelper.java | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/Dorkbox-Util/src/dorkbox/util/ClassHelper.java b/Dorkbox-Util/src/dorkbox/util/ClassHelper.java index 3fa9393..692b40d 100644 --- a/Dorkbox-Util/src/dorkbox/util/ClassHelper.java +++ b/Dorkbox-Util/src/dorkbox/util/ClassHelper.java @@ -17,7 +17,7 @@ package dorkbox.util; import java.lang.reflect.*; -public +public final class ClassHelper { /** @@ -27,6 +27,7 @@ class ClassHelper { * @param clazz class to get the parameter from * @param genericParameterToGet 0-based index of parameter as class to get */ + @SuppressWarnings({"StatementWithEmptyBody", "UnnecessaryLocalVariable"}) public static Class getGenericParameterAsClassForSuperClass(Class clazz, int genericParameterToGet) { Class classToCheck = clazz; @@ -40,7 +41,7 @@ class ClassHelper { Type[] actualTypeArguments = ((ParameterizedType) superClassGeneric).getActualTypeArguments(); // is it possible? if (actualTypeArguments.length > genericParameterToGet) { - Class rawTypeAsClass = ClassHelper.getRawTypeAsClass(actualTypeArguments[genericParameterToGet]); + Class rawTypeAsClass = getRawTypeAsClass(actualTypeArguments[genericParameterToGet]); return rawTypeAsClass; } else { @@ -53,8 +54,6 @@ class ClassHelper { classToCheck = classToCheck.getSuperclass(); } - classToCheck = clazz; - // NOTHING! now check interfaces! classToCheck = clazz; while (classToCheck != Object.class) { @@ -70,7 +69,6 @@ class ClassHelper { } else { // record the parameters. - } } } @@ -88,6 +86,7 @@ class ClassHelper { /** * Return the class that is this type. */ + @SuppressWarnings("UnnecessaryLocalVariable") public static Class getRawTypeAsClass(Type type) { 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 *

- * 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 boolean hasInterface(Class clazzItMustHave, Class clazz) { @@ -148,7 +147,25 @@ class ClassHelper { } // now walk up to see if we can find it. 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. @@ -157,10 +174,8 @@ class ClassHelper { /** * Checks to see if the clazz is a subclass of a parent class. - * - * @param baseClass - * @param genericClass */ + @SuppressWarnings("SimplifiableIfStatement") public static boolean hasParentClass(Class parentClazz, Class clazz) { Class superClass = clazz.getSuperclass(); @@ -174,4 +189,8 @@ class ClassHelper { return false; } + + private + ClassHelper() { + } }