57 lines
1.3 KiB
Plaintext
57 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Creating temporary primitive wrapper objects only for `String` conversion or the use of the `compareTo()` method is inefficient.
|
|
|
|
Instead, the static `toString()` or `compare()` method of the primitive wrapper class should be used.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
private int isZero(int value){
|
|
return Integer.valueOf(value).compareTo(0); // Noncompliant
|
|
}
|
|
private String convert(int value){
|
|
return Integer.valueOf(value).toString(); // Noncompliant
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
private int isZero(int value){
|
|
return Integer.compare(value, 0); // Compliant
|
|
}
|
|
private String convert(int value){
|
|
return Integer.toString(value); // Compliant
|
|
}
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Call the static method {wrapperClass}.[toString(...)|compare(...)} instead of instantiating a temporary object.
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 29 Jul 2013, 15:21:20 Freddy Mallet wrote:
|
|
Is implemented by \http://jira.codehaus.org/browse/SONARJAVA-246
|
|
|
|
=== on 16 Aug 2013, 14:29:29 Dinesh Bolkensteyn wrote:
|
|
\[~freddy.mallet] You can't use {primitiveWrapperClass} in the rule description ;-)
|
|
|
|
endif::env-github,rspecator-view[]
|