You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Biestro (Jira)" <ji...@apache.org> on 2022/05/25 06:44:00 UTC

[jira] [Comment Edited] (JEXL-370) Cannot check if variable is defined using ObjectContext if the value is null

    [ https://issues.apache.org/jira/browse/JEXL-370?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17541846#comment-17541846 ] 

Henri Biestro edited comment on JEXL-370 at 5/25/22 6:43 AM:
-------------------------------------------------------------

Thank you for the report Alex, issue confirmed.
As a workaround, you can use your own ObjectContext and override the has() method as in the following.
{code}
    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) {
            @Override public boolean has(String name) {
                return jexl.getUberspect().getPropertyGet(getObject(), name) != null;
            }
        };
        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"));
    }
{code}


was (Author: henrib):
Thank you for the report Alex, issue confirmed.
As a workaround, you can use your own ObjectContext and override the has() method as in the following.
{code}
    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) {
            @Override public boolean has(String name) {
                final JexlPropertyGet jget = jexl.getUberspect().getPropertyGet(getObject(), name);
                try {
                    return jget != null;
                } catch (final Exception xany) {
                    return false;
                }
            }
        };
        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"));
    }
{code}

> Cannot check if variable is defined using ObjectContext if the value is null 
> -----------------------------------------------------------------------------
>
>                 Key: JEXL-370
>                 URL: https://issues.apache.org/jira/browse/JEXL-370
>             Project: Commons JEXL
>          Issue Type: Bug
>    Affects Versions: 3.2.1
>            Reporter: Alex Hutton
>            Assignee: Henri Biestro
>            Priority: Minor
>             Fix For: 3.3
>
>         Attachments: ObjectContextTest.java
>
>
> I'm migrating from 3.1 to 3.2.1, and I've noticed the following change in behaviour when using ObjectContext:
>  * Calling ObjectContext.has(String) for an object which has the given property, but for which the current value is null, returns false, and not true as it did before.
>  * Similarly, evaluating an expression which uses a property that is defined but which is null, throws an ('is undefined') exception rather than returning the null value.
> The javadoc for JexlContext.has(String) says:
> {quote}
> A variable may be defined with a null value; this method checks whether the value is null or if the variable is undefined.
> {quote}
> so my impression is that the behaviour of ObjectContext does not match what is written in the Javadoc  for the JexlContext interface.  It is a problem in my case because the 'undefined' exception causes quite a lot of confusion in use.
> I've attached a test class to demonstrate the example.  Please let me know if any extra info is needed, and thanks for all the great work on Jexl!



--
This message was sent by Atlassian Jira
(v8.20.7#820007)