velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r806597 - in /velocity/engine/branches/1.6.x: 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/
Date Fri, 21 Aug 2009 15:21:46 GMT
Author: nbubna
Date: Fri Aug 21 15:21:44 2009
New Revision: 806597

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

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

Modified: velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/RuntimeConstants.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/RuntimeConstants.java?rev=806597&r1=806596&r2=806597&view=diff
==============================================================================
--- velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/RuntimeConstants.java
(original)
+++ velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/RuntimeConstants.java
Fri Aug 21 15:21:44 2009
@@ -141,6 +141,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/branches/1.6.x/src/java/org/apache/velocity/runtime/defaults/velocity.properties
URL: http://svn.apache.org/viewvc/velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/defaults/velocity.properties?rev=806597&r1=806596&r2=806597&view=diff
==============================================================================
--- velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/defaults/velocity.properties
(original)
+++ velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/defaults/velocity.properties
Fri Aug 21 15:21:44 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/branches/1.6.x/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java?rev=806597&r1=806596&r2=806597&view=diff
==============================================================================
--- velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
(original)
+++ velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
Fri Aug 21 15:21:44 2009
@@ -75,6 +75,15 @@
      */
     public boolean strictRef = 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;
@@ -148,7 +157,8 @@
             rsvc.getBoolean(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, true);
 
         strictRef = rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false);
- 
+        toStringNullCheck = rsvc.getBoolean(RuntimeConstants.DIRECTIVE_IF_TOSTRING_NULLCHECK,
true); 
+
         /**
          * In the case we are referencing a variable with #if($foo) or
          * #if( ! $foo) then we allow variables to be undefined and we 
@@ -470,7 +480,7 @@
             else
                 return false;
         }        
-        else
+        else if (toStringNullCheck)
         {
             try
             {
@@ -482,6 +492,10 @@
                     + Log.formatFileString(this), e);
             }
         }
+        else
+        {
+            return true;
+        }
     }
 
     /**

Modified: velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/IfNullTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/IfNullTestCase.java?rev=806597&r1=806596&r2=806597&view=diff
==============================================================================
--- velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/IfNullTestCase.java (original)
+++ velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/IfNullTestCase.java Fri
Aug 21 15:21:44 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/branches/1.6.x/xdocs/docs/developer-guide.xml
URL: http://svn.apache.org/viewvc/velocity/engine/branches/1.6.x/xdocs/docs/developer-guide.xml?rev=806597&r1=806596&r2=806597&view=diff
==============================================================================
--- velocity/engine/branches/1.6.x/xdocs/docs/developer-guide.xml (original)
+++ velocity/engine/branches/1.6.x/xdocs/docs/developer-guide.xml Fri Aug 21 15:21:44 2009
@@ -1654,6 +1654,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>
 



Mime
View raw message