You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Matteo Trotta (Created) (JIRA)" <ji...@apache.org> on 2012/01/15 19:53:39 UTC

[jira] [Created] (JEXL-125) Unable to invoke method with ObjectContext

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

        

[jira] [Commented] (JEXL-125) Unable to invoke method with ObjectContext

Posted by "Henri Biestro (Commented) (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/JEXL-125?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13186801#comment-13186801 ] 

Henri Biestro commented on JEXL-125:
------------------------------------

This indeed is not a bug; the JexlContext only solves variables. To solve 'functions', you need a NamespaceResolver.
In case Maurizio's solution is not generic enough, you need an ObjectContext that implements NamespaceResolver. 
Unfortunately, the wrapped object in ObjectContext is not accessible (private) so some duplication/copy/paste is needed.
One solution can thus be the following:
{code:title=TestFoo125.java|borderStyle=solid}

    public static class Foo125 {
        public String method() {
            return "OK";
        }
    }

    public static class Foo125Context implements JexlContext, NamespaceResolver {
        private final JexlEngine jexl;
        private final Foo125 object;
        
        @Override
        public Object resolveNamespace(String name) {
            if (name == null) {
                return object;
            } else {
                return null;
            }
        }

        public Foo125Context(JexlEngine engine, Foo125 wrapped) {
            this.jexl = engine;
            this.object = wrapped;
        }

        public Object get(String name) {
            return jexl.getProperty(object, name);
        }

        public void set(String name, Object value) {
            jexl.setProperty(object, name, value);
        }

        public boolean has(String name) {
            return jexl.getUberspect().getPropertyGet(object, name, null) != null;
        }
    }

    public void test125() throws Exception {
        JexlEngine jexl = new JexlEngine();
        Expression e = jexl.createExpression("method()");
        JexlContext jc = new Foo125Context(jexl, new Foo125());
        assertEquals("OK", e.evaluate(jc));
    }
{code}
                
> 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

        

[jira] [Resolved] (JEXL-125) Unable to invoke method with ObjectContext

Posted by "Henri Biestro (Resolved) (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/JEXL-125?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Biestro resolved JEXL-125.
--------------------------------

    Resolution: Not A Problem

Not an issue, however ObjectContext should expose the wrapped object as 'protected' (will do in 3.0).
                
> 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
>            Assignee: Henri Biestro
>
> 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

        

[jira] [Issue Comment Edited] (JEXL-125) Unable to invoke method with ObjectContext

Posted by "Maurizio Cucchiara (Issue Comment Edited) (JIRA)" <ji...@apache.org>.
    [ 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

        

[jira] [Commented] (JEXL-125) Unable to invoke method with ObjectContext

Posted by "Maurizio Cucchiara (Commented) (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/JEXL-125?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13186602#comment-13186602 ] 

Maurizio Cucchiara commented on JEXL-125:
-----------------------------------------

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

        

[jira] [Commented] (JEXL-125) Unable to invoke method with ObjectContext

Posted by "Matteo Trotta (Commented) (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/JEXL-125?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13186812#comment-13186812 ] 

Matteo Trotta commented on JEXL-125:
------------------------------------

Ok, at the moment I can use the solution proposed by Maurizio in Example 1 as workaround.

Perhaps this is more a feature request than a bug, but why not make ObjectContext already implement NamespaceResolver as in the example you proposed?

Then there would be no need to extend ObjectContext and access a protected variable, ObjectContext would do all the job.

Thank you very much
                
> 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
>            Assignee: Henri Biestro
>
> 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