org.mvel2.MVEL.eval()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(324)

本文整理了Java中org.mvel2.MVEL.eval()方法的一些代码示例,展示了MVEL.eval()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MVEL.eval()方法的具体详情如下:
包路径:org.mvel2.MVEL
类名称:MVEL
方法名:eval

MVEL.eval介绍

[英]Evaluate an expression and return the value.
[中]对表达式求值并返回值。

代码示例

代码示例来源:origin: alibaba/jetcache

@Override
  public Object apply(Object context) {
    return MVEL.eval(script, context);
  }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

@Test
public void testBasicMVELFunctions() {
  //First, set up out functions
  HashMap<String, Object> functionMap = new HashMap<>();
  StringBuffer functions = new StringBuffer("def any(x, y) { return x or y } def all(x, y) { return x and y } ");
  MVEL.eval(functions.toString(), functionMap); //This stores that functions in the map we pass in.
  HashMap<String, Object> vars = new HashMap<>(functionMap); //Now, we need to pass the functions in to our variable map
  vars.put("fg", "Hello");
  StringBuffer expression = new StringBuffer();
  expression.append("return all(fg == 'Hello', true)");
  Boolean result = (Boolean)MVEL.eval(expression.toString(), vars);
  assert result != null && result;
  expression = new StringBuffer();
  expression.append("return any(fg == 'Goodbye', false)");
  Boolean result2 = (Boolean)MVEL.eval(expression.toString(), vars);
  assert result2 != null && ! result2;
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

.append("          return (orderContainsPlusMark(1) and currentItem.product.defaultCategory.name == 'hat');");
Boolean result = (Boolean)MVEL.eval(expression.toString(), vars);
assert result != null && result;

代码示例来源:origin: org.drools/drools-compiler

@Test
public void testMVELPopulate() throws Exception {
  Object q = MVEL.eval("new " + DumbFact.class.getName() + "()");
  Map m = new HashMap();
  m.put("obj", q);
  m.put("val", "mike");
  MVEL.eval("obj.name = val", m);
  m = new HashMap();
  m.put("obj", q);
  m.put("val", "42");
  MVEL.eval("obj.age = val", m);
  m = new HashMap();
  m.put("obj", q);
  m.put("val", "44");
  MVEL.eval("obj.number = val", m);
  DumbFact d = (DumbFact) q;
  assertEquals("mike", d.getName());
  assertEquals(42, d.getAge());
  assertEquals(new Long(44), d.getNumber());
}

代码示例来源:origin: org.mvel/mvel2

/**
 * Evaluate an expression in Boolean-only mode against a root context object and injected variables.
 *
 * @param expression A string containing the expression to be evaluated.
 * @param ctx        The context against which to evaluate the expression
 * @param vars       The variables to be injected
 * @return The resultant value as a Boolean
 */
public static Boolean evalToBoolean(String expression, Object ctx, Map<String, Object> vars) {
 return eval(expression, ctx, vars, Boolean.class);
}

代码示例来源:origin: org.mvel/mvel2

/**
 * Evaluates an expression and returns the resultant value as a String.
 *
 * @param expression A string containing the expressino to be evaluated.
 * @return The resultant value
 */
public static String evalToString(String expression) {
 return valueOf(eval(expression));
}

代码示例来源:origin: org.drools/drools-compiler

final String name = (String) MVEL.eval("$t.name", Collections.singletonMap("$t", value));

代码示例来源:origin: org.mvel/mvel2

public void testProtoFieldAccess() {
 if (!run) return;
 Object o = MVEL.eval("proto Person { int age = 5; String name; }; (p = new Person()).age",
   new HashMap<String, Object>());
 assertEquals(5, o);
}

代码示例来源:origin: org.mvel/mvel2

public void testShouldFail5() {
 try {
  MVEL.eval("[");
 }
 catch (Exception e) {
  return;
 }
 shouldThrowException();
}

代码示例来源:origin: org.mvel/mvel2

public void testShouldFail11() {
 try {
  MVEL.eval("for (int i = 0; i--; i++) {}");
 }
 catch (Exception e) {
  e.printStackTrace();
  return;
 }
 shouldThrowException();
}

代码示例来源:origin: org.mvel/mvel2

public void testMath5a() {
 String expression = "300.5 / 5.3 / 2.1 / 1.5";
 System.out.println("Expression: " + expression);
 assertEquals(300.5 / 5.3 / 2.1 / 1.5, MVEL.eval(expression));
}

代码示例来源:origin: org.mvel/mvel2

public void testMath35_Interpreted() {
 String expression = "b/x/b/b*y+a";
 Map map = new HashMap();
 map.put("a", 10);
 map.put("b", 20);
 map.put("c", 30);
 map.put("x", 40);
 map.put("y", 50);
 map.put("z", 60);
 assertNumEquals(20d / 40d / 20d / 20d * 50d + 10d, MVEL.eval(expression, map));
}

代码示例来源:origin: org.mvel/mvel2

public void testJIRA99_Interpreted() {
 Map map = new HashMap();
 map.put("x",
   20);
 map.put("y",
   10);
 map.put("z",
   5);
 assertEquals(20 - 10 - 5,
   MVEL.eval("x - y - z",
     map));
}

代码示例来源:origin: org.mvel/mvel2

public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) {
  VariableResolver resolver = factory.getIndexedVariableResolver(register);
  resolver.setValue(ctx = MathProcessor.doOperations(resolver.getValue(), operation, eval(expr, start, offset, ctx, factory)));
  return ctx;
 }
}

代码示例来源:origin: org.mvel/mvel2

public void testCallGlobalStaticFunctionFromMVELFunction() {
 TestClassAZZ azz = new TestClassAZZ();
 String expr = "def foobie12345() { hey(); } foobie12345();";
 assertEquals("Heythere!", MVEL.eval(expr, azz, new HashMap<String, Object>()));
}

代码示例来源:origin: org.mvel/mvel2

public void testStaticTyping2() {
 String exp = "int x = 5; int y = 2; new int[] { x, y }";
 int[] res = (int[]) MVEL.eval(exp, new HashMap());
 assertEquals(5, res[0]);
 assertEquals(2, res[1]);
}

代码示例来源:origin: org.mvel/mvel2

public void testShouldFail4() {
 try {
  MVEL.eval("hour zzz", createTestMap());
 }
 catch (Exception e) {
  return;
 }
 shouldThrowException();
}

代码示例来源:origin: org.mvel/mvel2

public void testFunctionPointer() {
  String ex = "squareRoot = java.lang.Math.sqrt; squareRoot(4)";

  Object o = MVEL.eval(ex, new HashMap());

  assertEquals(2.0, o);

  assertEquals(2.0, test(ex));
 }
/*

代码示例来源:origin: org.mvel/mvel2

public Object eval(TemplateRuntime runtime, TemplateOutputStream appender, Object ctx, VariableResolverFactory factory) {
 if (runtime.getNamedTemplateRegistry() == null) {
  runtime.setNamedTemplateRegistry(new SimpleTemplateRegistry());
 }
 runtime.getNamedTemplateRegistry()
   .addNamedTemplate(MVEL.eval(contents, cStart, cEnd - cStart, ctx, factory, String.class),
     new CompiledTemplate(runtime.getTemplate(), nestedNode));
 return next != null ? next.eval(runtime, appender, ctx, factory) : null;
}

代码示例来源:origin: org.mvel/mvel2

public void testProjectionSupport3() {
 String ex = "(toUpperCase() in ['bar', 'foo'])[1]";
 Map vars = createTestMap();
 assertEquals("FOO", MVEL.eval(ex, new Base(), vars));
 assertEquals("FOO", test("(toUpperCase() in ['bar', 'foo'])[1]"));
}

相关文章