55 lines
1.5 KiB
Plaintext
55 lines
1.5 KiB
Plaintext
The ``++equals++`` and ``++hashCode++`` methods of ``++java.net.URL++`` both may trigger a name service (usually DNS) lookup to resolve the host name or IP address. Depending on the configuration, and network status, that can take a long time. ``++URI++`` on the other hand makes no such calls and should be used instead unless the specific ``++URL++`` functionality is required.
|
|
|
|
|
|
In general it is better to use the ``++URI++`` class until access to the resource is actually needed, at which point you can just convert the ``++URI++`` to a ``++URL++`` using ``++URI.toURL()++``.
|
|
|
|
|
|
This rule checks for uses of ``++URL++`` 's in ``++Map++`` and ``++Set++`` , and for explicit calls to the ``++equals++`` and ``++hashCode++`` methods.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,java]
|
|
----
|
|
public void checkUrl(URL url) {
|
|
Set<URL> sites = new HashSet<URL>(); // Noncompliant
|
|
|
|
URL homepage = new URL("http://sonarsource.com"); // Compliant
|
|
if (homepage.equals(url)) { // Noncompliant
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,java]
|
|
----
|
|
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
|
|
// ...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|