velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r579714 - in /velocity/engine/trunk/src: java/org/apache/velocity/util/introspection/UberspectImpl.java test/org/apache/velocity/test/ForeachTestCase.java
Date Wed, 26 Sep 2007 16:53:20 GMT
Author: nbubna
Date: Wed Sep 26 09:53:16 2007
New Revision: 579714

URL: http://svn.apache.org/viewvc?rev=579714&view=rev
Log:
when falling back to iterator() method in #foreach, be sure to check return type
(thanks to Geir for catching this oversight)

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java?rev=579714&r1=579713&r2=579714&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java
Wed Sep 26 09:53:16 2007
@@ -149,7 +149,16 @@
             try
             {
                 Method iter = type.getMethod("iterator", null);
-                return (Iterator)iter.invoke(obj, null);
+                Class returns = iter.getReturnType();
+                if (Iterator.class.isAssignableFrom(returns))
+                {
+                    return (Iterator)iter.invoke(obj, null);
+                }
+                else
+                {
+                    log.error("iterator() method of reference in #foreach loop at "
+                              + i + " does not return a true Iterator.");
+                }
             }
             catch (NoSuchMethodException nsme)
             {

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java?rev=579714&r1=579713&r2=579714&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/ForeachTestCase.java Wed Sep 26
09:53:16 2007
@@ -113,6 +113,17 @@
                      "1 2 3 ", writer.toString());
     }
 
+    public void testNotReallyIterableIteratorMethod()
+        throws Exception
+    {
+        context.put("nri", new NotReallyIterable());
+
+        StringWriter writer = new StringWriter();
+        String template = "#foreach ($i in $nri)$i #end";
+        Velocity.evaluate(context, writer, "test", template);
+        assertEquals("", writer.toString());
+    }
+
 
     public static class MyIterable
     {
@@ -131,5 +142,13 @@
             return foo.iterator();
         }
     }
-        
+
+    public static class NotReallyIterable
+    {
+        public Object iterator()
+        {
+            return new Object();
+        }
+    }
+
 }



Mime
View raw message