无法使用 MVEL 导入静态方法
根据 MVEL 的文档,可以在脚本中导入静态 java 方法: http://mvel .codehaus.org/Programmatic+Imports+for+2.0。以下示例取自该页面,但不起作用(我收到错误:无法访问属性(空父级):时间)。可能出什么问题了?
import java.io.Serializable;
import org.mvel2.MVEL;
import org.mvel2.ParserContext;
public class Test {
public static void main(String[] args) {
ParserContext ctx = new ParserContext();
try {
ctx.addImport("time", System.class.getMethod("currentTimeMillis", long.class));
}
catch (NoSuchMethodException e) {
// handle exception here.
}
Serializable s = MVEL.compileExpression("time();", ctx);
Object ans = MVEL.executeExpression(s);
System.out.println(ans.toString());
}
}
According to MVEL's documentation, it's possible to import static java methods in a script: http://mvel.codehaus.org/Programmatic+Imports+for+2.0 . The following example is taken from that page, however is not working (I get an Error: unable to access property (null parent): time). What could be wrong?
import java.io.Serializable;
import org.mvel2.MVEL;
import org.mvel2.ParserContext;
public class Test {
public static void main(String[] args) {
ParserContext ctx = new ParserContext();
try {
ctx.addImport("time", System.class.getMethod("currentTimeMillis", long.class));
}
catch (NoSuchMethodException e) {
// handle exception here.
}
Serializable s = MVEL.compileExpression("time();", ctx);
Object ans = MVEL.executeExpression(s);
System.out.println(ans.toString());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getMethod 的第二个参数用于参数类型,它不用于方法的返回类型。
改变这一行:
这样:
getMethod's second argument used for parameter types, its not used for return type of the method.
change this line:
with this: