Rule Definition
the Implementation of equals() and hashcode() calls a naming service(DNS) to resolve the IP address. This may take a lot of time and causes a dependency on Network Connection.
It is inconsistent and may give different results with or without a network connection and takes long time to execute. The implementation is known to be incompatible with virtual hosting and should not be used.
Remediation
use java.net.URI instead of java.net.URL
Violation Code Sample
__________________________________________________________________________
(A)
public class Main {
public static void main(String[] argv) throws Exception {
URL relativeURL, baseURL;
baseURL = new URL("http://www.yourserver.com/");
relativeURL = new URL(baseURL, "./a.htm");
System.out.println(relativeURL.equals(baseURL)); //non-compliant
}
}
___________________________________________________________________________
(B)
public void checkUrl(URL url) {
Set<URL> sites = new HashSet<URL>(); // Noncompliant
URL homepage = new URL("http://castsoftware.com"); // Compliant
if (homepage.equals(url)) { // Noncompliant
// ...
}
}
____________________________________________________________________________
(C)
import java.io.IOException;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
URL url = new URL("http://www.castsoftware.com");
System.out.println("URL is " + url.hashCode());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Fixed Code Sample
____________________________________________________________
(A)
public class Main {
public static void main(String[] argv) throws Exception {
URL relativeURL, baseURL;
baseURL = new URL("http://www.yourserver.com/");
relativeURL = new URL(baseURL, "./a.htm");
URI.uri1 = relativeURL.toURI();
URI.uri2 = relativeURL.toURI();
System.out.println(uri1.equals(uri2));//compliant
}
}
_______________________________________________________________
(B)
public void checkUrl(URL url) {
Set<URI> sites = new HashSet<URI>(); // Compliant
URI homepage = new URI("http://sonarsource.com"); // Compliant
URI uri = url.toURI();
if (homepage.equals(uri)) { // Compliant
// ...
}
}______________________________________________________________
(C)
import java.net.URI;
import java.net.URISyntaxException;
public class Main {
public static void main(String[] args) throws NullPointerException, URISyntaxException {
URI uri = new URI("http://www.castsoftware.com");
System.out.println("URI : " + uri);
System.out.println(uri.hashCode());
}
}
Reference
https://javaantipatterns.wordpress.com/2007/11/24/comparing-urls-with-urlequals/
http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html
Related Technologies
Technical Criterion
Secure Coding - Time and State
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.