[ 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