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

Henri Biestro commented on JEXL-140:
------------------------------------

Hi Georges;
Thank you for your report; I think I understand you use case but I'm not sure 
this warrants a new keyword.
As a general rule, I'm trying to avoid adding new syntactic elements to keep 
Jexl lean and I believe your issue can be tackled by existing means.
I'm also trying to keep "sandboxing" easy to configure (i.e. controlling 
exposition of classes/objects/methods to scripts).

So, before agreeing to the 'class()' method as you suggest, allow me to propose 
alternative solutions to your problem.
The gist of it is to use the ability to solve 'ant'-like vars from a 
JexlContext, either by pre-setting the class-name/class-instance or by 
implementing a JexlContext that solves class-names.
The following code illustrates both.
Let me know what you think.
Regards

{code}

    public static int run140(int i) {
        return 140 * i;
    }

    public static class ClassAwareContext extends MapContext {
        @Override
        public boolean has(String name) {
            try {
                return super.has(name) || Class.forName(name) != null;
            } catch (ClassNotFoundException xnf) {
                return false;
            }
        }

        @Override
        public Object get(String name) {
            try {
                Object found = super.get(name);
                if (found == null && !super.has(name)) {
                    found = Class.forName(name);
                }
                return found;
            } catch (ClassNotFoundException xnf) {
                return null;
            }
        }
    }

    @Test
    public void test140() throws Exception {
        JexlEngine jexl = new JexlBuilder().create();
        JexlContext jc = new ClassAwareContext();
        jc.set(IssuesTest.class.getName().toLowerCase(), IssuesTest.class);

        JexlScript script;
        script = 
jexl.createScript("org.apache.commons.jexl3.issuestest.run140(3)");
        Object result = script.evaluate(jc);
        assertEquals(420, result);

        script = jexl.createScript("java.lang.System.getenv('USER')");
        result = script.evaluate(jc);
        assertEquals("henri", result);
    }
{code} 
                
> Improvements for invoking static methods
> ----------------------------------------
>
>                 Key: JEXL-140
>                 URL: https://issues.apache.org/jira/browse/JEXL-140
>             Project: Commons JEXL
>          Issue Type: Improvement
>            Reporter: George Scott
>            Priority: Minor
>
> Two related suggestions:
> 1. The documentation does not really describe how to call a static method 
> from a JEXL script.  I believe that it is done this way:
> {noformat}
> class="".class;
> return class.forName("java.lang.System").getenv("test");
> {noformat}
> 2. Implementing a class() function that parallels the existing new() function 
> would make calling static methods easier, helping to clean up scripts.  The 
> above example would become:
> {noformat}
> return class("java.lang.System").getenv("test");
> {noformat}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to