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

Maurizio Cucchiara edited comment on JEXL-125 at 1/15/12 10:07 PM:
-------------------------------------------------------------------

I'm not a JEXL guru, but looking at the source code, I guess that the code 
fragment you provided is not right (see below examples).

{code:title=Example 1}
    public void test() throws Exception {
        JexlEngine jexl = new JexlEngine();
        jexl.setStrict(true);
        JexlContext jc = new ObjectContext<Foo>(jexl, new Foo());
        Assert.assertEquals("OK", jc.get("method()"));
    }
{code}
{code:title=Example 2}
    @Test
    public void test() throws Exception {
        JexlEngine jexl = new JexlEngine();
        jexl.setStrict(true);
        Expression e = jexl.createExpression("method()");
        JexlContext jc = new FooContext(jexl, new Foo());
        Assert.assertEquals("FOOBAR", e.evaluate(jc));
    }

    static public class FooContext extends ObjectContext<Foo> {
        FooContext(JexlEngine jexl, Foo foo) {
            super(jexl, foo);
        }

        public String method() {
            return "FOOBAR";
        }
    }
{code}
Just another side note: when you are not sure if you are facing a bug or not, I 
strongly advise to ask to [the user 
ML|http://commons.apache.org/jexl/mail-lists.html] 

HTH
                
      was (Author: maurizio.cucchiara):
    I'm not a JEXL guru, but looking at the source code, I guess that the code 
fragment you provided is not right (see below examples).

{code:title=Example 1}
    public void test() throws Exception {
        JexlEngine jexl = new JexlEngine();
        jexl.setStrict(true);
        Expression e = jexl.createExpression("method()");
        JexlContext jc = new ObjectContext<Foo>(jexl, new Foo());
        Assert.assertEquals("OK", jc.get("methodA()"));
    }
{code}
{code:title=Example 2}
    @Test
    public void test() throws Exception {
        JexlEngine jexl = new JexlEngine();
        jexl.setStrict(true);
        Expression e = jexl.createExpression("method()");
        JexlContext jc = new FooContext(jexl, new Foo());
        Assert.assertEquals("FOOBAR", e.evaluate(jc));
    }

    static public class FooContext extends ObjectContext<Foo> {
        FooContext(JexlEngine jexl, Foo foo) {
            super(jexl, foo);
        }

        public String method() {
            return "FOOBAR";
        }
    }
{code}
Just another side note: when you are not sure if you are facing a bug or not, I 
strongly advise to ask to [the user 
ML|http://commons.apache.org/jexl/mail-lists.html] 

HTH
                  
> Unable to invoke method with ObjectContext
> ------------------------------------------
>
>                 Key: JEXL-125
>                 URL: https://issues.apache.org/jira/browse/JEXL-125
>             Project: Commons JEXL
>          Issue Type: Bug
>    Affects Versions: 2.1.1
>         Environment: Java 1.6.0_20 on Windows 7
>            Reporter: Matteo Trotta
>
> Hi, I'm trying to invoke a method on Object context but I can't get it to 
> work.
> I don't know if it's a bug or I'm doing it wrong.
> Here it is the code I'm using:
> {code:title=JexlTest.java}
> package it.test;
> import org.apache.commons.jexl2.Expression;
> import org.apache.commons.jexl2.JexlContext;
> import org.apache.commons.jexl2.JexlEngine;
> import org.apache.commons.jexl2.ObjectContext;
> import org.junit.Test;
> public class JexlTest {
>     public static class Foo {
>         public String method() {
>             return "OK";
>         }
>     }
>     @Test
>     public void test() throws Exception {
>         JexlEngine jexl = new JexlEngine();
>         jexl.setStrict(true);
>         Expression e = jexl.createExpression("method()");
>         JexlContext jc = new ObjectContext<Foo>(jexl, new Foo());
>         System.out.println(e.evaluate(jc));
>     }
> }
> {code}
> Here is the exception I'm getting:
> {noformat}
> org.apache.commons.jexl2.JexlException: it.test.JexlTest.test@19![0,8]: 
> 'method();' method error
>       at org.apache.commons.jexl2.Interpreter.call(Interpreter.java:1078)
>       at org.apache.commons.jexl2.Interpreter.visit(Interpreter.java:1100)
>       at 
> org.apache.commons.jexl2.parser.ASTMethodNode.jjtAccept(ASTMethodNode.java:18)
>       at org.apache.commons.jexl2.Interpreter.visit(Interpreter.java:1317)
>       at 
> org.apache.commons.jexl2.parser.ASTReference.jjtAccept(ASTReference.java:18)
>       at org.apache.commons.jexl2.Interpreter.interpret(Interpreter.java:232)
>       at 
> org.apache.commons.jexl2.ExpressionImpl.evaluate(ExpressionImpl.java:65)
>       at it.test.JexlTest.test(JexlTest.java:21)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>       at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
>       at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
>       at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
>       at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
>       at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:274)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
>       at org.junit.runners.ParentRunner$3.run(ParentRunner.java:242)
>       at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:58)
>       at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:240)
>       at org.junit.runners.ParentRunner.access$000(ParentRunner.java:48)
>       at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:233)
>       at org.junit.runners.ParentRunner.run(ParentRunner.java:303)
>       at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
>       at 
> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
>       at 
> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
>       at 
> com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>       at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
> Caused by: org.apache.commons.jexl2.JexlException$Property: 
> org.apache.commons.jexl2.ObjectContext.get@42![0,9]: '#0.method;' 
> inaccessible or unknown property #0
>       at org.apache.commons.jexl2.Interpreter.visit(Interpreter.java:1341)
>       at 
> org.apache.commons.jexl2.parser.ASTReference.jjtAccept(ASTReference.java:18)
>       at org.apache.commons.jexl2.JexlEngine.getProperty(JexlEngine.java:615)
>       at org.apache.commons.jexl2.JexlEngine.getProperty(JexlEngine.java:587)
>       at org.apache.commons.jexl2.ObjectContext.get(ObjectContext.java:42)
>       at org.apache.commons.jexl2.Interpreter.call(Interpreter.java:1047)
> {noformat}
> Using a MapContext with variable "foo" set to 'new Foo()' and the expression 
> "foo.method()" gives no error.
> Anyway for my use it's more practical a ObjectContext, as I have only one 
> object in the context.
> Thank you for very much and keep up the excellent work!
> Matteo Trotta

--
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