我有数据对象,你可以把它看作是一个“简化的地图”。有一些方法是get(String)和put(String,Object),但基本上就是这样。
现在,我想使用JEXL计算数据对象上的复杂表达式。我可以通过创建一个自定义JexlContext来做到这一点,它适用于像"foo“或foo != null这样的表达式。但是,当我尝试使用像"foo.bar“这样的表达式时,Jexl就会失败,并会出现错误消息"unsolvable”。显然,Jexl使用我的自定义JexlContext来计算" foo ",但不能计算foo对象上的"bar“。我的印象是,我必须使用自定义PropertyResolver。我可以实现,但我不知道。如何将其引入游戏,因为JexlUberspect不包含像setResolvers或addResolver这样的方法。
发布于 2020-03-11 12:13:45
类似于重复的问题,我想你可以:
public class ExtendedJexlArithmetic extends JexlArithmetic
{
public Object propertyGet(YourCustomClass left, YourCustomClass right)
{
// ...
}
}
JexlEngine jexlEngine=new JexlBuilder().arithmetic(new ExtendedJexlArithmetic (true)).create();
// or
JexlEngine jexl = new JexlEngine(null, new ExtendedJexlArithmetic(), null, null);来自于:https://commons.apache.org/proper/commons-jexl/apidocs/org/apache/commons/jexl3/package-summary.html的文档
还可以向重载属性getter和setters运算符行为添加方法。名为propertyGet/propertySet/arrayGet/arraySet的JexlArithmetic实例的公共方法是在适当情况下调用的潜在重写。下表概述了语法形式与调用方法之间的关系,其中V是属性值类,O是对象类,P是属性标识符类(通常是String或Integer)。
表达式方法模板foo.property public V propertyGet(O obj,P属性);foo.property =值公共V propertySet(O obj,P属性,V值);fooproperty公共V arrayGet(O obj,P属性,V值);fooproperty =值公共V arraySet(O obj,P属性,V值);
https://stackoverflow.com/questions/60632217
复制相似问题