[jboss-svn-commits] JBoss Common SVN: r2654 - in common-core/trunk/src: test/java/org/jboss/test/util/test and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Oct 25 17:00:46 EDT 2007


Author: dimitris at jboss.org
Date: 2007-10-25 17:00:45 -0400 (Thu, 25 Oct 2007)
New Revision: 2654

Added:
   common-core/trunk/src/test/java/org/jboss/test/util/test/ClassesUnitTestCase.java
Modified:
   common-core/trunk/src/main/java/org/jboss/util/Classes.java
Log:
JBCOMMON-36, add a Classes.getAllUniqueInterfaces utility method.

Modified: common-core/trunk/src/main/java/org/jboss/util/Classes.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/Classes.java	2007-10-25 20:28:02 UTC (rev 2653)
+++ common-core/trunk/src/main/java/org/jboss/util/Classes.java	2007-10-25 21:00:45 UTC (rev 2654)
@@ -38,7 +38,8 @@
  *
  * @version <tt>$Revision$</tt>
  * @author  <a href="mailto:jason at planet57.com">Jason Dillon</a>
- * @author Scott.Stark at jboss.org
+ * @author  <a href="mailto:scott.stark at jboss.org">Scott Stark</a>
+ * @author  <a href="mailto:dimitris at jboss.org">Dimitris Andreadis<a/>
  */
 public final class Classes
 {
@@ -401,18 +402,18 @@
    }
 
    /**
-    * Object a list of all interfaces starting with the argument class c through
-    * all superclasses
+    * Populates a list with all the interfaces implemented by the argument
+    * class c and all its superclasses.
     * 
-    * @param allIfaces - the list to populate for 
+    * @param allIfaces - the list to populate with the interfaces 
     * @param c - the class to start scanning for interfaces
     */
    public static void getAllInterfaces(List allIfaces, Class c)
    {
-      while( c != null )
+      while (c != null)
       {
          Class[] ifaces = c.getInterfaces();
-         for(int n = 0; n < ifaces.length; n ++)
+         for (int n = 0; n < ifaces.length; n ++)
          {
             allIfaces.add(ifaces[n]);
          }
@@ -421,6 +422,28 @@
    }
 
    /**
+    * Returns an array containing all the unique interfaces implemented
+    * by the argument class c and all its superclasses. Interfaces that
+    * appear multiple times through inheritence are only accounted for once.
+    * 
+    * @param c - the class to start scanning for interfaces
+    */   
+   public static Class[] getAllUniqueInterfaces(Class c)
+   {
+      Set uniqueIfaces = new HashSet();
+      while (c != null )
+      {
+         Class[] ifaces = c.getInterfaces();
+         for (int n = 0; n < ifaces.length; n ++)
+         {
+            uniqueIfaces.add(ifaces[n]);
+         }
+         c = c.getSuperclass();
+      }
+      return (Class[])uniqueIfaces.toArray(new Class[uniqueIfaces.size()]);
+   }
+
+   /**
     * Check if the given class is a primitive wrapper class.
     *
     * @param type    Class to check.

Added: common-core/trunk/src/test/java/org/jboss/test/util/test/ClassesUnitTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/ClassesUnitTestCase.java	                        (rev 0)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/ClassesUnitTestCase.java	2007-10-25 21:00:45 UTC (rev 2654)
@@ -0,0 +1,39 @@
+package org.jboss.test.util.test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.jboss.util.Classes;
+
+/**
+ * Unit tests for org.jboss.util.Classes utility
+ * 
+ * @author Dimitris.Andreadis at jboss.org
+ * @version $Revision: 43534 $
+ */
+public class ClassesUnitTestCase extends TestCase
+{
+   public void testGetAllInterfaces()
+   {
+      List list = new ArrayList();
+      Classes.getAllInterfaces(list, ExtendedClass.class);
+      assertEquals(3, list.size());
+      assertEquals(Interface1.class, (java.lang.Class)list.get(0));
+      assertEquals(Interface1.class, (java.lang.Class)list.get(1));
+      assertEquals(Interface2.class, (java.lang.Class)list.get(2));
+   }
+   
+   public void testGetAllUniqueInterfaces()
+   {
+      Class[] interfaces = Classes.getAllUniqueInterfaces(ExtendedClass.class);
+      assertEquals(2, interfaces.length);
+   }
+   
+   public interface Interface1 {}
+   public interface Interface2 {}
+
+   public static class BaseClass implements Interface1, Interface2 {}
+   public static class ExtendedClass extends BaseClass implements Interface1 {}
+} 
\ No newline at end of file




More information about the jboss-svn-commits mailing list