Author: wglass
Date: Wed Aug 23 11:53:40 2006
New Revision: 434124

URL: http://svn.apache.org/viewvc?rev=434124&view=rev
Log:
Stop calling object.toString() twice when evaluating references.  Thanks to 
Stephen Haberman for the patch.

Modified:
    
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
    
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/ParserTestCase.java
    jakarta/velocity/engine/trunk/xdocs/changes.xml

Modified: 
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL: 
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=434124&r1=434123&r2=434124&view=diff
==============================================================================
--- 
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
 (original)
+++ 
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
 Wed Aug 23 11:53:40 2006
@@ -265,11 +265,18 @@
 
         value =  EventHandlerUtil.referenceInsert(rsvc, context, literal(), 
value);
 
+        String toString = null;
+        if (value != null) 
+        {
+            toString = value.toString();
+        }
+
+
         /*
          *  if value is null...
          */
 
-        if ( value == null || (value.toString() == null) )
+        if ( value == null || toString == null)
         {
             /*
              *  write prefix twice, because it's schmoo, so the \ don't escape 
each other...
@@ -302,7 +309,7 @@
             {
                 writer.write(escPrefix);
                 writer.write(morePrefix);
-                writer.write(value.toString());
+                writer.write(toString);
             }
 
             return true;

Modified: 
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/ParserTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/ParserTestCase.java?rev=434124&r1=434123&r2=434124&view=diff
==============================================================================
--- 
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/ParserTestCase.java
 (original)
+++ 
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/ParserTestCase.java
 Wed Aug 23 11:53:40 2006
@@ -16,6 +16,8 @@
  */
 
 import java.io.StringWriter;
+import java.util.Map;
+import java.util.HashMap;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -155,6 +157,35 @@
         catch(ParseErrorException pe)
         {
             // Do nothing
+        }
+    }
+
+    /**
+     *  Test to see if we toString is called multiple times on references.
+     */
+    public void testASTReferenceToStringOnlyCalledOnce()
+        throws Exception
+    {
+        VelocityEngine ve = new VelocityEngine();
+
+        ve.init();
+
+        String template = "$counter";
+
+        ToStringCounter counter = new ToStringCounter();
+        Map m = new HashMap();
+        m.put("counter", counter);
+
+        ve.evaluate(new VelocityContext(m), new StringWriter(), "foo", 
template);
+
+        assertEquals(1, counter.timesCalled);
+    }
+
+    public static class ToStringCounter {
+        public int timesCalled = 0;
+        public String toString() {
+            this.timesCalled++;
+            return "foo";
         }
     }
 

Modified: jakarta/velocity/engine/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/xdocs/changes.xml?rev=434124&r1=434123&r2=434124&view=diff
==============================================================================
--- jakarta/velocity/engine/trunk/xdocs/changes.xml (original)
+++ jakarta/velocity/engine/trunk/xdocs/changes.xml Wed Aug 23 11:53:40 2006
@@ -24,6 +24,10 @@
   <body>
     <release version="1.5-dev" date="in Subversion">
 
+      <action type="fix" dev="wglass" issue="VELOCITY-438" due-to="Stephen 
Haberman">
+               Stop references from calling object.toString() twice.
+      </action>
+
       <action type="update" dev="wglass" issue="VELOCITY-429" due-to="">
                Pass through all runtime exceptions.  Among other benefits, 
this 
                allows plugins to throw a runtime exception to signify an 
application



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to