doesn't equal to self

Stanimir Simeonoff stanimir at
Mon Aug 18 14:04:36 UTC 2014


As the title says there is a major bug with HttpsURLConnection as it breaks
the contract of equals. So the instance  cannot be contained (mostly
removed) in any collection reliably. (Concurrent)HashMap has a provision to
test equality by reference prior calling equals, though.
Finding the bug in production is quite a ride as the http variant doesn't
exhibit the problem.

Here is a simple test case.

   public static void main(String[] args) throws Throwable{
     URLConnection c = new URL("").openConnection();
     System.out.println(c.getClass().getName()+" equals self: "
     System.out.println(c.getClass().getName()+" equals self: "

The culprit is in HttpsURLConnectionImpl.equals that blindly calls

    public boolean equals(Object obj) {
        return delegate.equals(obj);

It should be changed to:

    public boolean equals(Object obj) {
        return this==obj || (obj instanceof HttpsURLConnectionImpl) &&
delegate.equals( ((HttpsURLConnectionImpl)obj).delegate );

The class has some other issue that involves declaring "finalize" method to
simply call delegate's dispose. The finalize method is unneeded and just
creates unnecessary link in the finalization queue + Finalizer object.


More information about the core-libs-dev mailing list