commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adri...@apache.org
Subject svn commit: r931702 - /commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/Util.java
Date Wed, 07 Apr 2010 22:00:21 GMT
Author: adrianc
Date: Wed Apr  7 22:00:21 2010
New Revision: 931702

URL: http://svn.apache.org/viewvc?rev=931702&view=rev
Log:
Improved Util.instanceOf method.

Modified:
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/Util.java

Modified: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/Util.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/Util.java?rev=931702&r1=931701&r2=931702&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/Util.java (original)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/Util.java Wed Apr
 7 22:00:21 2010
@@ -39,26 +39,50 @@ public class Util {
      * @return true if objectClass is a class or sub-class of, or implements typeClass
      */
     public static boolean instanceOf(Class<?> objectClass, Class<?> typeClass)
{
-        if (typeClass.isInterface() && !objectClass.isInterface()) {
-            while (objectClass != null) {
+        if (objectClass == typeClass) {
+            return true;
+        }
+        if (objectClass.isInterface()) {
+            if (typeClass.isInterface()) {
+                // objectClass == interface, typeClass == interface
                 Class<?>[] ifaces = objectClass.getInterfaces();
                 for (Class<?> iface: ifaces) {
                     if (iface == typeClass) {
                         return true;
                     }
                 }
-                objectClass = objectClass.getSuperclass();
+            } else {
+                // objectClass == interface, typeClass != interface
+                Class<?>[] ifaces = typeClass.getInterfaces();
+                for (Class<?> iface: ifaces) {
+                    if (iface == objectClass) {
+                        return true;
+                    }
+                }
             }
-            return false;
         } else {
-            while (objectClass != null) {
-                if (objectClass == typeClass) {
-                    return true;
+            if (typeClass.isInterface()) {
+                // objectClass != interface, typeClass == interface
+                while (objectClass != null) {
+                    Class<?>[] ifaces = objectClass.getInterfaces();
+                    for (Class<?> iface: ifaces) {
+                        if (iface == typeClass) {
+                            return true;
+                        }
+                    }
+                    objectClass = objectClass.getSuperclass();
+                }
+            } else {
+                // objectClass != interface, typeClass != interface
+                while (objectClass != null) {
+                    if (objectClass == typeClass) {
+                        return true;
+                    }
+                    objectClass = objectClass.getSuperclass();
                 }
-                objectClass = objectClass.getSuperclass();
             }
-            return false;
         }
+        return false;
     }
 
     /** Returns <code>true</code> if <code>str</code> is <code>null</code>



Mime
View raw message