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支持多种编程概念,如变量、函数、控制结构(条件语句、循环)以及对象和数组操作。 ### 1. MVEL的核心特性 - **简洁的语法**: MVEL的语法简洁明了,易于理解和编写。它可以处理简单的数学运算、字符串操作和...
6. **函数和闭包**:可以定义匿名函数,或者引用已存在的方法,进行函数式编程。 7. **控制结构**:包括条件语句(如`if-else`)和循环(如`for`和`while`)。 8. **字符串操作**:支持字符串连接、格式化和模板化。...
### MVEL 2.0 语法指南精要 #### 一、引言 MVEL (Micro Velocity) 是一种高效、灵活且易于使用的表达式语言,主要用于处理数据和执行计算任务。MVEL 2.0 版本在继承 Java 语法的基础上进行了大量优化,以提高性能...
MVEL (Mvel Expression Language) 是一个轻量级的脚本语言,主要应用于Java环境,用于执行用户在配置文件或注解中定义的简单逻辑。MVEL 2.0是其一个版本,提供了更加强大的功能和改进。下面将详细介绍MVEL 2.0的使用...
mvel14-1.2.21.jar mvel14-1.2.21.jar
很好的MVEL基础语法学习资料,希望能帮到你~
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应用程序的表达式语言.
java运行依赖jar包
mvel2-2.1.6.Final-javadoc
mvel2-2.1.0.drools16.jar mvel2-2.1.0.drools16.jar
mvel-maven-插件 使用 MVEL 渲染模板的 Maven 插件。 用法 < groupId>uk.co.codezen < artifactId>mvel-maven-plugin < version>1.0 < goal>render < template
MVEL模板允许开发者以一种声明式的方式将动态数据嵌入到静态文本中,常用于生成HTML或者其他格式的输出。 **基本对象访问** 在MVEL模板中,可以非常直观地访问对象的属性。例如,`<h1>@{name}</h1>` 这行代码将会...
在IT领域,编程语言是构建复杂系统的基础工具,而Java作为一种广泛应用的面向对象的语言,其在各种场景下都有着广泛的应用。本话题聚焦于Java编程艺术中的一个关键环节——表达式解析器,它允许我们处理和求值数学或...
Easy Rules 是一个开源项目,它的主要目标是提供一种易于使用、可扩展且非侵入式的规则引擎,使得开发者可以轻松地在 Java 应用程序中定义和执行业务规则。该框架允许开发者通过简单的 Java API 定义规则,并将它们...