javajexl

JexlEngine constructor not working? (cannot be applied to given types)


I am trying to initialize an JexlEngine object, but the constructor does not let me do so (although the documentation states it should).

Here's the documentation for the JexlEngine class (in jexl3): https://people.apache.org/~henrib/jexl-3.0/apidocs/org/apache/commons/jexl3/JexlEngine.html

Originally the code worked with the jexl2 import, but I have recently converted the project to Maven, and had to swap out to jexl3 instead. Now the constructor no longer works.

Am I missing anything?

I am running this project in Netbeans, on Java 1.8 - it's a Maven project with included dependancies for jexl3 (used to work with jexl2 however)

My code:

public static final JexlEngine jexl = new JexlEngine(null, new MyArithmetic(), null, null){};

static {
        jexl.setCache(512);
        jexl.setLenient(false); // null shouldnt be treated as 0
        jexl.setSilent(false);  // Instead of logging throw an exception
        jexl.setStrict(true);
}

Based off the documentation, there should be a constructor with 4 parameters, as I am trying to run it, but for some strange reason, it wont let me run it. Any ideas why? (again - it used to work with Jexl2)

Error log:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project bilbon-core: Compilation failure: Compilation failure:
si/smth/project/bean/CUtil.java:[333,43] constructor JexlEngine in class org.apache.commons.jexl3.JexlEngine cannot be applied to given types;
required: no arguments
found: <nulltype>,si.smth.project.bean.CUtil.MyArithmetic,<nulltype>,<nulltype>
reason: actual and formal argument lists differ in length
si/smth/project/bean/CUtil.java:[333,99] <anonymous si.smth.project.bean.CUtil$1> is not abstract and does not override abstract method newInstance(java.lang.String,java.lang.Object...) in org.apache.commons.jexl3.JexlEngine
si/smth/project/bean/CUtil.java:[336,13] cannot find symbol
symbol:   method setCache(int)
location: variable jexl of type org.apache.commons.jexl3.JexlEngine
si/smth/project/bean/CUtil.java:[337,13] cannot find symbol

Solution

  • Use the empty constructor, which is the only constructor in latest java docs

    JexlEngine jexl = new JexlEngine();
    

    Or use JexlBuilder as describe in jexl:

    JexlEngine jexl = new JexlBuilder().create();
    

    You can call builder methods for your setters:

    JexlEngine jexl = strict(true).silent(false).cache(512) .create();
    

    Instead of Lenient flag you have setSilent and setStrict combinations:

    The setSilent and setStrict methods allow to fine-tune an engine instance behavior according to various error control needs. The strict flag tells the engine when and if null as operand is considered an error, the silent flag tells the engine what to do with the error (log as warning or throw exception).

    • When "silent" & "not-strict": 0 & null should be indicators of "default" values so that even in an case of error, something meaningful can still be inferred; may be convenient for configurations.
    • When "silent" & "strict": One should probably consider using null as an error case - ie, every object manipulated by JEXL should be valued; the ternary operator, especially the '?:' form can be used to workaround exceptional cases. Use case could be configuration with no implicit values or defaults.