gpt4 book ai didi

java - JEXL:如何自定义属性访问器

转载 作者:行者123 更新时间:2023-12-01 18:00:52 25 4
gpt4 key购买 nike

我有数据对象,您可以将其视为“简化的 map ”。有方法 get(String)put(String,Object),但基本上就是这样。

现在,我想使用 JEXL 来计算数据对象上的复杂表达式。我可以通过创建自定义 JexlContext 来实现此目的,这适用于 "foo"foo != null 等表达式。然而,一旦我尝试使用像“foo.bar”这样的表达式,Jexl 就会失败并显示错误消息“无法解析的属性”。显然,Jexl 使用我的自定义 JexlContext 来评估 "foo",但无法评估 foo 对象上的 "bar"。我的印象是,我必须使用自定义 PropertyResolver。我可以实现它,但我无法弄清楚。如何将其引入游戏中,因为 JexlUberspect 不包含像 setResolversaddResolver 这样的方法。

最佳答案

与重复的问题类似,我想你可以这样做:

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

You can also add methods to overload property getters and setters operators behaviors. Public methods of the JexlArithmetic instance named propertyGet/propertySet/arrayGet/arraySet are potential overrides that will be called when appropriate. The following table is an overview of the relation between a syntactic form and the method to call where V is the property value class, O the object class and P the property identifier class (usually String or Integer).

Expression                   Method Template
foo.property public V propertyGet(O obj, P property);

foo.property = value public V propertySet(O obj, P property, V value);

foo[property] public V arrayGet(O obj, P property, V value);

foo[property] = value public V arraySet(O obj, P property, V value);

关于java - JEXL:如何自定义属性访问器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60632217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com