This is the mail archive of the
java-patches@sources.redhat.com
mailing list for the Java project.
PATCH: critical Vector fix and Collections.class fixes
- To: java-patches at sources dot redhat dot com
- Subject: PATCH: critical Vector fix and Collections.class fixes
- From: Bryce McKinlay <bryce at albatross dot co dot nz>
- Date: Mon, 27 Nov 2000 21:42:31 +1300
Somehow I managed introduce a nasty and stupid bug into Vector when
doing the classpath merge. If your apps have been crashing
mysteriously in the past few days then this is why!!
This patch also fixes various problems with the collections wrapper
classes, and makes some ArrayList efficiency improvements.
regards
[ bryce ]
2000-11-27 Bryce McKinlay <bryce@albatross.co.nz>
* java/util/Vector.java (ensureCapacity): Don't increment modCount.
(addElement): Don't increment elementCount twice. Doh.
* java/util/ArrayList.java (add): Only call ensureCapacity if the
array needs to be expanded.
(addAll): Ditto.
* java/util/Collections.java (UnmodifiableCollection): Implement
toString().
(UnmodifiableList): Throw UnsupportedOperationException from
modification methods. Set `l' from the one-parameter constructor.
(UnmodifiableMap): Implement toString().
(SynchronizedCollection): Ditto.
(SynchronizedList): Set `l' from the one-parameter constructor.
(SynchronizedSortedSet): Set `ss' from the one-parameter constructor.
(SynchronizedMap): Implement toString().
Index: java/util/Vector.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/Vector.java,v
retrieving revision 1.9
diff -u -r1.9 Vector.java
--- Vector.java 2000/11/23 05:18:41 1.9
+++ Vector.java 2000/11/27 08:19:19
@@ -178,7 +178,6 @@
*/
public synchronized void ensureCapacity(int minCapacity)
{
- modCount++;
if (elementData.length >= minCapacity)
return;
@@ -459,7 +458,7 @@
public synchronized void addElement(Object obj)
{
if (elementCount == elementData.length)
- ensureCapacity(++elementCount);
+ ensureCapacity(elementCount + 1);
modCount++;
elementData[elementCount++] = obj;
}
Index: java/util/ArrayList.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/ArrayList.java,v
retrieving revision 1.4
diff -u -r1.4 ArrayList.java
--- ArrayList.java 2000/11/22 11:59:59 1.4
+++ ArrayList.java 2000/11/27 08:19:19
@@ -127,7 +127,8 @@
public boolean add(Object e)
{
modCount++;
- ensureCapacity(size + 1);
+ if (size == data.length)
+ ensureCapacity(size + 1);
data[size++] = e;
return true;
}
@@ -204,7 +205,8 @@
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size:" +
size);
- ensureCapacity(size + 1);
+ if (size == data.length)
+ ensureCapacity(size + 1);
if (index != size)
System.arraycopy(data, index, data, index + 1, size - index);
data[index] = e;
@@ -239,7 +241,8 @@
Iterator itr = c.iterator();
int csize = c.size();
- ensureCapacity(size + csize);
+ if (csize + size > data.length)
+ ensureCapacity(size + csize);
int end = index + csize;
if (size > 0 && index != size)
System.arraycopy(data, index, data, end, csize);
Index: java/util/Collections.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/Collections.java,v
retrieving revision 1.1
diff -u -r1.1 Collections.java
--- Collections.java 2000/11/22 11:59:59 1.1
+++ Collections.java 2000/11/27 08:19:19
@@ -43,7 +43,6 @@
*/
public class Collections
{
-
/**
* This class is non-instantiable.
*/
@@ -58,7 +57,6 @@
*/
public static final Set EMPTY_SET = new AbstractSet()
{
-
public int size()
{
return 0;
@@ -97,7 +95,6 @@
*/
public static final List EMPTY_LIST = new AbstractList()
{
-
public int size()
{
return 0;
@@ -116,7 +113,6 @@
*/
public static final Map EMPTY_MAP = new AbstractMap()
{
-
public Set entrySet()
{
return EMPTY_SET;
@@ -147,7 +143,6 @@
*/
private static int search(List l, Object key, final Comparator c)
{
-
int pos = 0;
// We use a linear search using an iterator if we can guess that the list
@@ -437,7 +432,6 @@
// stated - I just would be amazed if it isn't...
public static List nCopies(final int n, final Object o)
{
-
// Check for insane arguments
if (n < 0)
{
@@ -552,7 +546,6 @@
// Iterate backwards over l
while (i.hasPrevious())
{
-
// Obtain a random position to swap with. nextIndex is used so that the
// range of the random number includes the current position.
int swap = r.nextInt(i.nextIndex());
@@ -579,10 +572,8 @@
// It's not serializable because the spec is broken.
public static Set singleton(final Object o)
{
-
return new AbstractSet()
{
-
public int size()
{
return 1;
@@ -592,7 +583,6 @@
{
return new Iterator()
{
-
private boolean hasNext = true;
public boolean hasNext()
@@ -632,10 +622,8 @@
// It's not serializable because the spec is broken.
public static List singletonList(final Object o)
{
-
return new AbstractList()
{
-
public int size()
{
return 1;
@@ -666,7 +654,6 @@
// It's not serializable because the spec is broken.
public static Map singletonMap(final Object key, final Object value)
{
-
return new AbstractMap()
{
public Set entrySet()
@@ -811,7 +798,6 @@
private static class UnmodifiableListIterator extends UnmodifiableIterator
implements ListIterator
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private ListIterator li;
@@ -910,12 +896,15 @@
{
return c.toArray(a);
}
+ public String toString()
+ {
+ return c.toString();
+ }
}
private static class UnmodifiableList extends UnmodifiableCollection
implements List
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
List l;
@@ -928,11 +917,11 @@
public void add(int index, Object o)
{
- l.add(index, o);
+ throw new UnsupportedOperationException();
}
public boolean addAll(int index, Collection c)
{
- return l.addAll(index, c);
+ throw new UnsupportedOperationException();
}
public boolean equals(Object o)
{
@@ -963,16 +952,12 @@
return new UnmodifiableListIterator(l.listIterator(index));
}
public Object remove(int index)
- {
- return l.remove(index);
- }
- public boolean remove(Object o)
{
- return l.remove(o);
+ throw new UnsupportedOperationException();
}
public Object set(int index, Object o)
{
- return l.set(index, o);
+ throw new UnsupportedOperationException();
}
public List subList(int fromIndex, int toIndex)
{
@@ -1000,7 +985,6 @@
private static class UnmodifiableSortedSet extends UnmodifiableSet
implements SortedSet
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private SortedSet ss;
@@ -1039,7 +1023,6 @@
private static class UnmodifiableMap implements Map, Serializable
{
-
Map m;
public UnmodifiableMap(Map m)
@@ -1143,12 +1126,15 @@
{
return new UnmodifiableCollection(m.values());
}
+ public String toString()
+ {
+ return m.toString();
+ }
}
private static class UnmodifiableSortedMap extends UnmodifiableMap
implements SortedMap
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private SortedMap sm;
@@ -1226,7 +1212,6 @@
private static class SynchronizedListIterator extends SynchronizedIterator
implements ListIterator
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private ListIterator li;
@@ -1389,12 +1374,18 @@
return c.toArray(a);
}
}
+ public String toString()
+ {
+ synchronized(sync)
+ {
+ return c.toString();
+ }
+ }
}
private static class SynchronizedList extends SynchronizedCollection
implements List
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
List l;
@@ -1407,6 +1398,7 @@
public SynchronizedList(List l)
{
super(l);
+ this.l = l;
}
public void add(int index, Object o)
@@ -1505,7 +1497,6 @@
private static class SynchronizedSet extends SynchronizedCollection
implements Set
{
-
public SynchronizedSet(Object sync, Set s)
{
super(sync, s);
@@ -1534,7 +1525,6 @@
private static class SynchronizedSortedSet extends SynchronizedSet
implements SortedSet
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private SortedSet ss;
@@ -1547,6 +1537,7 @@
public SynchronizedSortedSet(SortedSet ss)
{
super(ss);
+ this.ss = ss;
}
public Comparator comparator()
@@ -1596,7 +1587,6 @@
private static class SynchronizedMap implements Map, Serializable
{
-
Object sync;
Map m;
@@ -1634,7 +1624,7 @@
}
// This is one of the ickiest cases of nesting I've ever seen. It just
- // means "return an SynchronizedSet, except that the iterator() method
+ // means "return a SynchronizedSet, except that the iterator() method
// returns an SynchronizedIterator whos next() method returns a
// synchronized wrapper around its normal return value".
public Set entrySet()
@@ -1772,12 +1762,18 @@
return new SynchronizedCollection(sync, m.values());
}
}
+ public String toString()
+ {
+ synchronized(sync)
+ {
+ return m.toString();
+ }
+ }
}
private static class SynchronizedSortedMap extends SynchronizedMap
implements SortedMap
{
-
// This is stored both here and in the superclass, to avoid excessive
// casting.
private SortedMap sm;
@@ -1790,6 +1786,7 @@
public SynchronizedSortedMap(SortedMap sm)
{
super(sm);
+ this.sm = sm;
}
public Comparator comparator()