Skip to content

Commit 56cc0e4

Browse files
author
wj
committed
Call to 'toArray()' with pre-sized array argument 'new String[map.keySet().size()]'
Inspection info: There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]). In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation. This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).
1 parent 7cb8ace commit 56cc0e4

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/main/java/org/apache/ibatis/reflection/wrapper/MapWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public String findProperty(String name, boolean useCamelCaseMapping) {
6363

6464
@Override
6565
public String[] getGetterNames() {
66-
return map.keySet().toArray(new String[map.keySet().size()]);
66+
return map.keySet().toArray(new String[0]);
6767
}
6868

6969
@Override
7070
public String[] getSetterNames() {
71-
return map.keySet().toArray(new String[map.keySet().size()]);
71+
return map.keySet().toArray(new String[0]);
7272
}
7373

7474
@Override

0 commit comments

Comments
 (0)