Archived

This forum has been archived. Please start a new discussion on GitHub.

ObjecrPrxHelperBase.equals throwing ClassCastException

Never quite what the recommended behaviour is when you try to do equals() on two objects of completely different types, but if you do (for example)

ObjectPrxHelperBase prx = ...
if (prx.equals(new Integer(0))) {
...
}

you get a ClassCastException, rather than equals() returning false.
Is this deliberate?
It's a bit unfortunate for what I'm trying to do because I'm passing a proxy into some code that's not mine (I'm trying to display a tree of remote objects in an Eclipse tree view if that means anything to anyone) and so I can't easily change either the Eclispe code or the ObjectPrxBaseHelper class.

Comments

  • marc
    marc Florida
    Well, you cannot compare a proxy with an integer. Returning "true" or "false" would be wrong IMO. An exception is what you should expect, since the types don't match.
  • marc
    marc Florida
    Oops... I forgot to mention: Please update your signature before you post any additional questions. Please see this thread for details regarding our support policy:

    http://www.zeroc.com/vbulletin/showthread.php?t=1697
  • But you could say that "false" was the right thing to return since a proxy is not "equal" no (say) an Integer. Indeed if I say

    Integer i = new Integer(0);
    MyPrx p = ...
    bool b = i.equals(p);

    then I get false, and to satisfy the symmetry requirement of implementing equals i.equals(p) must return the same as p.equals(i) (though to be fair the symmetric requirement is only written in terms of p.equals(i) must return true if and only if i.equals(p) returns true, which it wont, so throwing an exception is allowable behaviour).

    All of the pages I can find on "how to implement equals" on the internet suggest that some form of test such as

    if (!(other instanceof ObjectPrxHelperBase)) return false

    is required in an equals implementation. See for example
    http://www.javapractices.com/Topic17.cjp
    and
    http://www.geocities.com/technofundo/tech/java/equalhash.html
    and
    http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object-p2.html
    and
    http://www-128.ibm.com/developerworks/java/library/j-jtp05273.html

    (note that I think only the second one gets it right -- using the instanceof operator is the only way to correctly get the symmetric requirement right).
  • marc
    marc Florida
    Hmm... I see. I guess you are right, if these are the Java conventions for equals(), we should fix this. Thanks for the bug report!