Results 1 to 5 of 5

Thread: ObjecrPrxHelperBase.equals throwing ClassCastException

  1. #1
    tomq is offline Registered User
    Join Date
    Jan 2006
    Posts
    2

    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.

  2. #2
    marc's Avatar
    marc is offline ZeroC Staff
    Name: Marc Laukien
    Organization: ZeroC, Inc.
    Project: The Internet Communications Engine
    Join Date
    Feb 2003
    Location
    Florida
    Posts
    1,860
    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.

  3. #3
    marc's Avatar
    marc is offline ZeroC Staff
    Name: Marc Laukien
    Organization: ZeroC, Inc.
    Project: The Internet Communications Engine
    Join Date
    Feb 2003
    Location
    Florida
    Posts
    1,860
    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

  4. #4
    tomq is offline Registered User
    Join Date
    Jan 2006
    Posts
    2
    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...equalhash.html
    and
    http://www.javaworld.com/javaworld/j...object-p2.html
    and
    http://www-128.ibm.com/developerwork...-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).
    Tom Quarendon
    World Programming Company
    http://www.teamwpc.co.uk

  5. #5
    marc's Avatar
    marc is offline ZeroC Staff
    Name: Marc Laukien
    Organization: ZeroC, Inc.
    Project: The Internet Communications Engine
    Join Date
    Feb 2003
    Location
    Florida
    Posts
    1,860
    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!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Why no .equals() for Java classes
    By mefoster in forum Help Center
    Replies: 1
    Last Post: 05-11-2007, 09:19 PM
  2. Exception throwing question
    By g00fy in forum Help Center
    Replies: 2
    Last Post: 06-28-2006, 07:40 PM
  3. Re-throwing exception in Ice for C#
    By kwaclaw in forum Comments
    Replies: 1
    Last Post: 10-20-2005, 06:47 PM
  4. Throwing exception from another module
    By beardo in forum Help Center
    Replies: 3
    Last Post: 06-01-2005, 12:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •