Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supplier type parameter function can import to ParserContext, But expression complile fail and can not use it? #338

Open
linux2014linux opened this issue Sep 7, 2023 · 0 comments

Comments

@linux2014linux
Copy link

linux2014linux commented Sep 7, 2023

I add success custom method to ParserContext. But It complie fail it when I use expression.

public class LogicFunction {
  public static Object IFERROR(Supplier<Object> expression, Object defaultValue) {
      try {
          return expression.get();
      } catch (Throwable e) {
          return defaultValue;
      }
  }
}
public class MvelTest {
  @Test
  public void test() {
    ParserContext context = new ParserContext();
    Method[] declaredMethods = LogicFunction.class.getDeclaredMethods();
    for (Method declaredMethod : declaredMethods) {
        context.addImport(declaredMethod.getName(), declaredMethod);
    }

    Map<String, Object> data = new HashMap();
    Serializable serializable = MVEL.compileExpression("IFERROR(() -> 1/0, -1)", context);
    Object fieldValue = MVEL.executeExpression(serializable, data);
    System.out.println(fieldValue);
  }
}

Follow error information.

[Error: Malformed expression]
[Near : {... IFERROR(() -> 1/0, -1) ....}]

Anyone help me? Thank you!

@linux2014linux linux2014linux changed the title How import supplier parameter function to mvel? How import Supplier type parameter function to mvel? Sep 7, 2023
@linux2014linux linux2014linux changed the title How import Supplier type parameter function to mvel? Supplier type parameter function can import to ParserContext, But expression complile fail and can not use it? Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant