MVELのドキュメントによると、静的Javaメソッドをスクリプトにインポートすることが可能です:http://mvel.codehaus.org/Programmatic+Imports+for+2.0。次の例はそのページから抜粋したものですが、機能していません(エラーが発生しました:プロパティにアクセスできません(nullの親):時間)。何が間違っている可能性がありますか?
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());
}
}