aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/main/java/org
diff options
context:
space:
mode:
authorClaude Brisson <cbrisson@apache.org>2017-02-20 10:58:58 +0000
committerClaude Brisson <cbrisson@apache.org>2017-02-20 10:58:58 +0000
commita970dc446c23cfec163226111b98e764df5eb128 (patch)
treed0438dd4176227d02c5056d03214722bb1069577 /velocity-engine-core/src/main/java/org
parent4dc2f90c75f9060f7e6d7add27d6eec650e0e182 (diff)
downloadapache-velocity-engine-a970dc446c23cfec163226111b98e764df5eb128.tar.gz
[engine] implements new strategy for reference boolean evaluation
1) return false for a null object 2) return its value for a Boolean object, or the result of the getAsBoolean() method if it exists. 3) If directive.if.emptycheck = false (true by default), stop here and return true. 4) check for emptiness: - return whether an array is empty. - return whether isEmpty() is false (covers String and all Collection classes). - return whether length() is zero (covers CharSequence classes other than String). - returns whether size() is zero. - return whether a Number *strictly* equals zero. 5) check for emptiness after explicit conversion methods: - return whether the result of getAsString() is empty (and false for a null result) if it exists. - return whether the result of getAsNumber() *strictly* equals zero (and false for a null result) if it exists. git-svn-id: https://svn.apache.org/repos/asf/velocity/engine/trunk@1783739 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'velocity-engine-core/src/main/java/org')
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java8
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java2
-rw-r--r--velocity-engine-core/src/main/java/org/apache/velocity/util/DuckType.java49
3 files changed, 41 insertions, 18 deletions
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
index 2d1b5d91..b85c51f1 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/RuntimeConstants.java
@@ -76,6 +76,12 @@ public interface RuntimeConstants
String SKIP_INVALID_ITERATOR = "directive.foreach.skip.invalid";
/**
+ * An empty object (string, collection) or zero number is false.
+ * @since 2.0
+ */
+ String CHECK_EMPTY_OBJECTS = "directive.if.emptycheck";
+
+ /**
* Starting tag for error messages triggered by passing a parameter not allowed in the #include directive. Only string literals,
* and references are allowed.
*/
@@ -98,7 +104,7 @@ public interface RuntimeConstants
* @since 1.7
*/
String PROVIDE_SCOPE_CONTROL = "provide.scope.control";
-
+
/*
* ----------------------------------------------------------------------
* R E S O U R C E M A N A G E R C O N F I G U R A T I O N
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
index da210371..7875020e 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/runtime/parser/node/ASTReference.java
@@ -579,7 +579,7 @@ public class ASTReference extends SimpleNode
}
try
{
- return DuckType.asBoolean(value);
+ return DuckType.asBoolean(value, rsvc.getBoolean(RuntimeConstants.CHECK_EMPTY_OBJECTS, true));
}
catch(Exception e)
{
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/util/DuckType.java b/velocity-engine-core/src/main/java/org/apache/velocity/util/DuckType.java
index eb426d6e..5edfb093 100644
--- a/velocity-engine-core/src/main/java/org/apache/velocity/util/DuckType.java
+++ b/velocity-engine-core/src/main/java/org/apache/velocity/util/DuckType.java
@@ -23,9 +23,12 @@ import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
+import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
+import static org.apache.velocity.runtime.parser.node.MathUtils.isZero;
+
/**
* Support for getAs<Type>() convention for rendering (String), evaluating (Boolean)
* or doing math with (Number) references.
@@ -40,7 +43,9 @@ public class DuckType
STRING("getAsString"),
NUMBER("getAsNumber"),
BOOLEAN("getAsBoolean"),
- EMPTY("isEmpty");
+ EMPTY("isEmpty"),
+ LENGTH("length"),
+ SIZE("size");
final String name;
final Map<Class,Object> cache = new HashMap();
@@ -93,11 +98,6 @@ public class DuckType
get(value, Types.NUMBER) == null;
}
- public static boolean asBoolean(Object value)
- {
- return asBoolean(value, true);
- }
-
public static boolean asBoolean(Object value, boolean coerceType)
{
if (value == null)
@@ -129,23 +129,37 @@ public class DuckType
return true;
}
- // empty string
- if (value instanceof CharSequence)
+ // empty array
+ if (value.getClass().isArray())
{
- return ((CharSequence)value).length() == 0;
+ return Array.getLength(value) == 0;// [] is false
}
- // isEmpty() object
+ // isEmpty() for object / string
Object isEmpty = get(value, Types.EMPTY);
if (isEmpty != NO_METHOD)
{
return (Boolean)isEmpty;
}
- // empty array
- if (value.getClass().isArray())
+ // isEmpty() for object / other char sequences
+ Object length = get(value, Types.LENGTH);
+ if (length != NO_METHOD && length instanceof Number)
{
- return Array.getLength(value) == 0;// [] is false
+ return isZero((Number)length);
+ }
+
+ // size() object / collection
+ Object size = get(value, Types.SIZE);
+ if (size != NO_METHOD && size instanceof Number)
+ {
+ return isZero((Number)size);
+ }
+
+ // zero numbers are false
+ if (value instanceof Number)
+ {
+ return isZero((Number)value);
}
// null getAsString()
@@ -166,10 +180,13 @@ public class DuckType
{
return true;
}
+ // zero numbers are false
+ else if (asNumber != NO_METHOD && asNumber instanceof Number)
+ {
+ return isZero((Number)asNumber);
+ }
- // empty toString()
- String string = value.toString();
- return string == null || string.length() == 0;
+ return false;
}
public static Number asNumber(Object value)