Happy New Year, Mark !
Thank you for the bug report !
Indeed, JEL used to strictly follow JLS for string comparison, but then for
JEL 0.9.8 someone requested a full string comparison support (complete with
collation rules). It was indeed a very good idea, but since that version the
strings started to be treated differently from other objects. Their
comparison
was delegated to java.text.Collator, which by default throws an exception on
null arguments. It was possible to override this behaviour in
collator without
modifying JEL, but I agree that not following the JLS for string comparisons
by default is a bug.
I've made a pre-release version of JEL 2.1.3, fixing this bug (and adding a
few more tests for string comparison) and also committed this fix
on Savannah.
You can download the pre-release from:
http://www.donfti.ru/~metlov/jel-2_1_3-pre1.zip
The sha256sum is:
db720fdc4dc5679aa43232baea31f47a9fa6b784f30d2cadaac0541af38df9a7
jel-2_1_3-pre1.zip
With the best regards,
Konstantin.
Quoting Mark Taylor <m.b.taylor@bristol.ac.uk>:
> Dear Konstantin,
>
> happy new year! And a JEL question.
>
> If I evaluate a string equality test in which one of the operands is null,
> a NullPointerException results, e.g.:
>
> x=="abc"
>
> provokes a NullPointerException if x has the value null.
> Since the equivalent test in Java simply returns false, that's not
> what I was expecting. Is this intended behaviour?
>
> The following program illustrates this:
>
> import gnu.jel.CompiledExpression;
> import gnu.jel.Evaluator;
> import gnu.jel.Library;
>
> public class Cmp {
> public static void main(String[] args) throws Throwable {
> Library lib = new Library(new Class[] {Cmp.ALib.class}, null,
> null, null, null);
> compare("textValue", lib);
> compare("nullValue", lib);
> }
> private static void compare(String func, Library lib) throws
> Throwable {
> String txtExpr = func;
> String cmpExpr = func + "==\"abc\"";
>
> CompiledExpression txtCompex = Evaluator.compile(txtExpr, lib);
> CompiledExpression cmpCompex = Evaluator.compile(cmpExpr, lib);
>
> System.out.println();
> report(txtExpr, txtCompex);
> report(cmpExpr, cmpCompex);
> }
> private static void report(String expr, CompiledExpression compex)
> throws Throwable {
> System.out.println(expr);
> Object result = compex.evaluate(null);
> System.out.println(" -> " + represent(result));
> }
> private static String represent(Object obj) {
> if (obj == null) {
> return "null";
> }
> else if (obj instanceof String) {
> return "\"" + obj + "\"";
> }
> else {
> return obj.toString();
> }
> }
> public static class ALib {
> public static String textValue() {
> return "text";
> }
> public static String nullValue() {
> return null;
> }
> }
> }
>
> which produces the output (JEL 2.1.2):
>
> textValue
> -> "text"
> textValue=="abc"
> -> false
>
> nullValue
> -> null
> nullValue=="abc"
> Exception in thread "main" java.lang.NullPointerException
> at
> java.text.RuleBasedCollator.compare(RuleBasedCollator.java:357)
> at
> gnu.jel.CompiledExpression.compare(CompiledExpression.java:352)
> at dump.evaluate_boolean(Unknown Source)
> at
> gnu.jel.CompiledExpression.evaluate(CompiledExpression.java:157)
> at Cmp.report(Cmp.java:25)
> at Cmp.compare(Cmp.java:21)
> at Cmp.main(Cmp.java:10)
>
> Thanks!
>
> Mark
>
> --
> Mark Taylor Astronomical Programmer Physics, Bristol University, UK
> m.b.taylor@bristol.ac.uk http://www.star.bristol.ac.uk/~mbt/