You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Eric Gravel <Er...@intervalintl.com> on 2012/05/01 00:26:06 UTC

[jexl] Question about checking for undefined properties versus null value properties

I'm in the process of improving the details of our logs when an error is
encountered when compiling or evaluating a JEXL.  I'm mainly outputting
the JexlException information, what the script was, what objects were in
the context; that kind of stuff.  To do this, I decided to let
JexlEngine throw exceptions but setting lenient to false (strict-mode)
and silent to true (I want my code to be the only part output to the
log).

 

One side effect of doing this has been that if a property return null,
the engine seems to think it's an undefined property.  Is there no way
to have JexlEngine only throw an exception if a property is undefined
when strict-mode is on?

 

 

Looking through the code, I believe this difficulty arises from
visit(ASTReference).  Btw, my simply jexl was something like
"journal.serviceAmount != null"  since we use BigDecimale objects.  

 

In visit(ASTReference), in Interpreter class, there is this block of
code towards the end:

 

        if (result == null) {

            if (isVariable && !isTernaryProtected(node)

                    // variable unknow in context and not (from) a
register

                    && !(context.has(variableName.toString())

                    || (numChildren == 1

                    && node.jjtGetChild(0) instanceof ASTIdentifier

                    && ((ASTIdentifier)
node.jjtGetChild(0)).getRegister() >= 0))) {

                JexlException xjexl = propertyName != null

                                      ? new JexlException.Property(node,
propertyName)

                                      : new JexlException.Variable(node,
variableName.toString());

                return unknownVariable(xjexl);

            }

        }

 

This will be triggered since my 2 child node (ASTIdentifier for
serviceAmount) will return null when the journal object's
getServiceAmount() is called and its null value is returned.  So since
it is null, isVariable is true, no Ternary node, property is not in jexl
context, there's 2 children, this throws exception.

 

It would seem to me that this visit() should be able to differentiate
between a null value property and an undefined property.  Could we store
a Boolean of whether a PropertyGet object is retrieved by
uberspect.getProperty(), which is called in getAttribute() from
visit(ASTIdentifier), and then modify the IF mentioned earlier to says
!hasGetterMethod so that it only throws if the null wasn't linked to a
Getter method?

 

 

Thoughts?

 

 

Eric A. Gravel

Application Architect - IT Application Services

Interval International

6262 Sunset Drive * Miami, Florida 33143

305.666.1861, ext. 7315 * fax 305.668.3409

eric.gravel@intervalintl.com <ma...@intervalintl.com> 

IntervalWorld.com * ResortDeveloper.com

Please consider the environment before printing this e-mail.

 


_____________________________________________________________________________
Scanned by IBM Email Security Management Services powered by MessageLabs. For more information please visit http://www.ers.ibm.com
_____________________________________________________________________________

Re: [jexl] Question about checking for undefined properties versus null value properties

Posted by henrib <he...@apache.org>.
Hi Eric,

If I may, your main question might be summarized as: Is there anyway to have
Jexl only throw an exception if a property or variable is undefined when
strict-mode is on?

The answer is "yes" with jexl3 (i.e. the trunk); the JexlArithmetic and the
JexlEngine may be configured with different "strict" modes, JexlArithmetic
dealing with whether "null" is an error as an operand and the JexlEngine
dealing with the "undefined" variable/property/methods.

This (new) test should exert this use case:


Is this is the behavior you expect?

Retrofitting this feature in 2.1.x is however not in the current plan; I'll
push a 3.0 SNAPSHOT out (i.e. maven snapshot) soon for convenience.
Hope this helps,
Regards.
Henrib


--
View this message in context: http://apache-commons.680414.n4.nabble.com/jexl-Question-about-checking-for-undefined-properties-versus-null-value-properties-tp4599572p4600711.html
Sent from the Commons - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org