velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1753755 - in /velocity/engine/trunk/velocity-engine-core/src: main/java/org/apache/velocity/runtime/parser/node/ main/java/org/apache/velocity/util/ main/java/org/apache/velocity/util/introspection/ test/java/org/apache/velocity/test/
Date Fri, 22 Jul 2016 07:32:50 GMT
Author: cbrisson
Date: Fri Jul 22 07:32:50 2016
New Revision: 1753755

URL: http://svn.apache.org/viewvc?rev=1753755&view=rev
Log:
[engine] support .empty; fixes VELOCITY-843

Modified:
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ArrayListWrapper.java
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ArrayMethodsTestCase.java

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java?rev=1753755&r1=1753754&r2=1753755&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java
Fri Jul 22 07:32:50 2016
@@ -46,9 +46,23 @@ public class BooleanPropertyExecutor ext
      * @since 1.5
      */
     public BooleanPropertyExecutor(final Logger log, final Introspector introspector,
-            final Class clazz, final String property)
+                                   final Class clazz, final String property)
     {
-        super(log, introspector, clazz, property);
+        this(log, introspector, clazz, property, false);
+    }
+
+    /**
+     * @param log
+     * @param introspector
+     * @param clazz
+     * @param property
+     * @maram wrapArray
+     * @since 1.5
+     */
+    public BooleanPropertyExecutor(final Logger log, final Introspector introspector,
+            final Class clazz, final String property, final boolean wrapArray)
+    {
+        super(log, introspector, clazz, property, wrapArray);
     }
 
     protected void discover(final Class clazz, final String property)

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java?rev=1753755&r1=1753754&r2=1753755&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java
Fri Jul 22 07:32:50 2016
@@ -21,6 +21,7 @@ package org.apache.velocity.runtime.pars
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.velocity.exception.VelocityException;
+import org.apache.velocity.util.ArrayListWrapper;
 import org.apache.velocity.util.introspection.Introspector;
 import org.slf4j.Logger;
 
@@ -32,6 +33,7 @@ import java.lang.reflect.InvocationTarge
 public class PropertyExecutor extends AbstractExecutor
 {
     private final Introspector introspector;
+    private final boolean wrapArray;
 
     /**
      * @param log
@@ -41,10 +43,25 @@ public class PropertyExecutor extends Ab
      * @since 1.5
      */
     public PropertyExecutor(final Logger log, final Introspector introspector,
-            final Class clazz, final String property)
+                            final Class clazz, final String property)
+    {
+        this(log, introspector, clazz, property, false);
+    }
+
+    /**
+     * @param log
+     * @param introspector
+     * @param clazz
+     * @param property
+     * @param wrapArray
+     * @since 1.5
+     */
+    public PropertyExecutor(final Logger log, final Introspector introspector,
+            final Class clazz, final String property, final boolean wrapArray)
     {
         this.log = log;
         this.introspector = introspector;
+        this.wrapArray = wrapArray;
 
         // Don't allow passing in the empty string or null because
         // it will either fail with a StringIndexOutOfBounds error
@@ -124,6 +141,10 @@ public class PropertyExecutor extends Ab
     public Object execute(Object o)
         throws IllegalAccessException,  InvocationTargetException
     {
+        if (wrapArray)
+        {
+            o = new ArrayListWrapper(o);
+        }
         return isAlive() ? getMethod().invoke(o, ((Object []) null)) : null;
     }
 }

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ArrayListWrapper.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ArrayListWrapper.java?rev=1753755&r1=1753754&r2=1753755&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ArrayListWrapper.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/ArrayListWrapper.java
Fri Jul 22 07:32:50 2016
@@ -55,4 +55,9 @@ public class ArrayListWrapper extends Ab
         return Array.getLength(array);
     }
 
+    public boolean isEmpty()
+    {
+        return Array.getLength(array) == 0;
+    }
+
 }

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java?rev=1753755&r1=1753754&r2=1753755&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
Fri Jul 22 07:32:50 2016
@@ -292,6 +292,15 @@ public class UberspectImpl implements Ub
                                                    identifier);
         }
 
+        /*
+         * and idem on an array
+         */
+        if (!executor.isAlive() && obj.getClass().isArray())
+        {
+            executor = new BooleanPropertyExecutor(log, introspector, ArrayListWrapper.class,
+                    identifier, true);
+        }
+
         return (executor.isAlive()) ? new VelGetterImpl(executor) : null;
     }
 

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ArrayMethodsTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ArrayMethodsTestCase.java?rev=1753755&r1=1753754&r2=1753755&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ArrayMethodsTestCase.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/ArrayMethodsTestCase.java
Fri Jul 22 07:32:50 2016
@@ -112,6 +112,8 @@ public class ArrayMethodsTestCase extend
         boolean isEmpty = (size == 0);
         checkResult("isEmpty()", String.valueOf(isEmpty), compareToList);
 
+        checkPropertyResult("empty", String.valueOf(isEmpty), compareToList);
+
         // check that the wrapping doesn't apply to java.lang.Object methods
         // such as toString() (for backwards compatibility).
         assertFalse(evaluate("$array").equals(evaluate("$list")));
@@ -175,13 +177,33 @@ public class ArrayMethodsTestCase extend
             assertEquals(result, listResult);
         }
 
-        info("    <$!array."+method+"> resolved to <"+result+">");
+        info("    <$!array." + method + "> resolved to <" + result + ">");
         if (compareToList)
         {
             info("    <$!list."+method+"> resolved to "+listResult+">");
         }
     }
 
+    private void checkPropertyResult(String property, String expected,
+                             boolean compareToList) throws Exception
+    {
+        String result = evaluate("$!array."+property);
+        assertEquals(expected, result);
+
+        String listResult = null;
+        if (compareToList)
+        {
+            listResult = evaluate("$!list."+property);
+            assertEquals(result, listResult);
+        }
+
+        info("    <$!array."+property+"> resolved to <"+result+">");
+        if (compareToList)
+        {
+            info("    <$!list."+property+"> resolved to "+listResult+">");
+        }
+    }
+
 }
 
 



Mime
View raw message