On 9/25/07, Will Glass-Husain <[EMAIL PROTECTED]> 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, [EMAIL PROTECTED] <[EMAIL PROTECTED]> 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 > [EMAIL PROTECTED] > www.forio.com > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]