提问人:Rinke 提问时间:11/3/2023 更新时间:11/18/2023 访问量:42
脚本中的所有 Jexl 对象在任何方法调用时都返回 null
all Jexl objects in scripts return null on any method call
问:
我正在尝试让 jexl 脚本工作。
// arranging namespace Print
Map<String, Object> ns = new HashMap<>(1);
ns.put("Print", System.out); // make System.out.println available in expressions
JexlEngine je = new JexlBuilder().namespaces(ns).create();
// arranging custom Map for accessible variable 'agent' in script
Map<String, Object> myMap = ....;
myMap.put("agent", agentRef);
JexlContext jc = new MapContext(myMap);
// arranging script
JexlScript script = je.createScript(expression);
Object result = script.execute(jc);
表达式是这样的:
{
Print:println("blabla");
const x = agent.getId();
Print:println(x);
return true;
}
第一个命令只是将“blabla”打印到控制台,因此通过 System.out.println 进行打印可以完美无缺。
但是,对于变量代理:它确实存在,因为 jexl 不报告“不可解的变量”。但是,我尝试调用的代理对象的任何方法总是返回 null。 我只是在控制台上打印了一个 null。 当我调试时,agentRef 的字段上确实有 null 以外的值,就在我将其放入 Map 之前。因此,字段不为空。
此外,当我调用 agent.id(这应该是可能的,它只是一个 POJO)时,我得到“未定义的属性'id',假设为 false”。而 agent.getId() 只返回 null。
有人知道这里会出什么问题吗?
我尝试了其他语法,但无济于事。我尝试使用jc.set(“agent”, agentRef),但结果相同。
答:
0赞
henrib
11/18/2023
#1
JEXL 只会通过权限查看/内省允许的公共类;必须允许使用您自己的类/包(默认情况下不允许使用),并且它们必须是公共的。 若要了解如何创建适当的权限并避免在生产中使用 JexlPermissions.UNLIMITEDED,请参阅 JexlPermissions
评论
new JexlBuilder().permissions(JexlPermissions.UNRESTRICTED)...
agentRef
public
agent.id
getId()
public
)