< prev index next >

src/java.base/share/classes/java/util/Collections.java

Print this page
8197893: Mistaken type check in CheckedEntrySet.toArray
Reviewed-by: psandoz


3754                 final Class<V> valueType = this.valueType;
3755 
3756                 return new Iterator<Map.Entry<K,V>>() {
3757                     public boolean hasNext() { return i.hasNext(); }
3758                     public void remove()     { i.remove(); }
3759 
3760                     public Map.Entry<K,V> next() {
3761                         return checkedEntry(i.next(), valueType);
3762                     }
3763                 };
3764             }
3765 
3766             @SuppressWarnings("unchecked")
3767             public Object[] toArray() {
3768                 Object[] source = s.toArray();
3769 
3770                 /*
3771                  * Ensure that we don't get an ArrayStoreException even if
3772                  * s.toArray returns an array of something other than Object
3773                  */
3774                 Object[] dest = (CheckedEntry.class.isInstance(
3775                     source.getClass().getComponentType()) ? source :
3776                                  new Object[source.length]);
3777 
3778                 for (int i = 0; i < source.length; i++)
3779                     dest[i] = checkedEntry((Map.Entry<K,V>)source[i],
3780                                            valueType);
3781                 return dest;
3782             }
3783 
3784             @SuppressWarnings("unchecked")
3785             public <T> T[] toArray(T[] a) {
3786                 // We don't pass a to s.toArray, to avoid window of
3787                 // vulnerability wherein an unscrupulous multithreaded client
3788                 // could get his hands on raw (unwrapped) Entries from s.
3789                 T[] arr = s.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
3790 
3791                 for (int i=0; i<arr.length; i++)
3792                     arr[i] = (T) checkedEntry((Map.Entry<K,V>)arr[i],
3793                                               valueType);
3794                 if (arr.length > a.length)
3795                     return arr;
3796 




3754                 final Class<V> valueType = this.valueType;
3755 
3756                 return new Iterator<Map.Entry<K,V>>() {
3757                     public boolean hasNext() { return i.hasNext(); }
3758                     public void remove()     { i.remove(); }
3759 
3760                     public Map.Entry<K,V> next() {
3761                         return checkedEntry(i.next(), valueType);
3762                     }
3763                 };
3764             }
3765 
3766             @SuppressWarnings("unchecked")
3767             public Object[] toArray() {
3768                 Object[] source = s.toArray();
3769 
3770                 /*
3771                  * Ensure that we don't get an ArrayStoreException even if
3772                  * s.toArray returns an array of something other than Object
3773                  */
3774                 Object[] dest = (source.getClass() == Object[].class)
3775                     ? source
3776                     : new Object[source.length];
3777 
3778                 for (int i = 0; i < source.length; i++)
3779                     dest[i] = checkedEntry((Map.Entry<K,V>)source[i],
3780                                            valueType);
3781                 return dest;
3782             }
3783 
3784             @SuppressWarnings("unchecked")
3785             public <T> T[] toArray(T[] a) {
3786                 // We don't pass a to s.toArray, to avoid window of
3787                 // vulnerability wherein an unscrupulous multithreaded client
3788                 // could get his hands on raw (unwrapped) Entries from s.
3789                 T[] arr = s.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
3790 
3791                 for (int i=0; i<arr.length; i++)
3792                     arr[i] = (T) checkedEntry((Map.Entry<K,V>)arr[i],
3793                                               valueType);
3794                 if (arr.length > a.length)
3795                     return arr;
3796 


< prev index next >