[
https://issues.apache.org/jira/browse/JEXL-294?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16802844#comment-16802844
]
Henri Biestro commented on JEXL-294:
It may seem odd but the explanation resides in the logic statement 'everything
that is not false is true'.
The values 0, NaN, null, false, the empty-string and the "false" string are
considered false in the default JEXL arithmetic, any other value is considered
true. The default 'equals' operator in presence of one boolean argument will
coerce the second one to a boolean also. The implication is thus that "abc" is
our example is coerced to boolean; since that string is neither empty nor
"false", it is true. And true == true.
If you need a different behaviour, I'd recommend implementing your own
JexlArithmetic, one that would not coerce boolean arguments.
{code:java}
public static class Arithmetic294 extends JexlArithmetic {
/** Ctor. */
public Arithmetic294(boolean f) {
super(f);
}
@Override
public boolean equals(Object left, Object right) {
if (left == right) {
return true;
} else if (left == null || right == null) {
return false;
} else {
return compare(left, right, "==") == 0;
}
}
}
@Test
public void test294b() throws Exception {
final String str = "(b)->{ b == true }";
JexlContext ctxt = new MapContext();
JexlEngine jexl = new JexlBuilder().arithmetic(new
Arithmetic294(true)).create();
JexlScript e = jexl.createScript(str);
Object value = e.execute(ctxt, "abc");
Assert.assertEquals(false, value);
value = e.execute(ctxt, true);
Assert.assertEquals(true, value);
}
{code}
> expression:b == true , when b="abc" then return true
>
>
> Key: JEXL-294
> URL: https://issues.apache.org/jira/browse/JEXL-294
> Project: Commons JEXL
> Issue Type: Bug
>Affects Versions: 3.1
> Environment: java 8
> commons-jexl 3.1
>Reporter: 锁战强
>Assignee: Henri Biestro
>Priority: Blocker
>
> It seems a bit odd that the expression "abc" == true returns true.
> {code:java}
>
> @Test
> public void test294() throws Exception {
> final String str = "(b)->{ b == true }";
> JexlContext ctxt = new MapContext();
> JexlEngine jexl = new JexlBuilder().create();
> JexlScript e = jexl.createScript(str);
> Object value = e.execute(ctxt, "abc");
> Assert.assertEquals(true, value); // "abc" == true ??
> }
> {code}
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)