All Downloads are FREE. Search and download functionalities are using the official Maven repository.

resources.report.rules.pmd.OptimizableToArrayCall.html Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.8.2
Show 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()]);
  




© 2015 - 2024 Weber Informatics LLC | Privacy Policy