commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1510298 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/ClassUtils.java test/java/org/apache/commons/lang3/ClassUtilsTest.java
Date Sun, 04 Aug 2013 18:36:17 GMT
Author: mbenson
Date: Sun Aug  4 18:36:17 2013
New Revision: 1510298

URL: http://svn.apache.org/r1510298
Log:
[LANG-907] retrieve class hierarchy

Modified:
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java?rev=1510298&r1=1510297&r2=1510298&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java Sun Aug
 4 18:36:17 2013
@@ -19,12 +19,16 @@ package org.apache.commons.lang3;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
+import org.apache.commons.lang3.mutable.MutableObject;
 
 /**
  * <p>Operates on classes without using reflection.</p>
@@ -41,6 +45,12 @@ import java.util.Map;
  * @version $Id$
  */
 public class ClassUtils {
+    /**
+     * @see ClassUtils#hierarchy(Class, Interfaces)
+     */
+    public enum Interfaces {
+        INCLUDE, EXCLUDE;
+    }
 
     /**
      * The package separator character: <code>'&#x2e;' == {@value}</code>.
@@ -1129,4 +1139,102 @@ public class ClassUtils {
         }
     }
 
-}
+    /**
+     * Get an {@link Iterable} that can iterate over a class hierarchy in ascending (subclass
to superclass) order,
+     * excluding interfaces.
+     *
+     * @param type
+     * @return Iterable
+     */
+    public static Iterable<Class<?>> hierarchy(final Class<?> type) {
+        return hierarchy(type, Interfaces.EXCLUDE);
+    }
+
+    /**
+     * Get an {@link Iterable} that can iterate over a class hierarchy in ascending (subclass
to superclass) order.
+     *
+     * @param type
+     * @param interfacesBehavior
+     * @return Iterable
+     */
+    public static Iterable<Class<?>> hierarchy(final Class<?> type, Interfaces
interfacesBehavior) {
+        final Iterable<Class<?>> classes = new Iterable<Class<?>>()
{
+    
+            @Override
+            public Iterator<Class<?>> iterator() {
+                final MutableObject<Class<?>> next = new MutableObject<Class<?>>(type);
+                return new Iterator<Class<?>>() {
+    
+                    @Override
+                    public boolean hasNext() {
+                        return next.getValue() != null;
+                    }
+    
+                    @Override
+                    public Class<?> next() {
+                        final Class<?> result = next.getValue();
+                        next.setValue(result.getSuperclass());
+                        return result;
+                    }
+    
+                    @Override
+                    public void remove() {
+                        throw new UnsupportedOperationException();
+                    }
+    
+                };
+            }
+    
+        };
+        if (interfacesBehavior != Interfaces.INCLUDE) {
+            return classes;
+        }
+        return new Iterable<Class<?>>() {
+    
+            @Override
+            public Iterator<Class<?>> iterator() {
+                final Set<Class<?>> seenInterfaces = new HashSet<Class<?>>();
+                final Iterator<Class<?>> wrapped = classes.iterator();
+    
+                return new Iterator<Class<?>>() {
+                    Iterator<Class<?>> interfaces = Collections.<Class<?>>
emptySet().iterator();
+    
+                    @Override
+                    public boolean hasNext() {
+                        return interfaces.hasNext() || wrapped.hasNext();
+                    }
+    
+                    @Override
+                    public Class<?> next() {
+                        if (interfaces.hasNext()) {
+                            final Class<?> nextInterface = interfaces.next();
+                            seenInterfaces.add(nextInterface);
+                            return nextInterface;
+                        }
+                        final Class<?> nextSuperclass = wrapped.next();
+                        final Set<Class<?>> currentInterfaces = new LinkedHashSet<Class<?>>();
+                        walkInterfaces(currentInterfaces, nextSuperclass);
+                        interfaces = currentInterfaces.iterator();
+                        return nextSuperclass;
+                    }
+    
+                    private void walkInterfaces(Set<Class<?>> addTo, Class<?>
c) {
+                        for (Class<?> iface : c.getInterfaces()) {
+                            if (!seenInterfaces.contains(iface)) {
+                                addTo.add(iface);
+                            }
+                            walkInterfaces(addTo, iface);
+                        }
+                    }
+    
+                    @Override
+                    public void remove() {
+                        throw new UnsupportedOperationException();
+                    }
+    
+                };
+            }
+        };
+    }
+
+}
\ No newline at end of file

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java?rev=1510298&r1=1510297&r2=1510298&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java Sun
Aug  4 18:36:17 2013
@@ -34,10 +34,15 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.commons.lang3.ClassUtils.Interfaces;
+import org.apache.commons.lang3.reflect.testbed.GenericConsumer;
+import org.apache.commons.lang3.reflect.testbed.GenericParent;
+import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild;
 import org.junit.Test;
 
 /**
@@ -1221,4 +1226,23 @@ public class ClassUtilsTest  {
         assertEquals("org.apache.commons.lang3", ClassUtils.getPackageCanonicalName("org.apache.commons.lang3.ClassUtilsTest$Inner"));
     }
 
+    @Test
+    public void testHierarchyIncludingInterfaces() {
+        final Iterator<Class<?>> iter =
+            ClassUtils.hierarchy(StringParameterizedChild.class, Interfaces.INCLUDE).iterator();
+        assertEquals(StringParameterizedChild.class, iter.next());
+        assertEquals(GenericParent.class, iter.next());
+        assertEquals(GenericConsumer.class, iter.next());
+        assertEquals(Object.class, iter.next());
+        assertFalse(iter.hasNext());
+    }
+
+    @Test
+    public void testHierarchyExcludingInterfaces() {
+        final Iterator<Class<?>> iter = ClassUtils.hierarchy(StringParameterizedChild.class).iterator();
+        assertEquals(StringParameterizedChild.class, iter.next());
+        assertEquals(GenericParent.class, iter.next());
+        assertEquals(Object.class, iter.next());
+        assertFalse(iter.hasNext());
+    }
 }



Mime
View raw message