Bug 201985

Summary: Arrays.asList()
Product: [Eclipse Project] JDT Reporter: D.W. <dtw2005>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: VERIFIED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: alex.blewitt, maxime_daniel, Olivier_Thomann
Version: 3.3   
Target Milestone: 3.4 M2   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description D.W. CLA 2007-09-01 16:17:14 EDT
package array;

import java.util.*;

public class ArrayListTest {
	public static void main(String[] args) {
		ArrayList al = new ArrayList();
		al.add("Mike");
		al.add("John");
		al.add("David");

		// ArrayList toArray() returns an Object[] array.
		Object[] objs = al.toArray();

		// fixed List!!! be careful.
		List l = Arrays.asList(objs);
		l.add("Eric");
		/*
		 * Arrays asList() returns a fixed-size list 
		 * Eclipse 3.3 doesn't flag a error for the above l.add("Eric"). 
		 * so I got an exception during the execution
		 * Exception in thread "main"
		 * java.lang.UnsupportedOperationException at
		 * java.util.AbstractList.add(Unknown Source) at
		 * java.util.AbstractList.add(Unknown Source) at
		 * array.ArrayListTest.main(ArrayListTest.java:17)
		 * 
		 */
		System.out.println(l);

	}
}
Comment 1 Alex Blewitt CLA 2007-09-02 04:28:55 EDT
You don't know from the type (List) that it is fixed size. It is just as likely to be a variable-sized one. So unless you can track where the collection is instantiated, you won't find it in the general case.

It doesn't sound sensible to me to have a bunch of functionality that's associated with a single call to an API, but that's just my personal opinion.
Comment 2 Olivier Thomann CLA 2007-09-02 15:35:19 EDT
This is not up to the compiler to know the semantic of the method Arrays.asList(...). It is up to the developer to know that the returned list is a fixed-size list.
Closing as INVALID.
Comment 3 Maxime Daniel CLA 2007-09-17 08:38:14 EDT
Verified for 3.4 M2 using build I20070917-0010.