diff --git a/WHATSNEW b/WHATSNEW index 3d4a39832..40aed22ee 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -65,6 +65,11 @@ Other changes: * PropertyResource will effectively proxy another Resource if ${name} evaluates to a Resource object. + * Added forcestring attribute to equals condition to force evaluation + of Object args as strings; previously only API-level usage of the + equals condition allowed Object args, but Ant 1.8.x+ property + evaluation may yield values of any type. + Changes from Ant 1.8.0RC1 TO Ant 1.8.0 ====================================== diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index c09396949..9e1f7254b 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -138,7 +138,7 @@ the tests succeed.

equals

-

Tests whether the two given Strings are identical

+

Tests whether the two given values are equal.

@@ -147,12 +147,12 @@ the tests succeed. - + - + @@ -167,6 +167,13 @@ the tests succeed. them. Default is false. + + + + +
Attribute
arg1First string to test.First value to test. Yes
arg2Second string to test.Second value to test. Yes
No
forcestringForce string comparison of arg1/arg2. + Default is false. Since Ant 1.8.1 + No

isset

diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/Equals.java b/src/main/org/apache/tools/ant/taskdefs/condition/Equals.java index d214550d8..2d930f986 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/Equals.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/Equals.java @@ -32,6 +32,7 @@ public class Equals implements Condition { private boolean trim = false; private boolean caseSensitive = true; private int args; + private boolean forcestring = false; /** * Set the first argument @@ -106,6 +107,16 @@ public class Equals implements Condition { caseSensitive = b; } + /** + * Set whether to force string comparisons for non-equal, non-string objects. + * This allows object properties (legal in Ant 1.8.x+) to be compared as strings. + * @param forcestring value to set + * @since Ant 1.8.1 + */ + public void setForcestring(boolean forcestring) { + this.forcestring = forcestring; + } + /** * @return true if the two strings are equal * @exception BuildException if the attributes are not set correctly @@ -114,7 +125,13 @@ public class Equals implements Condition { if ((args & REQUIRED) != REQUIRED) { throw new BuildException("both arg1 and arg2 are required in equals"); } - + if (arg1 == arg2 || arg1 != null && arg1.equals(arg2)) { + return true; + } + if (forcestring) { + arg1 = arg1 == null || arg1 instanceof String ? arg1 : arg1.toString(); + arg2 = arg2 == null || arg2 instanceof String ? arg2 : arg2.toString(); + } if (arg1 instanceof String && trim) { arg1 = ((String) arg1).trim(); } @@ -126,6 +143,6 @@ public class Equals implements Condition { String s2 = (String) arg2; return caseSensitive ? s1.equals(s2) : s1.equalsIgnoreCase(s2); } - return arg1 == arg2 || arg1 != null && arg1.equals(arg2); + return false; } } diff --git a/src/tests/antunit/taskdefs/condition/equals-test.xml b/src/tests/antunit/taskdefs/condition/equals-test.xml new file mode 100644 index 000000000..8888d3967 --- /dev/null +++ b/src/tests/antunit/taskdefs/condition/equals-test.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +