Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Fixed
-
2.7.1
-
None
-
Any, with compiled expressions
Description
Related to OGNL-97, but somewhat more complex case: Referring to an object's property via bracket notation only works if we you use #this to denote the name of property, e.g. objectthis.propertyKey. Below a unit test that shows the interpreted expression, and the equivalent compiled expression that fails without #this (it evaluates to the object itself).
import java.util.HashMap;
import java.util.Map;
import org.apache.tapestry.services.impl.OgnlClassResolver;
import junit.framework.TestCase;
import ognl.ClassResolver;
import ognl.Node;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.enhance.ExpressionAccessor;
public class OgnlTest extends TestCase {
private TestObject testObject = new TestObject("propertyValue");
public class TestObject {
String property;
public TestObject(String property)
{ this.property = property; }public String getProperty()
{ return property; }}
public TestObject getObject()
{ return testObject; }public String getPropertyKey()
{ return "property"; }public void testOgnl() throws OgnlException
{ assertEquals("propertyValue", Ognl.getValue("object[propertyKey]", this)); }public void testEnhancedOgnl() throws Exception
{ // Succeeds context = (OgnlContext)Ognl.createDefaultContext(this, ognlResolver); expression = Ognl.compileExpression(context, this, "object[#this.propertyKey]"); assertEquals("propertyValue", Ognl.getValue(expression.getAccessor(), context, this)) ; // Fails context = (OgnlContext)Ognl.createDefaultContext(this, ognlResolver); expression = Ognl.compileExpression(context, this, "object[propertyKey]"); assertEquals("propertyValue", Ognl.getValue(expression.getAccessor(), context, this)) ; }}