
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
79 lines
2.7 KiB
Plaintext
79 lines
2.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
While ``++hashCode++`` and ``++toString++`` are available on arrays, they are largely useless. ``++hashCode++`` returns
|
|
the array's "identity hash code", and ``++toString++`` returns nearly the same value. Neither method's output actually
|
|
reflects the array's contents. Furthermore, ``++contentHashCode()++`` and ``++contentToString()++`` are also useless on
|
|
arrays of array.
|
|
|
|
Instead, you should use:
|
|
|
|
* On array of objects or arrays: ``++contentDeepHashCode()++`` and ``++contentDeepToString()++``
|
|
* On array of primitives: ``++contentHashCode()++`` and ``++contentToString()++``
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,kotlin]
|
|
----
|
|
fun main() {
|
|
val primitiveArray = intArrayOf(1, 2, 3)
|
|
val objectArray = arrayOf("A", "B", "C")
|
|
val arrayOfArray = arrayOf(arrayOf("A", "B"), arrayOf("C", "D"))
|
|
|
|
println(primitiveArray.toString()) // Noncompliant, output: [I@2acf57e3
|
|
println(primitiveArray.hashCode()) // Noncompliant, output: 718231523
|
|
println(objectArray.toString()) // Noncompliant, output: [Ljava.lang.String;@506e6d5e
|
|
println(objectArray.hashCode()) // Noncompliant, output: 1349414238
|
|
println(arrayOfArray.toString()) // Noncompliant, output: [[Ljava.lang.String;@96532d6
|
|
println(arrayOfArray.contentToString()) // Noncompliant, output: [[Ljava.lang.String;@3796751b, [Ljava.lang.String;@67b64c45]
|
|
println(arrayOfArray.hashCode()) // Noncompliant, output: 157627094
|
|
println(arrayOfArray.contentHashCode()) // Noncompliant, output: 586055243
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,kotlin]
|
|
----
|
|
fun main() {
|
|
val primitiveArray = intArrayOf(1, 2, 3)
|
|
val objectArray = arrayOf("A", "B", "C")
|
|
val arrayOfArray = arrayOf(arrayOf("A", "B"), arrayOf("C", "D"))
|
|
|
|
println(primitiveArray.contentToString()) // Compliant, output: [1, 2, 3]
|
|
println(primitiveArray.contentHashCode()) // Compliant, output: 30817
|
|
println(objectArray.contentDeepToString()) // Compliant, output: [A, B, C]
|
|
println(objectArray.contentDeepHashCode()) // Compliant, output: 94369
|
|
println(arrayOfArray.contentDeepToString()) // Compliant, output: [[A, B], [C, D]]
|
|
println(arrayOfArray.contentDeepHashCode()) // Compliant, output: 98369
|
|
}
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
On arrays of objects:
|
|
* Use "contentDeepHashCode()" instead.
|
|
* Use "contentDeepToString()" instead.
|
|
|
|
On arrays of primitives:
|
|
* Use "contentHashCode()" instead.
|
|
* Use "contentToString()" instead.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 15 Jul 2016, 14:24:30 Ann Campbell wrote:
|
|
https://github.com/google/error-prone/blob/master/docs/bugpattern/ArrayHashCode.md
|
|
|
|
endif::env-github,rspecator-view[]
|