ZhiqiangGao / beanshell2

Automatically exported from code.google.com/p/beanshell2
0 stars 0 forks source link

String comparision '==' failed for obscure reasons (Fixed, please update code) #86

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. i tried to reproduce it, but somehow it was not reproducable.
2. 'question == "first"' => false, even if right. very simple, but didn't work 
in my application environment.
3. in every other constellation (other test program), it worked very well and 
delivered 'true'

What is the expected output? What do you see instead?
only in my appliction: 'question=="anything"' delivered false. i wrote a test 
application but there it worked.

What version of the product are you using? On what operating system?
current

Please provide any additional information below.
the solution is quite simple in BSHBinaryExpression, line 185:
    case EQ:
            return new Primitive((lhs == rhs));

replace this line with this block:
    case EQ:
        if (lhs instanceof String && rhs instanceof String) {
            return new Primitive(lhs.equals(rhs));
        } else {
            return new Primitive((lhs == rhs));
        }

cnsntrk

Original issue reported on code.google.com by can.sent...@likaliner.de on 4 Jun 2013 at 10:31

GoogleCodeExporter commented 9 years ago
Beanshell handles '==' like java and not e.g. like JavaScript. That means 
you've got to use "equals" to compare instances.

bsh % "abc" == "abc";
<true>
bsh % "abc" == new String("abc");
<false>
bsh % "abc".equals("abc");
<true>
bsh % "abc".equals(new String("abc"));
<true>

Original comment by pejob...@gmail.com on 5 Jun 2013 at 6:48

GoogleCodeExporter commented 9 years ago
Hello Guys,

thank you for your quite fast answer and detailed answer. But to be honest, i 
simply expected it, to act more loosely.

What about that:
public class BashTester {

    public static void main(String[] args) throws EvalError {
        Interpreter interpreter = new Interpreter();

        interpreter.setStrictJava( <<<- true or false  ->>> );

        interpreter.set("questionA", new String("answer"));
        interpreter.set("questionB", "answer");

        System.out.println(interpreter.eval("questionA==\"answer\""));
        System.out.println(interpreter.eval("questionB==\"answer\""));
    }
}

Original comment by can.sent...@likaliner.de on 5 Jun 2013 at 7:21

GoogleCodeExporter commented 9 years ago
And you base your expection on what?

Original comment by pejob...@gmail.com on 5 Jun 2013 at 9:22

GoogleCodeExporter commented 9 years ago
1) if i use loose variable types, than there is no real difference between
       foo = "test";
       and 
       foo = new String("test");

2) the great advantage of bsh is the simple use and the unnecessary 
understanding difference of objects and instances in  java.

But, it is not my code and i'm not part of your group. so you have to decide 
you own, if it will enhance you engine or not.

I personally like the lib and think this mode of operation is intuitively right 
for the "none-strict-java" behaviour.

Original comment by can.sent...@likaliner.de on 5 Jun 2013 at 9:52

GoogleCodeExporter commented 9 years ago
Sorry, I don't think this can be changed. First it will break compatibility. 
And 2nd I don't think changing the behaviour of "==" just for 
java.lang.String-type will be understood by java developers. And 3rd: When this 
is changed, you must supply compare instances for 'same instance' (e.g. 
something like "===").

Original comment by pejob...@gmail.com on 5 Jun 2013 at 10:33

GoogleCodeExporter commented 9 years ago
ok, i already have a fork and it is now running fine for my purpose.

Thank you for your time.

Enjoy

Original comment by can.sent...@likaliner.de on 5 Jun 2013 at 10:40