Can't import static method using MVEL
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 g开发者_Python百科et 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());
}
}
getMethod's second argument used for parameter types, its not used for return type of the method.
change this line:
System.class.getMethod("currentTimeMillis", long.class)
with this:
System.class.getMethod("currentTimeMillis")
精彩评论