velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r598472 - /velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java
Date Tue, 27 Nov 2007 00:27:32 GMT
Author: nbubna
Date: Mon Nov 26 16:27:31 2007
New Revision: 598472

URL: http://svn.apache.org/viewvc?rev=598472&view=rev
Log:
add support for getting iterators from lists,arrays, etc (formerly in IteratorTool, based
on UberspectImpl's version)

Modified:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java?rev=598472&r1=598471&r2=598472&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java Mon
Nov 26 16:27:31 2007
@@ -22,16 +22,22 @@
 import java.io.InputStream;
 import java.io.IOException;
 import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
+import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
+import org.apache.velocity.util.ArrayIterator;
+import org.apache.velocity.util.EnumerationIterator;
 
 /**
  * Repository for common class and reflection methods.
@@ -326,6 +332,54 @@
             throw new UnsupportedOperationException("Field "+fieldname+" in class "+clazz.getName()+"
is not static.  Only static fields are supported.");
         }
         return field.get(null);
+    }
+
+    /**
+     * Retrieves an Iterator from or creates and Iterator for the specified object.
+     * This method is almost entirely copied from Engine's UberspectImpl class.
+     */
+    public static Iterator getIterator(Object obj)
+        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
+    {
+        if (obj.getClass().isArray())
+        {
+            return new ArrayIterator(obj);
+        }
+        else if (obj instanceof Collection)
+        {
+            return ((Collection) obj).iterator();
+        }
+        else if (obj instanceof Map)
+        {
+            return ((Map) obj).values().iterator();
+        }
+        else if (obj instanceof Iterator)
+        {
+            return ((Iterator) obj);
+        }
+        else if (obj instanceof Iterable)
+        {
+            return ((Iterable)obj).iterator();
+        }
+        else if (obj instanceof Enumeration)
+        {
+            return new EnumerationIterator((Enumeration) obj);
+        }
+        else
+        {
+            // look for an iterator() method to support
+            // any user tools/DTOs that want to work in
+            // foreach w/o implementing the Collection interface
+            Method iter = obj.getClass().getMethod("iterator");
+            if (Iterator.class.isAssignableFrom(iter.getReturnType()))
+            {
+                return (Iterator)iter.invoke(obj);
+            }
+            else
+            {
+                return null;
+            }
+        }
     }
 
 }



Mime
View raw message