hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Audley (JIRA)" <j...@apache.org>
Subject [jira] Commented: (HTTPCLIENT-593) ProtocolSocketFactory equals and hashCode don't support subclassing
Date Wed, 02 Aug 2006 18:12:14 GMT
    [ http://issues.apache.org/jira/browse/HTTPCLIENT-593?page=comments#action_12425319 ] 
Chris Audley commented on HTTPCLIENT-593:

No Oleg, not a good point!  This should be fixed in the Beta1.

To help illustrate some of the problems with the equals method, play around with this little
test program.

    public static void main(String[] args) {
        ProtocolSocketFactory factory1 = new DefaultProtocolSocketFactory();
        ProtocolSocketFactory factory2 = new DefaultProtocolSocketFactory() {};

        Protocol protocolA = new Protocol("http", factory1, 80);
        Protocol protocolB = new Protocol("http", factory2, 80);
        Protocol protocolC = new Protocol("http", factory2, 80);

        if (!protocolB.equals(protocolC))
            System.out.println("Hey! protocolB doesn't equal protocolC");
        if (!protocolA.equals(protocolB))
            System.out.println("Whew, protocolA != protocolB!!!");
        if (protocolB.equals(protocolA))
            System.out.println("What! protocolB == protocolA!!!");
        if (protocolA.equals(protocolB) != protocolB.equals(protocolA))
            System.out.println("Oh no! equals isn't reflexive");
        if (!protocolB.equals(protocolB))
            System.out.println("Uh, You may want to fix this");

Notice that factory2 is a subclass of DefaultProtocolSocketFactory.  The first instance of
Protocol (protocolA) should not be equal to protocolB or protocolC, which should be equal
to each other.  As you'll see when you run the program, this is not the case.  Every single
one of the println methods will be run.

May favorite error is the last one.

> ProtocolSocketFactory equals and hashCode don't support subclassing
> -------------------------------------------------------------------
>                 Key: HTTPCLIENT-593
>                 URL: http://issues.apache.org/jira/browse/HTTPCLIENT-593
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>          Components: HttpClient
>    Affects Versions: Nightly Builds, 3.1 Alpha 1
>            Reporter: Chris Audley
>            Priority: Minor
>             Fix For: 4.0 Alpha 1
> In the implemenation of equals and hashCode for the classes
> org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory
> org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory
> The implementation of equals and hashCode attempts to make all instances of the classes
equal.  However, the manner in which the methods are coded makes it necessary for any subclass
to implement equals and hashCode themselves.  A minor change to the methods in these classes
will make possible to subclass these factories without re-implementing the equals and hashCode.
 The method equals should be written as
>         return ((obj != null) && obj.getClass().equals(getClass()));
> rather than
>         return ((obj != null) && obj.getClass().equals(DefaultProtocolSocketFactory.class));
> And similarly, the hashCode method should be
>         return getClass().hashCode();
> rather than
>         return DefaultProtocolSocketFactory.class.hashCode();

This message is automatically generated by JIRA.
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira


To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org

View raw message