You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2007/09/25 19:59:48 UTC

svn commit: r579331 - in /velocity/engine/trunk/src: java/org/apache/velocity/util/introspection/UberspectImpl.java test/org/apache/velocity/test/ForeachTestCase.java

Author: nbubna
Date: Tue Sep 25 10:59:43 2007
New Revision: 579331

URL: http://svn.apache.org/viewvc?rev=579331&view=rev
Log:
support any object with an iterator() method in #foreach (VELOCITY-443)

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=579331&r1=579330&r2=579331&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 Tue Sep 25 10:59:43 2007
@@ -139,6 +139,22 @@
             }
             return new EnumerationIterator((Enumeration) obj);
         }
+        else
+        {
+            // look for an iterator() method to support the JDK5 Iterable
+            // interface or any user tools/DTOs that want to work in
+            // foreach without implementing the Collection interface
+            Class type = obj.getClass();
+            try
+            {
+                Method iter = type.getMethod("iterator", null);
+                return (Iterator)iter.invoke(obj, null);
+            }
+            catch (NoSuchMethodException nsme)
+            {
+                // eat this one, but let all other exceptions thru
+            }
+        }
 
         /*  we have no clue what this is  */
         log.error("Could not determine type of iterator in #foreach loop at " + i);

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=579331&r1=579330&r2=579331&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 Tue Sep 25 10:59:43 2007
@@ -21,6 +21,7 @@
 
 import java.io.StringWriter;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
 import junit.framework.TestCase;
@@ -75,10 +76,10 @@
     }
 
     /**
-     * Tests proper method execution during a Foreach loop with items
-     * of varying classes.
+     * Tests proper method execution during a Foreach loop over a Collection
+     * with items of varying classes.
      */
-    public void testMethodCall()
+    public void testCollectionAndMethodCall()
         throws Exception
     {
         List col = new ArrayList();
@@ -94,4 +95,41 @@
         assertEquals("Method calls while looping over varying classes failed",
                      "int 100 str STRVALUE ", writer.toString());
     }
+
+    /**
+     * Tests that #foreach will be able to retrieve an iterator from
+     * an arbitrary object that happens to have an iterator() method.
+     * (With the side effect of supporting the new Java 5 Iterable interface)
+     */
+    public void testObjectWithIteratorMethod()
+        throws Exception
+    {
+        context.put("iterable", new MyIterable());
+
+        StringWriter writer = new StringWriter();
+        String template = "#foreach ($i in $iterable)$i #end";
+        Velocity.evaluate(context, writer, "test", template);
+        assertEquals("Failed to call iterator() method",
+                     "1 2 3 ", writer.toString());
+    }
+
+
+    public static class MyIterable
+    {
+        private List foo;
+
+        public MyIterable()
+        {
+            foo = new ArrayList();
+            foo.add(new Integer(1));
+            foo.add(new Long(2));
+            foo.add("3");
+        }
+        
+        public Iterator iterator()
+        {
+            return foo.iterator();
+        }
+    }
+        
 }



Re: svn commit: r579331 - in /velocity/engine/trunk/src: java/org/apache/velocity/util/introspection/UberspectImpl.java test/org/apache/velocity/test/ForeachTestCase.java

Posted by Nathan Bubna <nb...@gmail.com>.
On 9/25/07, Geir Magnusson Jr. <ge...@pobox.com> wrote:
> what if it returns something you can't iterate over?  Maybe check the
> return type first?

yeah, that's a problem.  thanks for catching this!

> geir
>
> On Sep 25, 2007, at 4:22 PM, Nathan Bubna wrote:
>
> > On 9/25/07, Will Glass-Husain <wg...@gmail.com> wrote:
> >> Wow, Nathan -- you are on a tear.  Nice work.
> >
> > well, i found a little time to work on some of these nagging little
> > things.  :)
> >
> >> WILL
> >>
> >> On 9/25/07, nbubna@apache.org <nb...@apache.org> wrote:
> >>>
> >>> Author: nbubna
> >>> Date: Tue Sep 25 10:59:43 2007
> >>> New Revision: 579331
> >>>
> >>> URL: http://svn.apache.org/viewvc?rev=579331&view=rev
> >>> Log:
> >>> support any object with an iterator() method in #foreach
> >>> (VELOCITY-443)
> >>>
> >>> 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=579331&r1=579330&r2=579331&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
> >>> Tue Sep 25 10:59:43 2007
> >>> @@ -139,6 +139,22 @@
> >>>              }
> >>>              return new EnumerationIterator((Enumeration) obj);
> >>>          }
> >>> +        else
> >>> +        {
> >>> +            // look for an iterator() method to support the JDK5
> >>> Iterable
> >>> +            // interface or any user tools/DTOs that want to
> >>> work in
> >>> +            // foreach without implementing the Collection
> >>> interface
> >>> +            Class type = obj.getClass();
> >>> +            try
> >>> +            {
> >>> +                Method iter = type.getMethod("iterator", null);
> >>> +                return (Iterator)iter.invoke(obj, null);
> >>> +            }
> >>> +            catch (NoSuchMethodException nsme)
> >>> +            {
> >>> +                // eat this one, but let all other exceptions thru
> >>> +            }
> >>> +        }
> >>>
> >>>          /*  we have no clue what this is  */
> >>>          log.error("Could not determine type of iterator in
> >>> #foreach loop
> >>> at " + i);
> >>>
> >>> 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=579331&r1=579330&r2=579331&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
> >>> Tue Sep 25 10:59:43 2007
> >>> @@ -21,6 +21,7 @@
> >>>
> >>> import java.io.StringWriter;
> >>> import java.util.ArrayList;
> >>> +import java.util.Iterator;
> >>> import java.util.List;
> >>>
> >>> import junit.framework.TestCase;
> >>> @@ -75,10 +76,10 @@
> >>>      }
> >>>
> >>>      /**
> >>> -     * Tests proper method execution during a Foreach loop with
> >>> items
> >>> -     * of varying classes.
> >>> +     * Tests proper method execution during a Foreach loop over a
> >>> Collection
> >>> +     * with items of varying classes.
> >>>       */
> >>> -    public void testMethodCall()
> >>> +    public void testCollectionAndMethodCall()
> >>>          throws Exception
> >>>      {
> >>>          List col = new ArrayList();
> >>> @@ -94,4 +95,41 @@
> >>>          assertEquals("Method calls while looping over varying
> >>> classes
> >>> failed",
> >>>                       "int 100 str STRVALUE ", writer.toString());
> >>>      }
> >>> +
> >>> +    /**
> >>> +     * Tests that #foreach will be able to retrieve an iterator
> >>> from
> >>> +     * an arbitrary object that happens to have an iterator()
> >>> method.
> >>> +     * (With the side effect of supporting the new Java 5 Iterable
> >>> interface)
> >>> +     */
> >>> +    public void testObjectWithIteratorMethod()
> >>> +        throws Exception
> >>> +    {
> >>> +        context.put("iterable", new MyIterable());
> >>> +
> >>> +        StringWriter writer = new StringWriter();
> >>> +        String template = "#foreach ($i in $iterable)$i #end";
> >>> +        Velocity.evaluate(context, writer, "test", template);
> >>> +        assertEquals("Failed to call iterator() method",
> >>> +                     "1 2 3 ", writer.toString());
> >>> +    }
> >>> +
> >>> +
> >>> +    public static class MyIterable
> >>> +    {
> >>> +        private List foo;
> >>> +
> >>> +        public MyIterable()
> >>> +        {
> >>> +            foo = new ArrayList();
> >>> +            foo.add(new Integer(1));
> >>> +            foo.add(new Long(2));
> >>> +            foo.add("3");
> >>> +        }
> >>> +
> >>> +        public Iterator iterator()
> >>> +        {
> >>> +            return foo.iterator();
> >>> +        }
> >>> +    }
> >>> +
> >>> }
> >>>
> >>>
> >>>
> >>
> >>
> >> --
> >> Forio Business Simulations
> >>
> >> Will Glass-Husain
> >> wglass@forio.com
> >> www.forio.com
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> > For additional commands, e-mail: dev-help@velocity.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: svn commit: r579331 - in /velocity/engine/trunk/src: java/org/apache/velocity/util/introspection/UberspectImpl.java test/org/apache/velocity/test/ForeachTestCase.java

Posted by Nathan Bubna <nb...@gmail.com>.
On 9/26/07, Christopher Schultz <ch...@christopherschultz.net> wrote:
> Geir,
>
> Geir Magnusson Jr. wrote:
> > what if it returns something you can't iterate over?
>
> You get a ClassCastException :(
>
> > Maybe check the return type first?
>
> I agree. It's your choice if you want to throw something like
> VelocityException or some other RuntimeException, or if you want to
> ignore the loop and log an error (similar to #if($doesntExist = 'foo')
> behavior).

i'll log a message to point out the problem and return null from
UberspectImpl.getIterator(), so that it acts otherwise just as it
would have before.

> -chris
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: svn commit: r579331 - in /velocity/engine/trunk/src: java/org/apache/velocity/util/introspection/UberspectImpl.java test/org/apache/velocity/test/ForeachTestCase.java

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Geir,

Geir Magnusson Jr. wrote:
> what if it returns something you can't iterate over?

You get a ClassCastException :(

> Maybe check the return type first?

I agree. It's your choice if you want to throw something like
VelocityException or some other RuntimeException, or if you want to
ignore the loop and log an error (similar to #if($doesntExist = 'foo')
behavior).

-chris



Re: svn commit: r579331 - in /velocity/engine/trunk/src: java/org/apache/velocity/util/introspection/UberspectImpl.java test/org/apache/velocity/test/ForeachTestCase.java

Posted by "Geir Magnusson Jr." <ge...@pobox.com>.
what if it returns something you can't iterate over?  Maybe check the  
return type first?

geir

On Sep 25, 2007, at 4:22 PM, Nathan Bubna wrote:

> On 9/25/07, Will Glass-Husain <wg...@gmail.com> wrote:
>> Wow, Nathan -- you are on a tear.  Nice work.
>
> well, i found a little time to work on some of these nagging little  
> things.  :)
>
>> WILL
>>
>> On 9/25/07, nbubna@apache.org <nb...@apache.org> wrote:
>>>
>>> Author: nbubna
>>> Date: Tue Sep 25 10:59:43 2007
>>> New Revision: 579331
>>>
>>> URL: http://svn.apache.org/viewvc?rev=579331&view=rev
>>> Log:
>>> support any object with an iterator() method in #foreach  
>>> (VELOCITY-443)
>>>
>>> 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=579331&r1=579330&r2=579331&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
>>> Tue Sep 25 10:59:43 2007
>>> @@ -139,6 +139,22 @@
>>>              }
>>>              return new EnumerationIterator((Enumeration) obj);
>>>          }
>>> +        else
>>> +        {
>>> +            // look for an iterator() method to support the JDK5  
>>> Iterable
>>> +            // interface or any user tools/DTOs that want to  
>>> work in
>>> +            // foreach without implementing the Collection  
>>> interface
>>> +            Class type = obj.getClass();
>>> +            try
>>> +            {
>>> +                Method iter = type.getMethod("iterator", null);
>>> +                return (Iterator)iter.invoke(obj, null);
>>> +            }
>>> +            catch (NoSuchMethodException nsme)
>>> +            {
>>> +                // eat this one, but let all other exceptions thru
>>> +            }
>>> +        }
>>>
>>>          /*  we have no clue what this is  */
>>>          log.error("Could not determine type of iterator in  
>>> #foreach loop
>>> at " + i);
>>>
>>> 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=579331&r1=579330&r2=579331&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
>>> Tue Sep 25 10:59:43 2007
>>> @@ -21,6 +21,7 @@
>>>
>>> import java.io.StringWriter;
>>> import java.util.ArrayList;
>>> +import java.util.Iterator;
>>> import java.util.List;
>>>
>>> import junit.framework.TestCase;
>>> @@ -75,10 +76,10 @@
>>>      }
>>>
>>>      /**
>>> -     * Tests proper method execution during a Foreach loop with  
>>> items
>>> -     * of varying classes.
>>> +     * Tests proper method execution during a Foreach loop over a
>>> Collection
>>> +     * with items of varying classes.
>>>       */
>>> -    public void testMethodCall()
>>> +    public void testCollectionAndMethodCall()
>>>          throws Exception
>>>      {
>>>          List col = new ArrayList();
>>> @@ -94,4 +95,41 @@
>>>          assertEquals("Method calls while looping over varying  
>>> classes
>>> failed",
>>>                       "int 100 str STRVALUE ", writer.toString());
>>>      }
>>> +
>>> +    /**
>>> +     * Tests that #foreach will be able to retrieve an iterator  
>>> from
>>> +     * an arbitrary object that happens to have an iterator()  
>>> method.
>>> +     * (With the side effect of supporting the new Java 5 Iterable
>>> interface)
>>> +     */
>>> +    public void testObjectWithIteratorMethod()
>>> +        throws Exception
>>> +    {
>>> +        context.put("iterable", new MyIterable());
>>> +
>>> +        StringWriter writer = new StringWriter();
>>> +        String template = "#foreach ($i in $iterable)$i #end";
>>> +        Velocity.evaluate(context, writer, "test", template);
>>> +        assertEquals("Failed to call iterator() method",
>>> +                     "1 2 3 ", writer.toString());
>>> +    }
>>> +
>>> +
>>> +    public static class MyIterable
>>> +    {
>>> +        private List foo;
>>> +
>>> +        public MyIterable()
>>> +        {
>>> +            foo = new ArrayList();
>>> +            foo.add(new Integer(1));
>>> +            foo.add(new Long(2));
>>> +            foo.add("3");
>>> +        }
>>> +
>>> +        public Iterator iterator()
>>> +        {
>>> +            return foo.iterator();
>>> +        }
>>> +    }
>>> +
>>> }
>>>
>>>
>>>
>>
>>
>> --
>> Forio Business Simulations
>>
>> Will Glass-Husain
>> wglass@forio.com
>> www.forio.com
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: svn commit: r579331 - in /velocity/engine/trunk/src: java/org/apache/velocity/util/introspection/UberspectImpl.java test/org/apache/velocity/test/ForeachTestCase.java

Posted by Nathan Bubna <nb...@gmail.com>.
On 9/25/07, Will Glass-Husain <wg...@gmail.com> wrote:
> Wow, Nathan -- you are on a tear.  Nice work.

well, i found a little time to work on some of these nagging little things.  :)

> WILL
>
> On 9/25/07, nbubna@apache.org <nb...@apache.org> wrote:
> >
> > Author: nbubna
> > Date: Tue Sep 25 10:59:43 2007
> > New Revision: 579331
> >
> > URL: http://svn.apache.org/viewvc?rev=579331&view=rev
> > Log:
> > support any object with an iterator() method in #foreach (VELOCITY-443)
> >
> > 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=579331&r1=579330&r2=579331&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
> > Tue Sep 25 10:59:43 2007
> > @@ -139,6 +139,22 @@
> >              }
> >              return new EnumerationIterator((Enumeration) obj);
> >          }
> > +        else
> > +        {
> > +            // look for an iterator() method to support the JDK5 Iterable
> > +            // interface or any user tools/DTOs that want to work in
> > +            // foreach without implementing the Collection interface
> > +            Class type = obj.getClass();
> > +            try
> > +            {
> > +                Method iter = type.getMethod("iterator", null);
> > +                return (Iterator)iter.invoke(obj, null);
> > +            }
> > +            catch (NoSuchMethodException nsme)
> > +            {
> > +                // eat this one, but let all other exceptions thru
> > +            }
> > +        }
> >
> >          /*  we have no clue what this is  */
> >          log.error("Could not determine type of iterator in #foreach loop
> > at " + i);
> >
> > 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=579331&r1=579330&r2=579331&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
> > Tue Sep 25 10:59:43 2007
> > @@ -21,6 +21,7 @@
> >
> > import java.io.StringWriter;
> > import java.util.ArrayList;
> > +import java.util.Iterator;
> > import java.util.List;
> >
> > import junit.framework.TestCase;
> > @@ -75,10 +76,10 @@
> >      }
> >
> >      /**
> > -     * Tests proper method execution during a Foreach loop with items
> > -     * of varying classes.
> > +     * Tests proper method execution during a Foreach loop over a
> > Collection
> > +     * with items of varying classes.
> >       */
> > -    public void testMethodCall()
> > +    public void testCollectionAndMethodCall()
> >          throws Exception
> >      {
> >          List col = new ArrayList();
> > @@ -94,4 +95,41 @@
> >          assertEquals("Method calls while looping over varying classes
> > failed",
> >                       "int 100 str STRVALUE ", writer.toString());
> >      }
> > +
> > +    /**
> > +     * Tests that #foreach will be able to retrieve an iterator from
> > +     * an arbitrary object that happens to have an iterator() method.
> > +     * (With the side effect of supporting the new Java 5 Iterable
> > interface)
> > +     */
> > +    public void testObjectWithIteratorMethod()
> > +        throws Exception
> > +    {
> > +        context.put("iterable", new MyIterable());
> > +
> > +        StringWriter writer = new StringWriter();
> > +        String template = "#foreach ($i in $iterable)$i #end";
> > +        Velocity.evaluate(context, writer, "test", template);
> > +        assertEquals("Failed to call iterator() method",
> > +                     "1 2 3 ", writer.toString());
> > +    }
> > +
> > +
> > +    public static class MyIterable
> > +    {
> > +        private List foo;
> > +
> > +        public MyIterable()
> > +        {
> > +            foo = new ArrayList();
> > +            foo.add(new Integer(1));
> > +            foo.add(new Long(2));
> > +            foo.add("3");
> > +        }
> > +
> > +        public Iterator iterator()
> > +        {
> > +            return foo.iterator();
> > +        }
> > +    }
> > +
> > }
> >
> >
> >
>
>
> --
> Forio Business Simulations
>
> Will Glass-Husain
> wglass@forio.com
> www.forio.com
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: svn commit: r579331 - in /velocity/engine/trunk/src: java/org/apache/velocity/util/introspection/UberspectImpl.java test/org/apache/velocity/test/ForeachTestCase.java

Posted by Will Glass-Husain <wg...@gmail.com>.
Wow, Nathan -- you are on a tear.  Nice work.

WILL

On 9/25/07, nbubna@apache.org <nb...@apache.org> wrote:
>
> Author: nbubna
> Date: Tue Sep 25 10:59:43 2007
> New Revision: 579331
>
> URL: http://svn.apache.org/viewvc?rev=579331&view=rev
> Log:
> support any object with an iterator() method in #foreach (VELOCITY-443)
>
> 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=579331&r1=579330&r2=579331&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
> Tue Sep 25 10:59:43 2007
> @@ -139,6 +139,22 @@
>              }
>              return new EnumerationIterator((Enumeration) obj);
>          }
> +        else
> +        {
> +            // look for an iterator() method to support the JDK5 Iterable
> +            // interface or any user tools/DTOs that want to work in
> +            // foreach without implementing the Collection interface
> +            Class type = obj.getClass();
> +            try
> +            {
> +                Method iter = type.getMethod("iterator", null);
> +                return (Iterator)iter.invoke(obj, null);
> +            }
> +            catch (NoSuchMethodException nsme)
> +            {
> +                // eat this one, but let all other exceptions thru
> +            }
> +        }
>
>          /*  we have no clue what this is  */
>          log.error("Could not determine type of iterator in #foreach loop
> at " + i);
>
> 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=579331&r1=579330&r2=579331&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
> Tue Sep 25 10:59:43 2007
> @@ -21,6 +21,7 @@
>
> import java.io.StringWriter;
> import java.util.ArrayList;
> +import java.util.Iterator;
> import java.util.List;
>
> import junit.framework.TestCase;
> @@ -75,10 +76,10 @@
>      }
>
>      /**
> -     * Tests proper method execution during a Foreach loop with items
> -     * of varying classes.
> +     * Tests proper method execution during a Foreach loop over a
> Collection
> +     * with items of varying classes.
>       */
> -    public void testMethodCall()
> +    public void testCollectionAndMethodCall()
>          throws Exception
>      {
>          List col = new ArrayList();
> @@ -94,4 +95,41 @@
>          assertEquals("Method calls while looping over varying classes
> failed",
>                       "int 100 str STRVALUE ", writer.toString());
>      }
> +
> +    /**
> +     * Tests that #foreach will be able to retrieve an iterator from
> +     * an arbitrary object that happens to have an iterator() method.
> +     * (With the side effect of supporting the new Java 5 Iterable
> interface)
> +     */
> +    public void testObjectWithIteratorMethod()
> +        throws Exception
> +    {
> +        context.put("iterable", new MyIterable());
> +
> +        StringWriter writer = new StringWriter();
> +        String template = "#foreach ($i in $iterable)$i #end";
> +        Velocity.evaluate(context, writer, "test", template);
> +        assertEquals("Failed to call iterator() method",
> +                     "1 2 3 ", writer.toString());
> +    }
> +
> +
> +    public static class MyIterable
> +    {
> +        private List foo;
> +
> +        public MyIterable()
> +        {
> +            foo = new ArrayList();
> +            foo.add(new Integer(1));
> +            foo.add(new Long(2));
> +            foo.add("3");
> +        }
> +
> +        public Iterator iterator()
> +        {
> +            return foo.iterator();
> +        }
> +    }
> +
> }
>
>
>


-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com