Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 12b2bc54e45889313468787fda5d54ea9271ccbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Tim Hanson <thanson@bea.com> - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=137634
 *******************************************************************************/
package org.eclipse.jdt.internal.core.builder;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;

public class ReferenceCollection {

// contains no simple names as in just 'a' which is kept in simpleNameReferences instead
// TODO after #addDependencies, it will contain simple names, though. See ReferenceCollectionTest
char[][][] qualifiedNameReferences;
char[][] simpleNameReferences;
char[][] rootReferences;

protected ReferenceCollection(char[][][] qualifiedNameReferences, char[][] simpleNameReferences, char[][] rootReferences) {
	this.qualifiedNameReferences = internQualifiedNames(qualifiedNameReferences, false);
	this.simpleNameReferences = internSimpleNames(simpleNameReferences, true);
	this.rootReferences = internSimpleNames(rootReferences, false);
}

/**
 * Add the given fully qualified names to this reference collection.
 * Subsequent queries of {@link #includes(char[][][], char[][], char[][])} will report true
 * if the given names intersect with one of the added type name dependencies.
 *
 * @see CompilationUnitScope#recordQualifiedReference
 */
public void addDependencies(String[] typeNameDependencies) {
	// if each qualified type name is already known then all of its subNames can be skipped
	// and its expected that very few qualified names in typeNameDependencies need to be added
	// but could always take 'p1.p2.p3.X' and make all qualified names 'p1' 'p1.p2' 'p1.p2.p3' 'p1.p2.p3.X', then intern
	next: for(String typeNameDependency: typeNameDependencies) {
		char[][] qualifiedTypeName = CharOperation.splitOn('.', typeNameDependency.toCharArray());
		if (!isWellKnownQualifiedName(qualifiedTypeName)) {
			int qLength = qualifiedTypeName.length;
			QualifiedNameSet internedNames = InternedQualifiedNames[qLength <= MaxQualifiedNames ? qLength - 1 : 0];
			qualifiedTypeName = internSimpleNames(qualifiedTypeName, false, false);
			qualifiedTypeName = internedNames.add(qualifiedTypeName);
			int idx;
			while ((idx = Arrays.binarySearch(this.qualifiedNameReferences, qualifiedTypeName, CHAR_CHAR_ARR_COMPARATOR)) < 0) {
				this.simpleNameReferences = ensureContainedInSortedOrder(this.simpleNameReferences, qualifiedTypeName[qualifiedTypeName.length - 1]);
				this.rootReferences = ensureContainedInSortedOrder(this.rootReferences, qualifiedTypeName[0]);

				int length = this.qualifiedNameReferences.length;
				idx = -(idx+1);
				char[][][] newArray = new char[length + 1][][];
				insertIntoArray(this.qualifiedNameReferences, this.qualifiedNameReferences = newArray, qualifiedTypeName, idx);

				qualifiedTypeName = CharOperation.subarray(qualifiedTypeName, 0, qualifiedTypeName.length - 1);
				char[][][] temp = internQualifiedNames(new char[][][] {qualifiedTypeName}, false);
				if (temp == EmptyQualifiedNames)
					continue next; // qualifiedTypeName is a well known name
				qualifiedTypeName = temp[0];
			}
		}
	}
}

public boolean includes(char[] simpleName) {
	boolean result = sortedArrayContains(this.simpleNameReferences, simpleName, CHAR_ARR_COMPARATOR);
	if (REFERENCE_COLLECTION_DEBUG) {
		assertIncludes(result, simpleName);
	}
	return result;
}

public boolean includes(char[][] qualifiedName) {
	boolean result = sortedArrayContains(this.qualifiedNameReferences, qualifiedName, CHAR_CHAR_ARR_COMPARATOR);
	if (REFERENCE_COLLECTION_DEBUG) {
		assertIncludes(result, qualifiedName);
	}
	return result;
}

private static String qualifiedNamesToString(char[][][] qualifiedNames) {
	if (qualifiedNames == null)
		return "null"; //$NON-NLS-1$
	return Arrays.stream(qualifiedNames).map(CharOperation::toString).collect(Collectors.joining(",")); //$NON-NLS-1$
}

/**
 * @deprecated
 */
public boolean includes(char[][][] qualifiedNames, char[][] simpleNames) {
	return includes(qualifiedNames, simpleNames, null);
}

public boolean includes(char[][][] qualifiedNames, char[][] simpleNames, char[][] rootNames) {
	boolean result = doIncludes(qualifiedNames, simpleNames, rootNames);
	if (REFERENCE_COLLECTION_DEBUG) {
		assertIncludes(result, qualifiedNames, simpleNames, rootNames);
	}
	return result;
}

private boolean doIncludes(char[][][] qualifiedNames, char[][] simpleNames, char[][] rootNames) {
	if (rootNames != null) {
		if (!includesRootName(rootNames))
			return false;
	}
	// if either collection of names is null, it means it contained a well known name so we know it already has a match
	if (simpleNames == null || qualifiedNames == null) {
		if (simpleNames == null && qualifiedNames == null) {
			if (JavaBuilder.DEBUG)
				System.out.println("Found well known match"); //$NON-NLS-1$
			return true;
		} else if (qualifiedNames == null) {
			return includesSimpleName(simpleNames);
		}
		return includesQualifiedName(qualifiedNames);
	}

	if (simpleNames.length <= qualifiedNames.length) {
		return includesSimpleName(simpleNames) && includesQualifiedName(qualifiedNames);
	} else {
		return includesQualifiedName(qualifiedNames) && includesSimpleName(simpleNames);
	}
}

public boolean insideRoot(char[] rootName) {
	boolean result = sortedArrayContains(this.rootReferences, rootName, CHAR_ARR_COMPARATOR);
	if (REFERENCE_COLLECTION_DEBUG) {
		if (result != debugIncludes(rootName)) {
			String message = "Mismatch: " + String.valueOf(rootName) + (result ? " should not " : " should ") + " be included in "  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
					+ Arrays.asList(CharOperation.toStrings(this.rootReferences));
			throw new IllegalStateException(message);
		}
	}
	return result;
}

private static <T> boolean sortedArrayContains(T[] array, T element, Comparator<? super T> comparator) {
	int l = array.length;
	if (l < BINARY_SEARCH_THRESHOLD) {
		for (int i = 0; i < l; i++)
			if (element == array[i]) return true;
		return false;
	}
	return Arrays.binarySearch(array, element, comparator) >= 0;
}

private boolean includesSimpleName(char[][] simpleNames) {
	return intersects(simpleNames, this.simpleNameReferences, CHAR_ARR_COMPARATOR);
}

private boolean includesQualifiedName(char[][][] qualifiedNames) {
	if (intersects(qualifiedNames, this.qualifiedNameReferences, CHAR_CHAR_ARR_COMPARATOR)) {
		return true;
	}
	char[][] maybeSimpleName;
	for(int i = qualifiedNames.length - 1; i >= 0 && (maybeSimpleName = qualifiedNames[i]).length == 1; i--) {
		if (includes(maybeSimpleName[0])) {
			return true;
		}
	}
	return false;
}

private boolean includesRootName(char[][] rootNames) {
	return intersects(rootNames, this.rootReferences, CHAR_ARR_COMPARATOR);
}

// TODO there may be better thresholds available for different scenarios
private static final int BINARY_SEARCH_THRESHOLD = 16;
private static <T> boolean intersects(T[] firstSortedArr, T[] secondSortedArr, Comparator<? super T> comparator) {
	/*
	 * Both arrays are sorted, so we can walk them in pairs.
	 * Using binary search for the remaining array elements to figure the next
	 * interesting index can greatly reduce the runtime cost for arrays that do
	 * have more than a few elements.
	 */
	for(int i = 0, l = firstSortedArr.length, j = 0, k = secondSortedArr.length; i < l && j < k;) {
		T firstElement = firstSortedArr[i];
		T secondElement = secondSortedArr[j];
		int compare = comparator.compare(firstElement, secondElement);
		if (compare == 0) {
			return true;
		} else if (compare < 0) {
			/*
			 * left side is smaller than the right side, but not exactly the right side.
			 * Take the next element from the left and proceed.
			 *
			 * If the number of remaining elements in the first array is sufficiently big,
			 * attempt a binary search for the second element to possibly skip a few elements.
			 */
			i++;
			if (l - i > BINARY_SEARCH_THRESHOLD) {
				i = Arrays.binarySearch(firstSortedArr, i, l, secondElement, comparator);
				if (i >= 0) {
					return true;
				}
				i = -(i + 1);
			}
		} else {
			/*
			 * the inverse logic is applied here
			 */
			j++;
			if (k - j > BINARY_SEARCH_THRESHOLD) {
				j = Arrays.binarySearch(secondSortedArr, j, k, firstElement, comparator);
				if (j >= 0) {
					return true;
				}
				j = -(j + 1);
			}
		}
	}
	return false;
}

// Generifying this appears to be pointless since arracopy takes an Object array anyways.
private static void insertIntoArray(Object[] src, Object[] target, Object entry, int idx) {
	System.arraycopy(src, 0, target, 0, idx);
	target[idx] = entry;
	System.arraycopy(src, idx, target, idx+1, src.length - idx);
}

private static char[][] ensureContainedInSortedOrder(char[][] sortedArray, char[] entry) {
	int idx = Arrays.binarySearch(sortedArray, entry, CHAR_ARR_COMPARATOR);
	if (idx < 0) {
		idx = -(idx + 1);
		char[][] result = new char[sortedArray.length + 1][];
		insertIntoArray(sortedArray, result, entry, idx);
		return result;
	}
	return sortedArray;
}

private static boolean isWellKnownQualifiedName(char[][] qualifiedName) {
	for (int i = 0, m = WellKnownQualifiedNames.length, qLength = qualifiedName.length; i < m; i++) {
		char[][] wellKnownName = WellKnownQualifiedNames[i];
		if (qLength > wellKnownName.length)
			break; // all remaining well known names are shorter
		if (CharOperation.equals(qualifiedName, wellKnownName)) {
			return true;
		}
	}
	return false;
}

// When any type is compiled, its methods are verified for certain problems
// the MethodVerifier requests 3 well known types which end up in the reference collection
// having WellKnownQualifiedNames & WellKnownSimpleNames, saves every type 40 bytes
// NOTE: These collections are sorted by length
static final char[][][] WellKnownQualifiedNames = new char[][][] {
	TypeConstants.JAVA_LANG_RUNTIMEEXCEPTION,
	TypeConstants.JAVA_LANG_THROWABLE,
	TypeConstants.JAVA_LANG_OBJECT,
	TypeConstants.JAVA_LANG,
	new char[][] {TypeConstants.JAVA},
	new char[][] {new char[] {'o', 'r', 'g'}},
	new char[][] {new char[] {'c', 'o', 'm'}},
	CharOperation.NO_CHAR_CHAR}; // default package
static final char[][] WellKnownSimpleNames = new char[][] {
	TypeConstants.JAVA_LANG_RUNTIMEEXCEPTION[2],
	TypeConstants.JAVA_LANG_THROWABLE[2],
	TypeConstants.JAVA_LANG_OBJECT[2],
	TypeConstants.JAVA,
	TypeConstants.LANG,
	new char[] {'o', 'r', 'g'},
	new char[] {'c', 'o', 'm'}};

static final char[][][] EmptyQualifiedNames = new char[0][][];
static final char[][] EmptySimpleNames = CharOperation.NO_CHAR_CHAR;

// each array contains qualified char[][], one for size 2, 3, 4, 5, 6, 7 & the rest
static final int MaxQualifiedNames = 7;
static QualifiedNameSet[] InternedQualifiedNames = new QualifiedNameSet[MaxQualifiedNames];
// each array contains simple char[], one for size 1 to 29 & the rest
static final int MaxSimpleNames = 30;
static NameSet[] InternedSimpleNames = new NameSet[MaxSimpleNames];
static {
	for (int i = 0; i < MaxQualifiedNames; i++)
		InternedQualifiedNames[i] = new QualifiedNameSet(37);
	for (int i = 0; i < MaxSimpleNames; i++)
		InternedSimpleNames[i] = new NameSet(37);
}

//TODO: remove once ReferenceCollection.internQualifiedNames(StringSet) is adapted to use java.util.Set, so that git history is preserved
public static char[][][] internQualifiedNames(Set<String> qualifiedStrings) {
	if (qualifiedStrings == null) return EmptyQualifiedNames;
	int length = qualifiedStrings.size();
	if (length == 0) return EmptyQualifiedNames;

	char[][][] result = new char[length][][];
	for (String qualifiedString : qualifiedStrings)
		if (qualifiedString != null)
			result[--length] = CharOperation.splitOn('/', qualifiedString.toCharArray());
	return internQualifiedNames(result, false);
}

//TODO: remove once PDE API Tools has been adapted to also use java.util.Set, so that git history is preserved
public static char[][][] internQualifiedNames(StringSet qualifiedStrings) {
	if (qualifiedStrings == null) return EmptyQualifiedNames;
	int length = qualifiedStrings.elementSize;
	if (length == 0) return EmptyQualifiedNames;

	char[][][] result = new char[length][][];
	String[] strings = qualifiedStrings.values;
	for (int i = 0, l = strings.length; i < l; i++)
		if (strings[i] != null)
			result[--length] = CharOperation.splitOn('/', strings[i].toCharArray());
	return internQualifiedNames(result, false);
}

public static char[][][] internQualifiedNames(char[][][] qualifiedNames) {
	return internQualifiedNames(qualifiedNames, false);
}

/**
 * Use a flyweight cache for the char arrays to avoid duplicated arrays with the same contents.
 * After calling this method, identity comparison on the array contents of the resulting array
 * will work for arrays with equal content.
 *
 * Optionally drops very common qualified names from the array to spare some bytes.
 *
 * @return a new array with interned elements.
 */
public static char[][][] internQualifiedNames(char[][][] qualifiedNames, boolean keepWellKnown) {
	if (qualifiedNames == null) return EmptyQualifiedNames;
	int length = qualifiedNames.length;
	if (length == 0) return EmptyQualifiedNames;

	char[][][] keepers = new char[length][][];
	char[][] prev = null;
	boolean isSorted = true;
	int index = 0;

	next : for (int i = 0; i < length; i++) {
		char[][] qualifiedName = qualifiedNames[i];
		int qLength = qualifiedName.length;
		for (int j = 0, m = WellKnownQualifiedNames.length; j < m; j++) {
			char[][] wellKnownName = WellKnownQualifiedNames[j];
			if (qLength > wellKnownName.length)
				break; // all remaining well known names are shorter
			if (CharOperation.equals(qualifiedName, wellKnownName)) {
				if (keepWellKnown) {
					// This code is duplicated to encourage the JIT to inline more stuff
					if (isSorted) {
						if (prev != null && compareCharCharArray(prev, qualifiedName) > 0) {
							isSorted = false;
						}
						prev = qualifiedName;
					}
					keepers[index++] = wellKnownName;
				}
				continue next;
			}
		}

		// InternedQualifiedNames[0] is for the rest (> 7 & 1)
		// InternedQualifiedNames[1] is for size 2...
		// InternedQualifiedNames[6] is for size 7
		QualifiedNameSet internedNames = InternedQualifiedNames[qLength <= MaxQualifiedNames ? qLength - 1 : 0];
		qualifiedName = internSimpleNames(qualifiedName, false, false);
		// This code is duplicated to encourage the JIT to inline more stuff
		if (isSorted) {
			if (prev != null && compareCharCharArray(prev, qualifiedName) > 0) {
				isSorted = false;
			}
			prev = qualifiedName;
		}
		keepers[index++] = internedNames.add(qualifiedName);
	}
	if (length > index) {
		if (index == 0) return EmptyQualifiedNames;
		System.arraycopy(keepers, 0, keepers = new char[index][][], 0, index);
	}
	if (!isSorted) {
		Arrays.sort(keepers, CHAR_CHAR_ARR_COMPARATOR);
	}
	return keepers;
}

/**
 * Compares the two char arrays.
 * Longer arrays are considered to be smaller than shorter arrays.
 * Arrays with the same length are compared char by char lexicographically.
 *
 * @see Character#compare(char, char)
 */
private static int compareCharArray(char[] left, char[] right){
	if (left == right) {
		return 0;
	}
	int l = left.length;
	int diff = right.length - l;
	if (diff == 0) {
		for(int i = 0; i < l && (diff = left[i] - right[i]) == 0; i++) {
			// all logic is in the loop header
		}
	}
	return diff;
}
private static final Comparator<char[]> CHAR_ARR_COMPARATOR = ReferenceCollection::compareCharArray;

/**
 * Compares the two char-char arrays.
 * Longer arrays are considered to be smaller than shorter arrays.
 * Arrays with the same length are compared according to the logic in {@link #compareCharArray(char[], char[])}.
 */
static int compareCharCharArray(char[][] left, char[][]right) {
	if (left == right) {
		return 0;
	}
	int l = left.length;
	int diff = right.length - l;
	if (diff == 0) {
		for(int i = 0; i < l && (diff = compareCharArray(left[i], right[i])) == 0; i++) {
			// all logic is in the loop header
		}
	}
	return diff;
}
private static final Comparator<char[][]> CHAR_CHAR_ARR_COMPARATOR = ReferenceCollection::compareCharCharArray;

/**
 * @deprecated
 */
public static char[][] internSimpleNames(Set<String> simpleStrings) {
	return internSimpleNames(simpleStrings, true);
}

// TODO: remove once ReferenceCollection.internSimpleNames(StringSet, boolean) is adapted to use java.util.Set, so that git history is preserved
public static char[][] internSimpleNames(Set<String> simpleStrings, boolean removeWellKnown) {
	if (simpleStrings == null) return EmptySimpleNames;
	int length = simpleStrings.size();
	if (length == 0) return EmptySimpleNames;

	char[][] result = new char[length][];
	for (String simpleString : simpleStrings)
		if (simpleString != null)
			result[--length] = simpleString.toCharArray();
	return internSimpleNames(result, removeWellKnown);
}

//TODO: adjust to use java.util.Set once PDE API Tools have been adapted to use the set version, so that git history is preserved
public static char[][] internSimpleNames(StringSet simpleStrings, boolean removeWellKnown) {
	if (simpleStrings == null) return EmptySimpleNames;
	int length = simpleStrings.elementSize;
	if (length == 0) return EmptySimpleNames;

	char[][] result = new char[length][];
	String[] strings = simpleStrings.values;
	for (int i = 0, l = strings.length; i < l; i++)
		if (strings[i] != null)
			result[--length] = strings[i].toCharArray();
	return internSimpleNames(result, removeWellKnown);
}
/**
 * Use a flyweight cache for the char arrays to avoid duplicated arrays with the same contents.
 * After calling this method, identity comparison on the array contents of the resulting array
 * will work for arrays with equal content.
 *
 * Optionally drops very common qualified names from the array to spare some bytes.
 *
 * @return a new array with interned elements.
 */
public static char[][] internSimpleNames(char[][] simpleNames, boolean removeWellKnown) {
	return internSimpleNames(simpleNames, removeWellKnown, true);
}
private static char[][] internSimpleNames(char[][] simpleNames, boolean removeWellKnown, boolean doSort) {
	if (simpleNames == null) return EmptySimpleNames;
	int length = simpleNames.length;
	if (length == 0) return EmptySimpleNames;

	char[][] keepers = new char[length][];
	char[] prev = null;
	boolean isSorted = true;
	int index = 0;
	next : for (int i = 0; i < length; i++) {
		char[] name = simpleNames[i];
		int sLength = name.length;
		for (int j = 0, m = WellKnownSimpleNames.length; j < m; j++) {
			char[] wellKnownName = WellKnownSimpleNames[j];
			if (sLength > wellKnownName.length)
				break; // all remaining well known names are shorter
			if (CharOperation.equals(name, wellKnownName)) {
				if (!removeWellKnown) {
					keepers[index++] = wellKnownName;
					// This code is duplicated to encourage the JIT to inline more stuff
					if (doSort && isSorted) {
						if (prev != null && compareCharArray(prev, name) > 0) {
							isSorted = false;
						}
						prev = name;
					}
				}
				continue next;
			}
		}

		// InternedSimpleNames[0] is for the rest (> 29)
		// InternedSimpleNames[1] is for size 1...
		// InternedSimpleNames[29] is for size 29
		NameSet internedNames = InternedSimpleNames[sLength < MaxSimpleNames ? sLength : 0];
		keepers[index++] = internedNames.add(name);
		// This code is duplicated to encourage the JIT to inline more stuff
		if (doSort && isSorted) {
			if (prev != null && compareCharArray(prev, name) > 0) {
				isSorted = false;
			}
			prev = name;
		}
	}
	if (length > index) {
		if (index == 0) return EmptySimpleNames;
		System.arraycopy(keepers, 0, keepers = new char[index][], 0, index);
	}
	if (doSort && !isSorted) {
		Arrays.sort(keepers, CHAR_ARR_COMPARATOR);
	}
	return keepers;
}

// DEBUG code below
public static boolean REFERENCE_COLLECTION_DEBUG = false;

private void assertIncludes(boolean expectation, char[] simpleName) {
	if (expectation != debugIncludes(simpleName)) {
		String message = "Mismatch: " + String.valueOf(simpleName) + (expectation ? " should not " : " should ") + " be included in "  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				+ Arrays.asList(CharOperation.toStrings(this.simpleNameReferences));
		throw new IllegalStateException(message);
	}
}

private void assertIncludes(boolean expectation, char[][] qualifiedName) {
	if (expectation != debugIncludes(qualifiedName)) {
		String message = "Mismatch: " + CharOperation.toString(qualifiedName) + (expectation ? " should not " : " should ") + " be included in "  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				+ qualifiedNamesToString(this.qualifiedNameReferences);
		throw new IllegalStateException(message);
	}
}

private void assertIncludes(boolean expectation, char[][][] qualifiedNames, char[][] simpleNames, char[][] rootNames) {
	if (expectation != debugIncludes(qualifiedNames, simpleNames, rootNames)) {
		String message = String.format("Mismatched includes(..): ReferenceCollection([%s], %s, %s).includes([%s], %s, %s)", //$NON-NLS-1$
				qualifiedNamesToString(this.qualifiedNameReferences),
				Arrays.toString(CharOperation.toStrings(this.simpleNameReferences)),
				Arrays.toString(CharOperation.toStrings(this.rootReferences)),
				qualifiedNamesToString(qualifiedNames),
				Arrays.toString(CharOperation.toStrings(simpleNames)),
				Arrays.toString(CharOperation.toStrings(rootNames))
		);
		throw new IllegalStateException(message);
	}
}

private boolean debugIncludes(char[][][] qualifiedNames, char[][] simpleNames, char[][] rootNames) {
	// if either collection of names is null, it means it contained a well known name so we know it already has a match
	if (rootNames != null) {
		boolean foundRoot = false;
		for (int i = 0, l = rootNames.length; !foundRoot && i < l; i++)
			foundRoot = debugInsideRoot(rootNames[i]);
		if (!foundRoot)
			return false;
	}
	if (simpleNames == null || qualifiedNames == null) {
		if (simpleNames == null && qualifiedNames == null) {
			if (JavaBuilder.DEBUG)
				System.out.println("Found well known match"); //$NON-NLS-1$
			return true;
		} else if (qualifiedNames == null) {
			for (int i = 0, l = simpleNames.length; i < l; i++) {
				if (debugIncludes(simpleNames[i])) {
					if (JavaBuilder.DEBUG)
						System.out.println("Found match in well known package to " + new String(simpleNames[i])); //$NON-NLS-1$
					return true;
				}
			}
		} else {
			for (int i = 0, l = qualifiedNames.length; i < l; i++) {
				char[][] qualifiedName = qualifiedNames[i];
				if (qualifiedName.length == 1 ? debugIncludes(qualifiedName[0]) : debugIncludes(qualifiedName)) {
					if (JavaBuilder.DEBUG)
						System.out.println("Found well known match in " + CharOperation.toString(qualifiedName)); //$NON-NLS-1$
					return true;
				}
			}
		}
		return false;
	}

	int sLength = simpleNames.length;
	int qLength = qualifiedNames.length;
	if (sLength <= qLength) {
		for (int i = 0; i < sLength; i++) {
			if (debugIncludes(simpleNames[i])) {
				for (int j = 0; j < qLength; j++) {
					char[][] qualifiedName = qualifiedNames[j];
					if (qualifiedName.length == 1 ? debugIncludes(qualifiedName[0]) : debugIncludes(qualifiedName)) {
						if (JavaBuilder.DEBUG)
							System.out.println("Found match in " + CharOperation.toString(qualifiedName) //$NON-NLS-1$
								+ " to " + new String(simpleNames[i])); //$NON-NLS-1$
						return true;
					}
				}
				return false;
			}
		}
	} else {
		for (int i = 0; i < qLength; i++) {
			char[][] qualifiedName = qualifiedNames[i];
			if (qualifiedName.length == 1 ? debugIncludes(qualifiedName[0]) : debugIncludes(qualifiedName)) {
				for (int j = 0; j < sLength; j++) {
					if (debugIncludes(simpleNames[j])) {
						if (JavaBuilder.DEBUG)
							System.out.println("Found match in " + CharOperation.toString(qualifiedName) //$NON-NLS-1$
								+ " to " + new String(simpleNames[j])); //$NON-NLS-1$
						return true;
					}
				}
				return false;
			}
		}
	}
	return false;
}

private boolean debugInsideRoot(char[] rootName) {
	for (int i = 0, l = this.rootReferences.length; i < l; i++)
		if (rootName == this.rootReferences[i]) return true;
	return false;
}

private boolean debugIncludes(char[] simpleName) {
	for (int i = 0, l = this.simpleNameReferences.length; i < l; i++)
		if (simpleName == this.simpleNameReferences[i]) return true;
	return false;
}

private boolean debugIncludes(char[][] qualifiedName) {
	for (int i = 0, l = this.qualifiedNameReferences.length; i < l; i++)
		if (qualifiedName == this.qualifiedNameReferences[i]) return true;
	return false;
}

}

Back to the top