From 717ba0311d40b3ea64b8cd64fc243bc823e0dd14 Mon Sep 17 00:00:00 2001 From: Michal Kordas Date: Fri, 7 Aug 2015 21:57:57 +0200 Subject: [PATCH] Replace toArray() argument with correctly sized array. #1555 Fixes `ToArrayCallWithZeroLengthArrayArgument` inspection violation. Description: >Reports any call to toArray() on an object of type or subtype java.util.Collection with a zero-length array argument. When passing in an array of too small size, the toArray() method has to construct a new array of the right size using reflection. This has significantly worse performance than passing in an array of at least the size of the collection itself. --- src/main/java/com/puppycrawl/tools/checkstyle/Utils.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/Utils.java b/src/main/java/com/puppycrawl/tools/checkstyle/Utils.java index 467d164de..d34bec62e 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/Utils.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/Utils.java @@ -32,6 +32,7 @@ import java.util.regex.PatternSyntaxException; import org.apache.commons.beanutils.ConversionException; import org.apache.commons.lang3.ArrayUtils; +import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableMap; import com.puppycrawl.tools.checkstyle.api.TokenTypes; @@ -76,7 +77,8 @@ public final class Utils { TOKEN_NAME_TO_VALUE = builder.build(); TOKEN_VALUE_TO_NAME = tempTokenValueToName; - final Integer[] ids = TOKEN_NAME_TO_VALUE.values().toArray(new Integer[0]); + final ImmutableCollection values = TOKEN_NAME_TO_VALUE.values(); + final Integer[] ids = values.toArray(new Integer[values.size()]); TOKEN_IDS = ArrayUtils.toPrimitive(ids); }