From 5a1dcb13eaa6040be54cf3bc6772470a397b2c79 Mon Sep 17 00:00:00 2001 From: Andrew John Hughes Date: Fri, 18 Mar 2016 20:29:35 +0000 Subject: Cleanup warnings in java.util.Collections. 2016-03-18 Andrew John Hughes Cleanup warnings in java.util.Collections. * java/util/Collections.java: (EMPTY_SET): Suppress warnings generated by this legacy raw type. (EMPTY_LIST): Likewise. (EMPTY_MAP): Likewise. (compare(T, T, Comparator): Suppress warning generated by casting to Comparable. (binarySearch(List, Comparator)): Remove unneeded casts. (reverse(List)): Suppress warnings when casting to ListIterator. Cast is necessary as lists are modified. (reverseOrder(Comparator)): Return reverseOrder() rather than rcInstance. (reverseOrder()): Create appropriately typed ReverseComparator rather than using set instance. (rcInstance): Removed. (ReverseComparator.compare(T,T)): Suppress warnings from casting to Comparable. (rotate(List,int)): Suppress warnings when casting to List. Cast is necessary to modify list. (shuffle(List,Random)): Likewise. (disjoint(Collection,Collection)): Remove unnecessary casting. (sort(List,Comparator)): Suppress warnings when casting the returned array. Arrays of a generic type can not be created. (swap(List,int,int)): Suppress warnings when casting list to List. This is necessary to use the set method. (entrySet().SynchronizedMapEntry): Type should not have its own type parameters which hide those of the map. (entrySet().SynchronizedSet.iterator()): Remove type parameters from SynchronizedMapEntry creation. (UnmodifiableList.list): Retain input type parameter so as to avoid casting. (UnmodifiableList.UnmodifiableList): Remove cast. (UnmodifiableList.li): Retain input type parameter so as to avoid casting. (UnmodifiableListIterator(ListIterator)): Likewise. (UnmodifiableMap(Map)): Suppress warnings from casting down to Map. (UnmodifiableMap.entrySet()): Remove type parameters from UnmodifiableEntrySet creation. (UnmodifiableMap.UnmodifiableEntrySet): Remove type parameters and make instance-specific so we can use the UnmodifiableMap's type parameters. (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableMapEntry): Likewise. (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableMapEntry.e): Retain input type parameters so as to avoid casting. (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableMapEntry.UnmodifiableMapEntry(Map.Entry)): Likewise. (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableEntrySet(Set)): Likewise. (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableIterator.next()): Remove type parameters from UnmodifiableMapEntry creation. (UnmodifiableMap.toArray()): Likewise and improve method structure. (UnmodifiableMap.toArray(S[])): Remove type parameters from UnmodifiableMapEntry and suppress warnings from casting array members. (UnmodifiableSortedMap.sm): Retain input type parameters so as to avoid casting. (UnmodifiableSortedMap.UnmodifiableSortedMap(SortedMap)): Remove cast. (CheckedList.addAll(int,Collection)): Replace iterator with for-each loop, removing the need for casting. (CheckedMap.entrySet()): Remove type parameters from CheckedEntrySet creation and klass argument. (CheckedMap.CheckedEntrySet): Don't create additional type parameters, use those of the containing map as a instance class, not a static class. (keyType): Type should be K to match enclosing CheckedMap. (valueType): Type should be V to match enclosing CheckedMap. (CheckedMap.CheckedEntrySet.CheckedEntrySet(Set,Class,Class)): Suppress warnings when casting Map.Entry class. (CheckedMap.CheckedEntrySet.iterator()): Make return type Map.Entry and create corresponding CheckedIterator instance. (CheckedMap.CheckedEntrySet.CheckedIterator.next()): Likewise with Map.Entry instance. (CheckedMap.CheckedEntrySet.CheckedIterator.Map.Entry.getKey()): Make return type K. (CheckedMap.CheckedEntrySet.CheckedIterator.Map.Entry.getValue()): Make return type V. (CheckedMap.CheckedEntrySet.CheckedIterator.Map.Entry.setValue(Object)): Likewise. (CheckedMap.putAll(Map)): Use for-each loop instead of Iterator, avoiding casting. --- ChangeLog | 92 +++++++++++++++++++++++++ java/util/Collections.java | 167 +++++++++++++++++++++++---------------------- 2 files changed, 178 insertions(+), 81 deletions(-) diff --git a/ChangeLog b/ChangeLog index f5221bd84..0d41d6c8c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,95 @@ +2016-03-18 Andrew John Hughes + + * java/util/Collections.java: + (EMPTY_SET): Suppress warnings generated by this + legacy raw type. + (EMPTY_LIST): Likewise. + (EMPTY_MAP): Likewise. + (compare(T, T, Comparator): Suppress warning + generated by casting to Comparable. + (binarySearch(List, Comparator)): Remove unneeded + casts. + (reverse(List)): Suppress warnings when casting + to ListIterator. Cast is necessary as + lists are modified. + (reverseOrder(Comparator)): Return reverseOrder() + rather than rcInstance. + (reverseOrder()): Create appropriately typed + ReverseComparator rather than using set instance. + (rcInstance): Removed. + (ReverseComparator.compare(T,T)): Suppress warnings + from casting to Comparable. + (rotate(List,int)): Suppress warnings when casting + to List. Cast is necessary to modify list. + (shuffle(List,Random)): Likewise. + (disjoint(Collection,Collection)): Remove unnecessary + casting. + (sort(List,Comparator)): Suppress warnings when casting + the returned array. Arrays of a generic type can not + be created. + (swap(List,int,int)): Suppress warnings when casting + list to List. This is necessary to use the + set method. + (entrySet().SynchronizedMapEntry): Type should not have its + own type parameters which hide those of the map. + (entrySet().SynchronizedSet.iterator()): Remove type + parameters from SynchronizedMapEntry creation. + (UnmodifiableList.list): Retain input type parameter + so as to avoid casting. + (UnmodifiableList.UnmodifiableList): Remove cast. + (UnmodifiableList.li): Retain input type parameter + so as to avoid casting. + (UnmodifiableListIterator(ListIterator)): Likewise. + (UnmodifiableMap(Map)): Suppress warnings from casting + down to Map. + (UnmodifiableMap.entrySet()): Remove type parameters + from UnmodifiableEntrySet creation. + (UnmodifiableMap.UnmodifiableEntrySet): Remove type + parameters and make instance-specific so we can + use the UnmodifiableMap's type parameters. + (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableMapEntry): + Likewise. + (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableMapEntry.e): + Retain input type parameters so as to avoid casting. + (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableMapEntry.UnmodifiableMapEntry(Map.Entry)): + Likewise. + (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableEntrySet(Set)): + Likewise. + (UnmodifiableMap.UnmodifiableEntrySet.UnmodifiableIterator.next()): + Remove type parameters from UnmodifiableMapEntry creation. + (UnmodifiableMap.toArray()): Likewise and improve method + structure. + (UnmodifiableMap.toArray(S[])): Remove type parameters from + UnmodifiableMapEntry and suppress warnings from casting + array members. + (UnmodifiableSortedMap.sm): Retain input type parameters + so as to avoid casting. + (UnmodifiableSortedMap.UnmodifiableSortedMap(SortedMap)): + Remove cast. + (CheckedList.addAll(int,Collection)): Replace iterator with for-each + loop, removing the need for casting. + (CheckedMap.entrySet()): Remove type parameters from CheckedEntrySet + creation and klass argument. + (CheckedMap.CheckedEntrySet): Don't create additional + type parameters, use those of the containing map as + a instance class, not a static class. + (keyType): Type should be K to match enclosing CheckedMap. + (valueType): Type should be V to match enclosing CheckedMap. + (CheckedMap.CheckedEntrySet.CheckedEntrySet(Set,Class,Class)): + Suppress warnings when casting Map.Entry class. + (CheckedMap.CheckedEntrySet.iterator()): Make return type + Map.Entry and create corresponding CheckedIterator instance. + (CheckedMap.CheckedEntrySet.CheckedIterator.next()): Likewise + with Map.Entry instance. + (CheckedMap.CheckedEntrySet.CheckedIterator.Map.Entry.getKey()): + Make return type K. + (CheckedMap.CheckedEntrySet.CheckedIterator.Map.Entry.getValue()): + Make return type V. + (CheckedMap.CheckedEntrySet.CheckedIterator.Map.Entry.setValue(Object)): + Likewise. + (CheckedMap.putAll(Map)): Use for-each loop instead of Iterator, + avoiding casting. + 2016-01-04 Andrew John Hughes Cleanup warnings in gnu.xml.transform.* and diff --git a/java/util/Collections.java b/java/util/Collections.java index b970dd8f6..9fa8b4b7a 100644 --- a/java/util/Collections.java +++ b/java/util/Collections.java @@ -1,5 +1,5 @@ /* Collections.java -- Utility class with methods to operate on collections - Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006 + Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2016 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -110,7 +110,9 @@ public class Collections * An immutable, serializable, empty Set. * @see Serializable */ - public static final Set EMPTY_SET = new EmptySet(); + // Legacy public empty collection constants have no type parameter + @SuppressWarnings("unchecked") + public static final Set EMPTY_SET = new EmptySet(); /** * Returns an immutable, serializable parameterized empty set. @@ -279,7 +281,9 @@ public class Collections * @see Serializable * @see RandomAccess */ - public static final List EMPTY_LIST = new EmptyList(); + // Legacy public empty collection constants have no type parameter + @SuppressWarnings("unchecked") + public static final List EMPTY_LIST = new EmptyList(); /** * Returns an immutable, serializable parameterized empty list. @@ -471,7 +475,9 @@ public class Collections * An immutable, serializable, empty Map. * @see Serializable */ - public static final Map EMPTY_MAP = new EmptyMap(); + // Legacy public empty collection constants have no type parameter + @SuppressWarnings("unchecked") + public static final Map EMPTY_MAP = new EmptyMap(); /** * Returns an immutable, serializable parameterized empty map. @@ -628,9 +634,10 @@ public class Collections * clever, but worth it for removing a duplicate of the search code. * Note: This code is also used in Arrays (for sort as well as search). */ + @SuppressWarnings("unchecked") static final int compare(T o1, T o2, Comparator c) { - return c == null ? ((Comparable) o1).compareTo(o2) : c.compare(o1, o2); + return c == null ? ((Comparable) o1).compareTo(o2) : c.compare(o1, o2); } /** @@ -701,7 +708,7 @@ public class Collections // if the list is sequential-access. if (isSequential(l)) { - ListIterator itr = ((List) l).listIterator(); + ListIterator itr = l.listIterator(); int i = 0; T o = itr.next(); // Assumes list is not empty (see isSequential) boolean forward = true; @@ -739,7 +746,7 @@ public class Collections while (low <= hi) { pos = (low + hi) >>> 1; - final int d = compare(((List) l).get(pos), key, c); + final int d = compare(l.get(pos), key, c); if (d == 0) return pos; else if (d > 0) @@ -1188,14 +1195,16 @@ public class Collections */ public static void reverse(List l) { - ListIterator i1 = l.listIterator(); + @SuppressWarnings("unchecked") + ListIterator i1 = (ListIterator) l.listIterator(); int pos1 = 1; int pos2 = l.size(); - ListIterator i2 = l.listIterator(pos2); + @SuppressWarnings("unchecked") + ListIterator i2 = (ListIterator) l.listIterator(pos2); while (pos1 < pos2) { Object o1 = i1.next(); - Object o2 = i2.previous(); + Object o2 = i2.previous(); i1.set(o2); i2.set(o1); ++pos1; @@ -1220,7 +1229,7 @@ public class Collections public static Comparator reverseOrder(final Comparator c) { if (c == null) - return (Comparator) rcInstance; + return reverseOrder(); return new ReverseComparator () { public int compare(T a, T b) @@ -1243,14 +1252,9 @@ public class Collections */ public static Comparator reverseOrder() { - return (Comparator) rcInstance; + return new ReverseComparator(); } - /** - * The object for {@link #reverseOrder()}. - */ - private static final ReverseComparator rcInstance = new ReverseComparator(); - /** * The implementation of {@link #reverseOrder()}. This class name * is required for compatibility with Sun's JDK serializability. @@ -1279,9 +1283,10 @@ public class Collections * @param b the second object * @return <, ==, or > 0 according to b.compareTo(a) */ + @SuppressWarnings("unchecked") public int compare(T a, T b) { - return ((Comparable) b).compareTo(a); + return ((Comparable) b).compareTo(a); } } @@ -1348,7 +1353,9 @@ public class Collections // Now, make the swaps. We must take the remainder every time through // the inner loop so that we don't overflow i to negative values. - List objList = (List) list; + // We need to be able to modify the list + @SuppressWarnings("unchecked") + List objList = (List) list; while (--lcm >= 0) { Object o = objList.get(lcm); @@ -1426,7 +1433,9 @@ public class Collections public static void shuffle(List l, Random r) { int lsize = l.size(); - List list = (List) l; + // We need to be able to modify the list + @SuppressWarnings("unchecked") + List list = (List) l; ListIterator i = list.listIterator(lsize); boolean sequential = isSequential(l); Object[] a = null; // stores a copy of the list for the sequential case @@ -1531,8 +1540,7 @@ public class Collections */ public static boolean disjoint(Collection c1, Collection c2) { - Collection oc1 = (Collection) c1; - final Iterator it = oc1.iterator(); + final Iterator it = c1.iterator(); while (it.hasNext()) if (c2.contains(it.next())) return false; @@ -2112,7 +2120,8 @@ public class Collections */ public static void sort(List l, Comparator c) { - T[] a = (T[]) l.toArray(); + @SuppressWarnings("unchecked") + T[] a = (T[]) l.toArray(); Arrays.sort(a, c); ListIterator i = l.listIterator(); for (int pos = 0, alen = a.length; pos < alen; pos++) @@ -2136,8 +2145,10 @@ public class Collections */ public static void swap(List l, int i, int j) { - List list = (List) l; - list.set(i, list.set(j, list.get(i))); + // We need to be able to modify the list + @SuppressWarnings("unchecked") + List list = (List) l; + list.set(i, list.set(j, l.get(i))); } @@ -3304,7 +3315,7 @@ public class Collections public Set> entrySet() { // Define this here to spare some nesting. - class SynchronizedMapEntry implements Map.Entry + class SynchronizedMapEntry implements Map.Entry { final Map.Entry e; SynchronizedMapEntry(Map.Entry o) @@ -3445,7 +3456,7 @@ public class Collections { synchronized (super.mutex) { - return new SynchronizedMapEntry(super.next()); + return new SynchronizedMapEntry(super.next()); } } }; @@ -4541,7 +4552,7 @@ public class Collections * excessive casting. Package visible for use by subclass. * @serial the wrapped list */ - final List list; + final List list; /** * Wrap a given list. @@ -4551,7 +4562,7 @@ public class Collections UnmodifiableList(List l) { super(l); - list = (List) l; + list = l; } /** @@ -4776,13 +4787,13 @@ public class Collections * The wrapped iterator, stored both here and in the superclass to * avoid excessive casting. */ - private final ListIterator li; + private final ListIterator li; /** * Only trusted code creates a wrapper. * @param li the wrapped iterator */ - UnmodifiableListIterator(ListIterator li) + UnmodifiableListIterator(ListIterator li) { super(li); this.li = li; @@ -4905,7 +4916,7 @@ public class Collections * The wrapped map. * @serial the real map */ - private final Map m; + private final Map m; /** * Cache the entry set. @@ -4927,6 +4938,7 @@ public class Collections * @param m the map to wrap * @throws NullPointerException if m is null */ + @SuppressWarnings("unchecked") UnmodifiableMap(Map m) { this.m = (Map) m; @@ -4993,7 +5005,7 @@ public class Collections public Set> entrySet() { if (entries == null) - entries = new UnmodifiableEntrySet(m.entrySet()); + entries = new UnmodifiableEntrySet(m.entrySet()); return entries; } @@ -5003,18 +5015,18 @@ public class Collections * * @author Eric Blake (ebb9@email.byu.edu) */ - private static final class UnmodifiableEntrySet + private final class UnmodifiableEntrySet extends UnmodifiableSet> implements Serializable { // Unmodifiable implementation of Map.Entry used as return value for // UnmodifiableEntrySet accessors (iterator, toArray, toArray(Object[])) - private static final class UnmodifiableMapEntry + private final class UnmodifiableMapEntry implements Map.Entry { - private final Map.Entry e; + private final Map.Entry e; - private UnmodifiableMapEntry(Map.Entry e) + private UnmodifiableMapEntry(Map.Entry e) { super(); this.e = e; @@ -5097,7 +5109,7 @@ public class Collections * Wrap a given set. * @param s the set to wrap */ - UnmodifiableEntrySet(Set> s) + UnmodifiableEntrySet(Set> s) { super(s); } @@ -5117,7 +5129,7 @@ public class Collections public Map.Entry next() { final Map.Entry e = super.next(); - return new UnmodifiableMapEntry(e); + return new UnmodifiableMapEntry(e); } }; } @@ -5126,21 +5138,23 @@ public class Collections // Map.Entry public Object[] toArray() { - Object[] mapEntryResult = super.toArray(); - UnmodifiableMapEntry result[] = null; + Object[] result = super.toArray(); - if (mapEntryResult != null) - { - result = (UnmodifiableMapEntry[]) - new UnmodifiableMapEntry[mapEntryResult.length]; - for (int i = 0; i < mapEntryResult.length; ++i) - result[i] = new UnmodifiableMapEntry((Map.Entry)mapEntryResult[i]); - } + if (result == null) + return null; + + for (int i = 0; i < result.length; ++i) + { + @SuppressWarnings("unchecked") + Map.Entry entry = (Map.Entry) result[i]; + result[i] = new UnmodifiableMapEntry(entry); + } return result; } // The array returned is an array of UnmodifiableMapEntry instead of // Map.Entry + @SuppressWarnings("unchecked") public S[] toArray(S[] array) { S[] result = super.toArray(array); @@ -5148,7 +5162,7 @@ public class Collections if (result != null) for (int i = 0; i < result.length; i++) array[i] = - (S) new UnmodifiableMapEntry((Map.Entry) result[i]); + (S) new UnmodifiableMapEntry((Map.Entry) result[i]); return array; } @@ -5420,7 +5434,7 @@ public class Collections * excessive casting. * @serial the wrapped map */ - private final SortedMap sm; + private final SortedMap sm; /** * Wrap a given map. @@ -5430,7 +5444,7 @@ public class Collections UnmodifiableSortedMap(SortedMap sm) { super(sm); - this.sm = (SortedMap) sm; + this.sm = sm; } /** @@ -6179,11 +6193,9 @@ public class Collections */ public boolean addAll(int index, Collection coll) { - Collection typedColl = (Collection) coll; - final Iterator it = typedColl.iterator(); - while (it.hasNext()) + for (E entry : coll) { - if (!type.isInstance(it.next())) + if (!type.isInstance(entry)) throw new ClassCastException("A member of the collection is not of the correct type."); } return list.addAll(index, coll); @@ -6635,12 +6647,7 @@ public class Collections { if (entries == null) { - Class> klass = - (Class>) (Class) Map.Entry.class; - entries = new CheckedEntrySet,K,V>(m.entrySet(), - klass, - keyType, - valueType); + entries = new CheckedEntrySet(m.entrySet(), keyType, valueType); } return entries; } @@ -6652,20 +6659,20 @@ public class Collections * @author Andrew John Hughes (gnu_andrew@member.fsf.org) * @since 1.5 */ - private static final class CheckedEntrySet - extends CheckedSet + private final class CheckedEntrySet + extends CheckedSet> { /** * The type of the map's keys. * @serial the key type. */ - private final Class keyType; + private final Class keyType; /** * The type of the map's values. * @serial the value type. */ - private final Class valueType; + private final Class valueType; /** * Wrap a given set of map entries. @@ -6675,18 +6682,19 @@ public class Collections * @param keyType the type of the map's keys. * @param valueType the type of the map's values. */ - CheckedEntrySet(Set s, Class type, Class keyType, - Class valueType) + @SuppressWarnings("unchecked") + CheckedEntrySet(Set> s, Class keyType, + Class valueType) { - super(s, type); + super(s, (Class>) (Class) Map.Entry.class); this.keyType = keyType; this.valueType = valueType; } // The iterator must return checked map entries. - public Iterator iterator() + public Iterator> iterator() { - return new CheckedIterator(c.iterator(), type) + return new CheckedIterator>(c.iterator(), type) { /** * Obtains the next element from the underlying set of @@ -6695,10 +6703,10 @@ public class Collections * @return the next element in the collection. * @throws NoSuchElementException if there are no more elements. */ - public E next() + public Map.Entry next() { - final Map.Entry e = (Map.Entry) super.next(); - return (E) new Map.Entry() + final Map.Entry e = super.next(); + return new Map.Entry() { /** * Returns true if the object, o, is also a map @@ -6717,7 +6725,7 @@ public class Collections * * @return the key. */ - public Object getKey() + public K getKey() { return e.getKey(); } @@ -6727,7 +6735,7 @@ public class Collections * * @return the value. */ - public Object getValue() + public V getValue() { return e.getValue(); } @@ -6754,7 +6762,7 @@ public class Collections * a valid type for the underlying * map. */ - public Object setValue(Object value) + public V setValue(V value) { if (valueType.isInstance(value)) return e.setValue(value); @@ -6883,17 +6891,14 @@ public class Collections */ public void putAll(Map map) { - Map typedMap = (Map) map; - final Iterator> it = typedMap.entrySet().iterator(); - while (it.hasNext()) + for (Map.Entry entry : map.entrySet()) { - final Map.Entry entry = it.next(); if (!keyType.isInstance(entry.getKey())) throw new ClassCastException("A key is of the wrong type."); if (!valueType.isInstance(entry.getValue())) throw new ClassCastException("A value is of the wrong type."); } - m.putAll(typedMap); + m.putAll(map); } /** -- cgit v1.2.1