@Override
public Object executeExpr(final String expression) throws Exception {
// 从缓存中获取解析的表达式
Expression expr = cache.get(expression);
if (expr == null) {
//
StringBuilder builder = new StringBuilder(expression.length() * 2);
// 将[name]替换为['name']
Matcher mapMatcher = MAP_PATTERN.matcher(expression);
int index = 0;
while (mapMatcher.find()) {
builder.append(expression.substring(index, mapMatcher.start()));
String t = mapMatcher.group(1);
if (!NumberUtils.isDigits(t)) {
builder.append("['");
builder.append(mapMatcher.group(1));
builder.append("']");
} else {
builder.append(mapMatcher.group(0));
}
index = mapMatcher.end();
}
String expression2;
if (builder.length() == 0) {
expression2 = expression;
} else {
builder.append(expression.substring(index));
expression2 = builder.toString();
builder.setLength(0);
}
index = 0;
// 匹配正则表达式, 并替换内容
Matcher matcher = PREFIX_PATTERN.matcher(expression2);
while (matcher.find()) {
builder.append(expression2.substring(index, matcher.start()));
String prefix = matcher.group(1);
String name = matcher.group(2);
if (":".equals(prefix)) {
boolean isDigits = NumberUtils.isDigits(name);
if (isDigits) {
// 按顺序访问变量
name = ':' + name;
}
if (!mapVars.containsKey(name)) {
throw new IllegalArgumentException("Variable \'" + name
+ "\' not defined in DAO method");
}
// 按名称访问变量
builder.append(VAR_PREFIX);
builder.append("['");
builder.append(name);
builder.append("']");
} else if ("$".equals(prefix)) {
if (!mapConsts.containsKey(name)) {
throw new IllegalArgumentException("Constant \'" + name
+ "\' not defined in DAO class");
}
// 拼出常量访问语句
builder.append(CONST_PREFIX);
builder.append("[\'");
builder.append(name);
builder.append("\']");
}
index = matcher.end(2);
}
builder.append(expression2.substring(index));
// 编译表达式
expr = ExpressionFactory.createExpression(builder.toString());
cache.putIfAbsent(expression2, expr);
}
// 进行表达式求值
return expr.evaluate(context);
}