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 2019/08/12 09:20:00 UTC

[jira] [Commented] (JEXL-310) Define Alias or synonyms for Methods with parameters and accesible through Jexl Context

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

Henri Biestro commented on JEXL-310:
------------------------------------

Two fast solutions:
- derive the MapContext
- set a duck-typed callable, ie an object with a call method - in the context

{code:java}

    public static class Context310 extends MapContext {
        public int $$plus(int x, int y) {
            return x + y;
        }
    }
    
    public static class Multiply310 {
        public int call(int x, int y) {
            return x * y;
        }
    }
        
    @Test
    public void testIssue310() throws Exception {
        JexlEngine jexl = new JexlBuilder().safe(true).create();
        JexlContext context = new Context310();
        context.set("$$mult", new Multiply310());
        Object result = jexl.createScript("$$plus(10, 32)").execute(context);
        Assert.assertEquals(42, result);
        result = jexl.createScript("$$mult(6, 7)").execute(context);
        Assert.assertEquals(42, result);
    }

{code}

Other solution could be using a sandbox.

> Define Alias or synonyms for Methods with parameters and accesible through Jexl Context 
> ----------------------------------------------------------------------------------------
>
>                 Key: JEXL-310
>                 URL: https://issues.apache.org/jira/browse/JEXL-310
>             Project: Commons JEXL
>          Issue Type: Wish
>    Affects Versions: 3.1
>            Reporter: Osy
>            Priority: Trivial
>
> I have a static method:
> *Util.round : public static Object round(Object number, Object places)*
> I can access this Method within an expression adding the class to the context
> *jexlContext.set("util", new Util());*
> And using an expression as *:  util.round(34.678,2)***
> However -within the jexl expression - I don't want to access the method round as :
> *util.round(34.678,2),* instead I want to use the method with my own name:
> +*$$ROUND**(34.678,2)*+**
> It is possible?
>  
>  
>  
>  
>  



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)