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 2009/08/21 17:30:38 UTC

svn commit: r806601 - in /velocity/engine/trunk: src/java/org/apache/velocity/runtime/ src/java/org/apache/velocity/runtime/defaults/ src/java/org/apache/velocity/runtime/parser/node/ src/test/org/apache/velocity/test/ xdocs/docs/

Author: nbubna
Date: Fri Aug 21 15:30:38 2009
New Revision: 806601

URL: http://svn.apache.org/viewvc?rev=806601&view=rev
Log:
VELOCITY-731 add option to avoid toString() null-check (thanks to Jorgen Rydenius)

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java
    velocity/engine/trunk/xdocs/docs/developer-guide.xml

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=806601&r1=806600&r2=806601&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeConstants.java Fri Aug 21 15:30:38 2009
@@ -146,6 +146,16 @@
     String SET_NULL_ALLOWED = "directive.set.null.allowed";
 
     /**
+     * Indicates if toString() should be called during #if condition evaluation
+     * just to ensure it does not return null. Check is unnecessary if all
+     * toString() implementations are known to have non-null return values.
+     * Disabling the check (like Velocity 1.5 did) will can boost performance
+     * since toString() may be a complex operation on large objects.
+     * @since 1.6
+     */
+    String DIRECTIVE_IF_TOSTRING_NULLCHECK = "directive.if.tostring.nullcheck";
+
+    /**
      * Starting tag for error messages triggered by passing a parameter not allowed in the #include directive. Only string literals,
      * and references are allowed.
      */

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties?rev=806601&r1=806600&r2=806601&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties Fri Aug 21 15:30:38 2009
@@ -70,6 +70,16 @@
 directive.set.null.allowed = false
 
 # ----------------------------------------------------------------------------
+# I F  P R O P E R T I E S
+# ----------------------------------------------------------------------------
+# These properties control the behavior of #if
+# Default behavior is to check return value of toString() and treat an object
+# with toString() that returns null as null. If all objects have toString()
+# methods that never return null, this check is unnecessary and can be disabled
+# to gain performance. In Velocity 1.5, no such null check was performed.
+directive.if.tostring.nullcheck = true
+
+# ----------------------------------------------------------------------------
 # I N C L U D E  P R O P E R T I E S
 # ----------------------------------------------------------------------------
 # These are the properties that governed the way #include'd content

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=806601&r1=806600&r2=806601&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java Fri Aug 21 15:30:38 2009
@@ -90,6 +90,15 @@
      */
     public boolean strictEscape = false;
     
+    /**
+     * Indicates if toString() should be called during condition evaluation just
+     * to ensure it does not return null. Check is unnecessary if all toString()
+     * implementations are known to have non-null return values. Disabling the
+     * check will give a performance improval since toString() may be a complex
+     * operation on large objects.
+     */
+    public boolean toStringNullCheck = true;
+    
     private int numChildren = 0;
 
     protected Info uberInfo;
@@ -129,6 +138,7 @@
         
         strictEscape = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT_ESCAPE, false);
         strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
+        toStringNullCheck = rsvc.getBoolean(RuntimeConstants.DIRECTIVE_IF_TOSTRING_NULLCHECK, true); 
             
         /*
          *  the only thing we can do in init() is getRoot()
@@ -166,7 +176,7 @@
          */
         logOnNull =
             rsvc.getBoolean(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, true);
-         
+
         /**
          * In the case we are referencing a variable with #if($foo) or
          * #if( ! $foo) then we allow variables to be undefined and we 
@@ -530,7 +540,7 @@
             else
                 return false;
         }        
-        else
+        else if (toStringNullCheck)
         {
             try
             {
@@ -542,6 +552,10 @@
                     + Log.formatFileString(this), e);
             }
         }
+        else
+        {
+            return true;
+        }
     }
 
     /**

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java?rev=806601&r1=806600&r2=806601&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/IfNullTestCase.java Fri Aug 21 15:30:38 2009
@@ -20,6 +20,7 @@
  */
 
 import org.apache.velocity.VelocityContext;
+import org.apache.velocity.runtime.RuntimeConstants;
 
 /**
  * Used to check that nulls are properly handled in #if statements
@@ -89,6 +90,13 @@
         assertEvalEquals("foo", "#if( $notnull || $nullToString )foo#{else}bar#end");
     }
 
+    public void testToStringNullCheckConfig()
+    {
+        engine.setProperty(RuntimeConstants.DIRECTIVE_IF_TOSTRING_NULLCHECK, Boolean.FALSE);
+        assertEvalEquals("bar", "#if( $null )foo#{else}bar#end");
+        assertEvalEquals("foo", "#if( $nullToString )foo#{else}bar#end");
+    }
+
     public static class NullToString
     {
         public String toString()

Modified: velocity/engine/trunk/xdocs/docs/developer-guide.xml
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/xdocs/docs/developer-guide.xml?rev=806601&r1=806600&r2=806601&view=diff
==============================================================================
--- velocity/engine/trunk/xdocs/docs/developer-guide.xml (original)
+++ velocity/engine/trunk/xdocs/docs/developer-guide.xml Fri Aug 21 15:30:38 2009
@@ -1659,6 +1659,19 @@
 </p>
 
 <p>
+<strong>#if() Directive</strong>
+</p>
+
+<p>
+<code>directive.if.tostring.nullcheck = true</code><br/>
+Default behavior is to check return value of toString() and treat an
+object with toString() that returns null as null. If all objects have
+toString() methods that never return null, this check is unnecessary
+and can be disabled to gain performance. In Velocity 1.5, no such null
+check was performed.
+</p>
+
+<p>
 <strong>#set() Directive</strong>
 </p>