resources.report.rules.pmd.OptimizableToArrayCall.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sanity4j Show documentation
Show all versions of sanity4j Show documentation
Sanity4J was created to simplify running multiple static code
analysis tools on the Java projects. It provides a single entry
point to run all the selected tools and produce a consolidated
report, which presents all findings in an easily accessible
manner.
The newest version!
OptimizableToArrayCall
OptimizableToArrayCall
Calls to a collection’s toArray() method should specify target arrays sized to match the size of the collection. Initial arrays that are too small are discarded in favour of new ones that have to be created that are the proper size.
//PrimaryExpression
[PrimaryPrefix/Name[ends-with(@Image, 'toArray')]]
[
PrimarySuffix/Arguments/ArgumentList/Expression
/PrimaryExpression/PrimaryPrefix/AllocationExpression
/ArrayDimsAndInits/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image='0']
]
Example(s):
List foos = getFoos();
// inefficient, the array will be discarded
Foo[] fooArray = foos.toArray(new Foo[0]);
// much better; this one sizes the destination array,
// avoiding of a new one via reflection
Foo[] fooArray = foos.toArray(new Foo[foos.size()]);