#124 Problem with Java Relational operators == and !=

open
nobody
None
5
2003-06-11
2003-06-11
Anonymous
No

Heading: The relational operators == and != don't work
as per the java syntax rules

Bug Recreation: The following code should report back
an error because of the invalid comparison between
double and String object, in the 3rd line "scr05[0]
== "test" .

Code:
double[] scr05 = new double[1];
scr05[0] = 1.0;
scr05[0] = ( scr05[0] == "test" ) ? - 2 :1 ;

Possible fix:

Modified bsh.BSHBinaryExpression.java class to handle
== and != operators as per the java syntax listed
below. This fix worked, but not very sure whether it
could introduce some other bugs in the code. Could you
please provide a patch for this, if not atleast give us
code review feedback?

Java Syntax:
Case 1: If neither LHS operand nor RHS operand is "null",
it directly goes to else condition and checks whether
LHS and RHS operands are of same type, if not
reports back an error "

Case 2: If either LHS operand or RHS operand is of
type "null", it checks whether LHS or RHS operand is of
type primitive, if yes report back an error

Case 3: If both LHS operand and RHS operand are "null",
no error is reported back (Ex: if (null = null) ....)

Suggested code change in BeanShell interpreter:

Class Name: bsh.BSHBinaryExpression.java
Method Name: public Object eval( CallStack callstack,
Interpreter interpreter) throws EvalError

case EQ:

if( ( lhs == Primitive.NULL ) || ( rhs ==
Primitive.NULL ) )
{

if( (lhs instance of Primitive ) && (rhs
instanceof Primitive ) )
if(! ( lhs.getClass().getName().equals
(rhs.getClass().getName())) )
{
throw new EvalError("Operator: '" +
tokenImage[kind] +
"' inappropriate for objects", this);
}

}
else
{
if(! ( lhs.getClass().getName().equals
(rhs.getClass().getName())) )
{
throw new EvalError("Operator: '" +
tokenImage[kind] +
"' inappropriate for objects", this);
}
}
return new Primitive((lhs == rhs));

case NE:

//same code
if( ( lhs == Primitive.NULL ) || ( rhs ==
Primitive.NULL ) )
{

if( (lhs instanceof Primitive ) && (rhs
instanceof Primitive ) )
if(! ( lhs.getClass().getName().equals
(rhs.getClass().getName())) )
{
throw new EvalError("Operator: '" +
tokenImage[kind] +
"' inappropriate for objects", this);
}

}
else
{
if(! ( lhs.getClass().getName().equals
(rhs.getClass().getName())) )
{
throw new EvalError("Operator: '" +
tokenImage[kind] +
"' inappropriate for objects", this);
}
}
return new Primitive((lhs != rhs));

thanks,
Lakshmi.

Discussion

  • Pat Niemeyer
    Pat Niemeyer
    2003-06-20

    Logged In: YES
    user_id=18885

    I believe the real fix for this problem will require some
    thought. The problem is that we'd like the solution to work
    for both typed and untyped variables... i.e. if a variable
    has an assigned type we check it, if not we don't. Since we
    don't currently carry that information everywhere this may
    require some changes. I will make this a priority after the
    initial 1.3 release.