rspec/rules/S3012/java/rule.adoc
2023-08-10 15:06:59 +02:00

98 lines
2.9 KiB
Plaintext

== Why is this an issue?
The JDK provides a set of built-in methods to copy the contents of an array into another array.
Using a loop to perform the same operation is less clear, more verbose and should be avoided.
=== Exceptions
The rule detects only the most idiomatic patterns, it will not consider loops with non-trivial control flow.
For example, loops that copy array elements conditionally are ignored.
== How to fix it
You can use:
* `Arrays.copyOf` to copy an entire array into another array
* `System.arraycopy` to copy only a subset of an array into another array
* `Arrays.asList` to create a new list with the contents of the array
* `Collections.addAll` to add the elements of a collection into another collection
Note that `Arrays.asList` returns a fixed-size `List`, so further steps are required if a non-fixed-size `List` is needed.
=== Code examples
==== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
public void copyArray(String[] source){
String[] array = new String[source.length];
for (int i = 0; i < source.length; i++) {
array[i] = source[i]; // Noncompliant
}
}
public void copyList(List<String> source) {
List<String> list = new ArrayList<>();
for (String s : source) {
list.add(s); // Noncompliant
}
}
----
==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
public void copyArray(String[] source){
String[] array = Arrays.copyOf(source, source.length);
}
public void copyList(List<String> source) {
List<String> list = new ArrayList<>();
Collections.addAll(list, source);
}
----
[source,java]
----
public void makeCopiesConditional(int[] source) {
int[] dest = new int[source.length];
for (int i = 0; i < source.length; i++) {
if (source[i] > 10) {
dest[i] = source[i]; // Compliant, since the array elements are conditionally copied to the dest array
}
}
}
----
== Resources
=== Documentation
* https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#copyOf-T:A-int-:~:text=1.6-,copyOf,-public%20static%C2%A0byte[docs.oracle] - Arrays.copyOf documentation
* https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-:~:text=1.6-,asList,-%40SafeVarargs%0Apublic%20static[docs.oracle] - Arrays.asList documentation
* https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#arraycopy-java.lang.Object-int-java.lang.Object-int-int-:~:text=1.5-,arraycopy,-public%20static%C2%A0void[docs.oracle] - System.arraycopy documentation
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use "Arrays.copyOf", "Arrays.asList", "Collections.addAll" or "System.arraycopy" instead.
'''
== Comments And Links
(visible only on this page)
=== on 23 Jul 2015, 07:14:19 Nicolas Peru wrote:
I slightly changed the title to make it clearer. Please double check.
=== on 23 Jul 2015, 09:06:29 Ann Campbell wrote:
Thanks [~nicolas.peru].
endif::env-github,rspecator-view[]