Programmatic Imports for 2.0
By default, like Java, MVEL provides all java.lang.* classes
pre-imported as part of the compiler and runtime. However, MVEL provides the
ability to programmatically import individual classes, entire packages, and even
static methods.
Taking advantage of this means taking a bit of a journey away from the cozy
org.mvel.MVEL convenience class, but it's nothing too absurd.
Importing
Classes
Consider the following:
ParserContext context = new ParserContext();
context.addImport("Message", Message.class);
context.addImport("MessageFactory", MessageFactory.class);
Serializable compiled = MVEL.compileExpression(someExpression, context);
In this example we add two imports for both the classes Message and
MessageFactory. It's important to mention, as some of you may be
wondering, that it is in fact possible to alias classes here, rather than using
their real names. For example:
...
context.addImport("Utils", ScriptUtilities.class);
...
In this case, the compiler will resolve the token Utils as the
ScriptUtilities class. This can be handy for cases where you may be
implementing a DSL on top of MVEL, and it is not your intention to expose your
implementation, but rather provide an easy, short-hand API.
Importing
Packages
Package imports can be accomplished through the addPackageImport
method in ParserContext. Example:
ParserContext ctx = new ParserContext();
ctx.addPackageImport("java.util");
Serializable s = MVEL.compileExpression("map = new HashMap();", ctx);
Importing
Static Methods
You can import arbitrary static methods from any Java class within the
classpath so that it is accessible to the compiler, and at runtime.
ParserContext ctx = new ParserContext();
try {
ctx.addImport("time", System.class.getMethod("currentTimeMillis", long.class));
}
catch (NoSuchMethodException e) {
}
Serializable s = MVEL.compileExpression("time();" ctx);
As you may see from the example, static method imports are represented by a
java.lang.reflect.Method object, so you will need to use the Reflection
API to handle this. We know it's not as pretty and elegant as all the other
integration code, but this really is the best way.
The more cool part of this, is that we have imported the
System.currentTimeMillis() method and aliased it to the global function
time() in our script. Once again, a really useful way to aggregate
disparate static methods as utility functions into your scripts.
分享到:
相关推荐
MVEL 2.0表达式语言MVEL 2.0表达式语言MVEL 2.0表达式语言
mvel14-1.2.21.jar mvel14-1.2.21.jar
mvel2.0语法指南中文版
mvel2.0语法指南,MVEL通常用于执行用户(程序员)通过配置XML文件或注释等定义的基本逻辑。它也可以用来解析简单的JavaBean表达式。Runtime(运行时)允许MVEL表达式通过解释执行或者预编译生成字节码后执行。
很好的MVEL基础语法学习资料,希望能帮到你~
NULL 博文链接:https://yanguz123.iteye.com/blog/2146176
MVEL is very easy to use, and just as easy to integrate into your application. Let's take a quick look at a simple MVEL expression: foo.name == "Mr. Foo" This simple expression asks MVEL if the value...
功能强大的基于Java应用程序的表达式语言.
mvel2-2.1.6.Final-javadoc
java运行依赖jar包
mvel-maven-插件 使用 MVEL 渲染模板的 Maven 插件。 用法 < groupId>uk.co.codezen < artifactId>mvel-maven-plugin < version>1.0 < goal>render < template
mvel2-2.1.0.drools16.jar mvel2-2.1.0.drools16.jar
MVEL is an expression language – similar to OGNL – and a templating engine. I’d like to give you an example of MVEL Templates in this post so you can find out if MVEL might work for you. Templating...
介绍 这是一个用于测试表达式的简单 Repl(Read-Eval-Print-Loop)。 用法 由于这是一个简单的 Maven 项目,因此可以使用 > mvn install 项目配置了Maven exec插件,直接调用即可启动repl > mvn exec:java
数值表达式,可执行算术运算公式。 例如:(100 – 5) * 14/6 <br/>