You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by lv...@apache.org on 2007/11/27 12:29:45 UTC
svn commit: r598602 - in /harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/util/ArrayList.java main/java/java/util/Vector.java
test/api/common/tests/api/java/util/ArrayListTest.java
test/api/common/tests/api/java/util/VectorTest.java
Author: lvjing
Date: Tue Nov 27 03:29:38 2007
New Revision: 598602
URL: http://svn.apache.org/viewvc?rev=598602&view=rev
Log:
Methods in ArrayList and Vector should NOT call public and overridabe method size() to avoid its modification and size effect some problem.
Modified:
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/ArrayList.java
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java
harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/ArrayListTest.java
harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/VectorTest.java
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/ArrayList.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/ArrayList.java?rev=598602&r1=598601&r2=598602&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/ArrayList.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/ArrayList.java Tue Nov 27 03:29:38 2007
@@ -103,7 +103,7 @@
*/
@Override
public void add(int location, E object) {
- int size = size();
+ int size = lastIndex - firstIndex;
if (0 < location && location < size) {
if (firstIndex == 0 && lastIndex == array.length) {
growForInsert(location, 1);
@@ -168,7 +168,7 @@
*/
@Override
public boolean addAll(int location, Collection<? extends E> collection) {
- int size = size();
+ int size = lastIndex - firstIndex;
if (location < 0 || location > size) {
throw new IndexOutOfBoundsException();
}
@@ -330,7 +330,7 @@
*/
@Override
public E get(int location) {
- if (0 <= location && location < size()) {
+ if (0 <= location && location < (lastIndex - firstIndex)) {
return array[firstIndex + location];
}
throw new IndexOutOfBoundsException();
@@ -500,7 +500,7 @@
@Override
public E remove(int location) {
E result;
- int size = size();
+ int size = lastIndex - firstIndex;
if (0 <= location && location < size) {
if (location == size - 1) {
result = array[--lastIndex];
@@ -562,11 +562,11 @@
*/
@Override
protected void removeRange(int start, int end) {
- if (start >= 0 && start <= end && end <= size()) {
+ if (start >= 0 && start <= end && end <= (lastIndex - firstIndex)) {
if (start == end) {
return;
}
- int size = size();
+ int size = lastIndex - firstIndex;
if (end == size) {
Arrays.fill(array, firstIndex + start, lastIndex, null);
lastIndex = firstIndex + start;
@@ -601,7 +601,7 @@
*/
@Override
public E set(int location, E object) {
- if (0 <= location && location < size()) {
+ if (0 <= location && location < (lastIndex - firstIndex)) {
E result = array[firstIndex + location];
array[firstIndex + location] = object;
return result;
@@ -626,7 +626,7 @@
*/
@Override
public Object[] toArray() {
- int size = size();
+ int size = lastIndex - firstIndex;
Object[] result = new Object[size];
System.arraycopy(array, firstIndex, result, 0, size);
return result;
@@ -650,7 +650,7 @@
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] contents) {
- int size = size();
+ int size = lastIndex - firstIndex;
if (size > contents.length) {
Class<?> ct = contents.getClass().getComponentType();
contents = (T[]) Array.newInstance(ct, size);
@@ -668,7 +668,7 @@
* @see #size
*/
public void trimToSize() {
- int size = size();
+ int size = lastIndex - firstIndex;
E[] newArray = newElementArray(size);
System.arraycopy(array, firstIndex, newArray, 0, size);
array = newArray;
@@ -682,7 +682,7 @@
private void writeObject(ObjectOutputStream stream) throws IOException {
ObjectOutputStream.PutField fields = stream.putFields();
- fields.put("size", size()); //$NON-NLS-1$
+ fields.put("size", lastIndex - firstIndex); //$NON-NLS-1$
stream.writeFields();
stream.writeInt(array.length);
Iterator<?> it = iterator();
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java?rev=598602&r1=598601&r2=598602&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Vector.java Tue Nov 27 03:29:38 2007
@@ -400,7 +400,7 @@
}
if (object instanceof List) {
List<?> list = (List<?>) object;
- if (list.size() != size()) {
+ if (list.size() != elementCount) {
return false;
}
@@ -830,7 +830,7 @@
*/
@Override
protected void removeRange(int start, int end) {
- if (start >= 0 && start <= end && end <= size()) {
+ if (start >= 0 && start <= end && end <= elementCount) {
if (start == end) {
return;
}
@@ -1022,7 +1022,7 @@
return "[]"; //$NON-NLS-1$
}
int length = elementCount - 1;
- StringBuffer buffer = new StringBuffer(size() * 16);
+ StringBuffer buffer = new StringBuffer(elementCount * 16);
buffer.append('[');
for (int i = 0; i < length; i++) {
if (elementData[i] == this) {
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/ArrayListTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/ArrayListTest.java?rev=598602&r1=598601&r2=598602&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/ArrayListTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/ArrayListTest.java Tue Nov 27 03:29:38 2007
@@ -540,6 +540,18 @@
list.addAll(0, collection);
assertEquals(14, list.size());
}
+
+ public void test_override_size() throws Exception {
+ ArrayList testlist = new MockArrayList();
+ // though size is overriden, it should passed without exception
+ testlist.add("test_0");
+ testlist.add("test_1");
+ testlist.add("test_2");
+ testlist.add(1,"test_3");
+ testlist.get(1);
+ testlist.remove(2);
+ testlist.set(1, "test_4");
+ }
public static class ArrayListExtend extends ArrayList {
@@ -556,6 +568,12 @@
public int size() {
return size;
+ }
+ }
+
+ public class MockArrayList extends ArrayList {
+ public int size() {
+ return 0;
}
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/VectorTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/VectorTest.java?rev=598602&r1=598601&r2=598602&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/VectorTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/VectorTest.java Tue Nov 27 03:29:38 2007
@@ -17,6 +17,7 @@
package tests.api.java.util;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
@@ -26,6 +27,7 @@
import java.util.NoSuchElementException;
import java.util.Vector;
+import tests.api.java.util.ArrayListTest.MockArrayList;
import tests.support.Support_ListTest;
public class VectorTest extends junit.framework.TestCase {
@@ -907,6 +909,22 @@
String result = v.toString();
assertTrue("should contain self ref", result.indexOf("(this") > -1);
}
+
+ public void test_override_size() throws Exception {
+ Vector v = new Vector();
+ Vector testv = new MockVector();
+ // though size is overriden, it should passed without exception
+ testv.add(1);
+ testv.add(2);
+ testv.clear();
+
+ testv.add(1);
+ testv.add(2);
+ v.add(1);
+ v.add(2);
+ // RI's bug here
+ assertTrue(testv.equals(v));
+ }
/**
* @tests java.util.Vector#trimToSize()
@@ -922,6 +940,13 @@
protected Vector vectorClone(Vector s) {
return (Vector) s.clone();
}
+
+ public class MockVector extends Vector{
+ @Override
+ public synchronized int size() {
+ return 0;
+ }
+ }
/**
* Sets up the fixture, for example, open a network connection. This method