You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Dmitri Blinov (JIRA)" <ji...@apache.org> on 2017/06/22 12:03:00 UTC

[jira] [Commented] (JEXL-224) The ability to overload call() operator in customized JexlArithmetic implementation

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

Dmitri Blinov commented on JEXL-224:
------------------------------------

I have managed to achieve what I whanted by overriding {{Uberspect.getMethod()}} and implementing custom JexlMethod class

{code}

   protected class CollectionJexlMethod implements JexlMethod {

      public CollectionJexlMethod() {
      }

      @Override
      public Object invoke(Object obj, Object... params) throws Exception {
         if (obj instanceof Collection && params != null && params.length == 1) {
            Collection c = (Collection) obj;
            int i = TypeCast.getInt(params[0]);

            for (Object o : c) {
               if (i-- == 0)
                 return o;
            }
         }

         return null;
      }

      @Override
      public Object tryInvoke(String name, Object obj, Object... params) {

         if (obj instanceof Collection && "call".equals(name) && params != null && params.length == 1) {

            Collection c = (Collection) obj;
            int i = TypeCast.getInt(params[0]);

            for (Object o : c) {
               if (i-- == 0)
                 return o;
            }
         }

         return JexlEngine.TRY_FAILED;
      }

      @Override
      public boolean tryFailed(Object rval) {
         return rval == JexlEngine.TRY_FAILED;
      }

      @Override
      public boolean isCacheable() {
         return true;
      }

      @Override
      public Class<?> getReturnType() {
         return Object.class;
      }
   }

   @Override
   public JexlMethod getMethod(Object obj, String method, Object... args) {

      JexlMethod result = super.getMethod(obj, method, args);

      if (result == null) {

         if (obj instanceof Collection && "call".equals(method) && args != null && args.length == 1) {
           result = new CollectionJexlMethod();
         }
      }
      return result;
   }

{code}

The code is not as eloquent as it would be with JEXL-224 implemented, but it seems to work and I don't know if the issue is still worth fixing

> The ability to overload call() operator in customized JexlArithmetic implementation
> -----------------------------------------------------------------------------------
>
>                 Key: JEXL-224
>                 URL: https://issues.apache.org/jira/browse/JEXL-224
>             Project: Commons JEXL
>          Issue Type: Improvement
>    Affects Versions: 3.1
>            Reporter: Dmitri Blinov
>            Priority: Minor
>
> As of now there is a possibility to overload some operators and property accessors in JexlArithmetic, such as {{[]}}, {{.}} and {{~=}}, which is great extension point of Jexl. Suppose I want to implement index-based access to a {{j.u.Collection}} instance in a manner that {{a(0)}} would return the first element of collection, {{a(1)}} would return the second and so forth. This is different from using common access operator {{[]}} in a way that I specify not the index key of for example ArrayList, but an ordered index of the element. Of course, the is a possibility to introduce some function to this, but for the sake of the scripting simplicity I'm looking for the short version, like the call operator {{()}}. As of now I can only implement this behavior for the classes that are written by me, via adding a call() method to class declaration.But in the case of java.util.Collection this is not possible. So it would be great if Jexl could allow to overload a call() method in JexlArithmetic class in a way the propertyGet/arrayGet methods are now can be overloaded.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)