Uploaded image for project: 'Commons JEXL'
  1. Commons JEXL
  2. JEXL-26

ASTArrayAccess messes up on fallback to JexlContext

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.1
    • 2.0
    • None
    • N/A

    Description

      When executing an expression if a null comes up (unresolved object) it attempts to build an identifier to query the JexlContext directly. However it only works on sequential ASTIdentifier nodes. If there is another sort of node then it ends up searching the JexlContext for a mangled or malformed variable.

      I think this is a bug because subobjects with properties or methods that are null can "inherit" the value of objects or properties in the base JexlContext scope. It's too unpredictable to be a legitimate feature.

      for example in the expression: objects[23].status

      if (objects[23] != null && objects[23].status == null) then it will search for the bare expression "status" in the JexlContext. Ideally it would query the JexlContext map for the full expression, however the details of that are difficult to implement and can produce odd results.

      Even worse with an expression like: base.objects[23].status.
      if (base != null && base.objects[23] != null && base.objects[23].status == null) then this expression will evaluate look for "base.status" in the JexlContext map.

      It currently works as expected for expressions like base.object.status by searching for "base.object.status" in the JexlContext if status is null. Although I'm not entirely convinced that it is a good idea to do this.

      Simple Fix:

      in ASTReference.java change the function getIdentifierToDepth() to return an empty string if there are non ASTIdentifier Nodes in the expression (in my case it's ASTArrayAccess nodes). This is the simplest solution and stops subobjects effectively assuming values of objects in the base JexlContext while still allowing you to bind objects into the JexlContext with identifiers with "." in the name.

      So you could add "object.tracking.status" directly into the JexlContext and it would work. However you couldn't add object.tracking[23].status because one of the Nodes is an ArrayAccess node and it can't be translated back into a string by the current implementation.

      /* patched function */

      private String getIdentifierToDepth(int i) {
      StringBuffer varName = new StringBuffer();
      for (int j = 0; j <= i; j++) {
      SimpleNode node = (SimpleNode) jjtGetChild(j);
      if (node instanceof ASTIdentifier) {
      varName.append(((ASTIdentifier) node).getIdentifierString());
      if (j != i)

      { varName.append('.'); }

      } else

      { /* expression is unresolvable and variable name is invalid */ return ""; }

      }
      return varName.toString();
      }

      You could always add some code to support ASTArrayAccess nodes. However you'd also have to support either evaluating, or reducing to a string, the index expression. Which is messy.

      Attachments

        Activity

          People

            Unassigned Unassigned
            colinlear Colin Lear
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: