From 3641f1626df6b9b1c11dd3f16b01a01495f4662d Mon Sep 17 00:00:00 2001 From: Lars Grefer Date: Sat, 15 Aug 2020 16:30:09 +0200 Subject: Collection.toArray() call style 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. Signed-off-by: Lars Grefer --- util/src/main/java/org/aspectj/util/FileUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'util') diff --git a/util/src/main/java/org/aspectj/util/FileUtil.java b/util/src/main/java/org/aspectj/util/FileUtil.java index 976c3ccbc..a586b69f6 100644 --- a/util/src/main/java/org/aspectj/util/FileUtil.java +++ b/util/src/main/java/org/aspectj/util/FileUtil.java @@ -710,7 +710,7 @@ public class FileUtil { if ((null != srcDir) && srcDir.canRead()) { listFiles(srcDir, result, fileFilter); } - return result.toArray(new File[result.size()]); + return result.toArray(new File[0]); } /** -- cgit v1.2.3