You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/08/09 04:44:56 UTC

svn commit: r802477 - /commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java

Author: bayard
Date: Sun Aug  9 02:44:56 2009
New Revision: 802477

URL: http://svn.apache.org/viewvc?rev=802477&view=rev
Log:
Improving performance of getAllInterfaces per LANG-500's patch from Pino Silvaggio

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java?rev=802477&r1=802476&r2=802477&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java Sun Aug  9 02:44:56 2009
@@ -20,6 +20,8 @@
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 
@@ -284,24 +286,26 @@
         if (cls == null) {
             return null;
         }
-        List<Class<?>> list = new ArrayList<Class<?>>();
+
+        LinkedHashSet<Class<?>> interfacesFound = new LinkedHashSet<Class<?>>();
+        getAllInterfaces(cls, interfacesFound);
+
+        return new ArrayList<Class<?>>(interfacesFound);
+    }
+
+    private static void getAllInterfaces(Class<?> cls, HashSet<Class<?>> interfacesFound) {
         while (cls != null) {
             Class<?>[] interfaces = cls.getInterfaces();
-            for (Class<?> intface : interfaces) {
-                if (list.contains(intface) == false) {
-                    list.add(intface);
-                }
-                List<Class<?>> superInterfaces = getAllInterfaces(intface);
-                for (Class<?> superInterface : superInterfaces) {
-                    if (list.contains(superInterface) == false) {
-                        list.add(superInterface);
-                    }
+
+            for (Class<?> i : interfaces) {
+                if (interfacesFound.add(i)) {
+                    getAllInterfaces(i, interfacesFound);
                 }
             }
+
             cls = cls.getSuperclass();
-        }
-        return list;
-    }
+         }
+     }
 
     // Convert list
     // ----------------------------------------------------------------------