Bug 123893 - CCE in ResolvedAnnotation
Summary: CCE in ResolvedAnnotation
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.2 M5   Edit
Assignee: Kent Johnson CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-01-14 17:58 EST by Dirk Baeumer CLA
Modified: 2006-02-14 07:37 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dirk Baeumer CLA 2006-01-14 17:58:22 EST
A class cast exception happens in getAllMemberValuePairs due to the fact that the names array is an array of Strings and the comparator cast those elements to char[].

Additionally the insertion loop at the end can produce an AIOBE since the returned value from binarySearch is the negative insertion index and can therefore be less than -1. 

The correct code of the method should be as follows:

		// handle case of more methods than declared members
		char[][] names = new char[declaredLength][];
		for (int i = 0; i < declaredLength; i++)
			names[i] = pairs[i].getName().toCharArray();
		Comparator comparator = new Comparator() {
			public int compare(Object arg0, Object arg1) {
				return CharOperation.compareWith((char[]) arg0, (char[]) arg1);
			}
		};
		Arrays.sort(names, comparator);
		IResolvedMemberValuePair[] allPairs = new  IResolvedMemberValuePair[methodLength];
		for (int i = 0; i < methodLength; i++) {
			int index = Arrays.binarySearch(names, methods[i].selector, comparator);
			allPairs[i] = index < 0 
				? new ResolvedDefaultValuePair(methods[i], this.bindingResolver) 
				: pairs[index];
		}
		return allPairs;

The are some additionally comments here:

- the algorithm sorts the names not the pairs but the index is used at the end 
  to access the pairs not the names. So this only works if all names are found
  since the picked pair might not match the name for which the binary search
  is used.
Comment 1 Kent Johnson CLA 2006-01-16 15:48:49 EST
SO much for inheriting code.

Dirk, do you have a case we can test against?
Comment 2 Dirk Baeumer CLA 2006-01-17 04:19:32 EST
Not a public one. However making a test case is simple:

- create an annotation with two default values and a non default value.
- when sorted the default value should come after the non default value
- reference the annotation only providing the non default value
- resolve an IResolvedAnnotation from it
- call getAllMemberValuePairs

This should produce both exception.
Comment 3 David Audel CLA 2006-02-14 07:37:46 EST
Verified for 3.2 M5 using build I20060214-0010