Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 0.75 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I expose constant values in MVEL?
  2. public static final String PRODUCT_NAME;
  3.        
  4. "Thanks for using " + PRODUCT_NAME
  5.        
  6. ParserContext ctx = ParserContext.create();
  7. ctx.addImport(YourClass.class);
  8.  
  9. String expr = "YourClass.PRODUCT_NAME";
  10. Serializable compiled = MVEL.compileExpreasion(expr, ctx);
  11. Object result = MVEL.executeExpression(compiled);
  12.        
  13. public static final String PRODUCT_NAME = "My cool product";
  14. public static final String getProductName() {
  15.    return PRODUCT_NAME;
  16. }
  17.        
  18. public class Product{
  19.    public static final String PRODUCT_NAME = "TEST";
  20. }
  21.        
  22. import org.sample.Product;
  23.  
  24. rule "Test"
  25.     no-loop true
  26.     lock-on-active true
  27.     when
  28.        eval(true);
  29.     then
  30.         System.out.println(Product.PRODUCT_NAME); #prints TEST
  31.  
  32.   end