This is an automated email from the ASF dual-hosted git repository. henrib pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jexl.git
The following commit(s) were added to refs/heads/master by this push: new dfc15a74 JEXL-370: ObjectContext has() method no longer confuses property existence and property being null; dfc15a74 is described below commit dfc15a7472d0b641a19bc6e69f6bb90690eda4f0 Author: henrib <hen...@apache.org> AuthorDate: Wed May 25 10:52:40 2022 +0200 JEXL-370: ObjectContext has() method no longer confuses property existence and property being null; --- RELEASE-NOTES.txt | 1 + .../org/apache/commons/jexl3/ObjectContext.java | 7 +---- .../org/apache/commons/jexl3/Issues300Test.java | 35 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 9b3bb2b3..b56fde82 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -47,6 +47,7 @@ New Features in 3.3: Bugs Fixed in 3.3: ================== +* JEXL-370: Cannot check if variable is defined using ObjectContext if the value is null * JEXL-364: Evaluator options not propagated in closures * JEXL-362: JexlInfo position reporting is off * JEXL-361: Null may be used as operand silently even in arithmetic strict(true) mode diff --git a/src/main/java/org/apache/commons/jexl3/ObjectContext.java b/src/main/java/org/apache/commons/jexl3/ObjectContext.java index 6d4b0416..4fd408b1 100644 --- a/src/main/java/org/apache/commons/jexl3/ObjectContext.java +++ b/src/main/java/org/apache/commons/jexl3/ObjectContext.java @@ -90,12 +90,7 @@ public class ObjectContext<T> implements JexlContext, JexlContext.NamespaceResol @Override public boolean has(final String name) { - final JexlPropertyGet jget = jexl.getUberspect().getPropertyGet(object, name); - try { - return jget != null && jget.invoke(object) != null; - } catch (final Exception xany) { - return false; - } + return jexl.getUberspect().getPropertyGet(object, name) != null; } @Override diff --git a/src/test/java/org/apache/commons/jexl3/Issues300Test.java b/src/test/java/org/apache/commons/jexl3/Issues300Test.java index c5de60bd..e48ebf8e 100644 --- a/src/test/java/org/apache/commons/jexl3/Issues300Test.java +++ b/src/test/java/org/apache/commons/jexl3/Issues300Test.java @@ -28,6 +28,7 @@ import java.util.Map; import org.apache.commons.jexl3.internal.Engine32; import org.apache.commons.jexl3.internal.OptionsContext; +import org.apache.commons.jexl3.introspection.JexlPropertyGet; import org.junit.Assert; import static org.junit.Assert.assertEquals; import org.junit.Test; @@ -780,4 +781,38 @@ public class Issues300Test { String s1 = script.getSourceText(); Assert.assertNotEquals(s0, s1); } + + public static class Var370 { + private String name = null; + public void setName(String s) { + name = s; + } + public String getName() { + return name; + } + } + + @Test public void test370() { + Var370 var370 = new Var370(); + JexlEngine jexl = new JexlBuilder().safe(true).create(); + ObjectContext<Var370> ctxt = new ObjectContext<Var370>(jexl, var370); + JexlExpression get = jexl.createExpression("name"); + // not null + var370.setName("John"); + Assert.assertEquals("John",get.evaluate(ctxt)); + Assert.assertTrue(ctxt.has("name")); + // null + var370.setName(null); + Assert.assertNull(get.evaluate(ctxt)); + Assert.assertTrue(ctxt.has("name")); + // undefined + get = jexl.createExpression("phone"); + Assert.assertFalse(ctxt.has("phone")); + try { + get.evaluate(ctxt); + Assert.fail("phone should be undefined!"); + } catch(JexlException.Variable xvar) { + Assert.assertEquals("phone", xvar.getVariable()); + } + } }