From 88eb3552d89f3ee281a02d48d95f79b59a57d06d Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Tue, 16 Jul 2013 09:22:12 +0200 Subject: [PATCH] AtomicArray.toArray will now throw an exception if target array if of the wrong size. --- .../elasticsearch/common/util/concurrent/AtomicArray.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/util/concurrent/AtomicArray.java b/src/main/java/org/elasticsearch/common/util/concurrent/AtomicArray.java index c8df330e765..7a88c24265b 100644 --- a/src/main/java/org/elasticsearch/common/util/concurrent/AtomicArray.java +++ b/src/main/java/org/elasticsearch/common/util/concurrent/AtomicArray.java @@ -20,8 +20,8 @@ package org.elasticsearch.common.util.concurrent; import com.google.common.collect.ImmutableList; +import org.elasticsearch.ElasticSearchGenerationException; -import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReferenceArray; @@ -105,10 +105,8 @@ public class AtomicArray { * null. All in similar fashion to {@link ArrayList#toArray} */ public E[] toArray(E[] a) { - if (a.length < array.length()) { - a = (E[]) Array.newInstance(a.getClass().getComponentType(), array.length()); - } else if (a.length > array.length()) { - a[array.length()] = null; + if (a.length != array.length()) { + throw new ElasticSearchGenerationException("AtomicArrays can only be copied to arrays of the same size"); } for (int i = 0; i < array.length(); i++) { a[i] = array.get(i);