View | Details | Raw Unified | Return to bug 338006 | Differences between
and this patch

Collapse All | Expand All

(-)model/org/eclipse/jdt/core/IJavaProject.java (-2 / +5 lines)
Lines 49-55 Link Here
49
 * Java project elements need to be opened before they can be navigated or manipulated.
49
 * Java project elements need to be opened before they can be navigated or manipulated.
50
 * The children of a Java project are the package fragment roots that are
50
 * The children of a Java project are the package fragment roots that are
51
 * defined by the classpath and contained in this project (in other words, it
51
 * defined by the classpath and contained in this project (in other words, it
52
 * does not include package fragment roots for other projects).
52
 * does not include package fragment roots for other projects). The children
53
 * (i.e. the package fragment roots) appear in the order they are defined by 
54
 * the classpath.
53
 * </p>
55
 * </p>
54
 * <p>
56
 * <p>
55
 * An instance of one of these handles can be created via
57
 * An instance of one of these handles can be created via
Lines 518-524 Link Here
518
	 * Returns all of the  package fragment roots contained in this
520
	 * Returns all of the  package fragment roots contained in this
519
	 * project, identified on this project's resolved classpath. The result
521
	 * project, identified on this project's resolved classpath. The result
520
	 * does not include package fragment roots in other projects referenced
522
	 * does not include package fragment roots in other projects referenced
521
	 * on this project's classpath.
523
	 * on this project's classpath. The package fragment roots appear in the 
524
	 * order they are defined by the classpath.
522
	 *
525
	 *
523
	 * <p>NOTE: This is equivalent to <code>getChildren()</code>.
526
	 * <p>NOTE: This is equivalent to <code>getChildren()</code>.
524
	 *
527
	 *
(-)model/org/eclipse/jdt/internal/core/DeltaProcessor.java (-2 / +50 lines)
Lines 282-289 Link Here
282
		if (parent != null && parent.isOpen()) {
282
		if (parent != null && parent.isOpen()) {
283
			try {
283
			try {
284
				OpenableElementInfo info = (OpenableElementInfo) parent.getElementInfo();
284
				OpenableElementInfo info = (OpenableElementInfo) parent.getElementInfo();
285
				info.addChild(child);
285
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=338006
286
			} catch (JavaModelException e) {
286
				// Insert the package fragment roots in the same order as the classpath order.
287
				iterate: if (child instanceof IPackageFragmentRoot) {
288
					IJavaElement[] roots = info.getChildren();
289
					if (roots.length == 0)
290
						break iterate;
291
					
292
					IPackageFragmentRoot root = (IPackageFragmentRoot) child;
293
					IClasspathEntry[] resolvedClasspath = ((JavaProject)root.getJavaProject()).getResolvedClasspath();
294
					IClasspathEntry currentEntry = root.getResolvedClasspathEntry();
295
					
296
					int indexToInsert = -1;
297
					int lastComparedIndex = -1;
298
					for (int i =0, j=0; i < roots.length && j < resolvedClasspath.length; ) {
299
						
300
						// If the new root is already among the children, no need to proceed further. Just return.
301
						// This condition will be checked even if indexToInsert has already been found 
302
						if(roots[i].getPath().equals(child.getPath())) {
303
							return;
304
						}
305
						
306
						if (indexToInsert == -1) {
307
							if ( lastComparedIndex != j && currentEntry.getPath().equals(resolvedClasspath[j].getPath())) {
308
								indexToInsert = i;
309
								break;
310
							}
311
							lastComparedIndex = j;
312
313
							IClasspathEntry rootEntry = ((IPackageFragmentRoot)roots[i]).getResolvedClasspathEntry();
314
							if (rootEntry.getPath().equals(resolvedClasspath[j].getPath()))
315
								i++;
316
							else
317
								j++;
318
						}
319
					}
320
					if (indexToInsert >= 0) {
321
						int newSize = roots.length + 1;
322
						IPackageFragmentRoot[] newChildren = new IPackageFragmentRoot[newSize];
323
						
324
						if(indexToInsert > 0) 
325
							System.arraycopy(roots, 0, newChildren, 0, indexToInsert);
326
						
327
						newChildren[indexToInsert] = (IPackageFragmentRoot)child;
328
						System.arraycopy(roots, indexToInsert, newChildren, indexToInsert+1, (newSize - indexToInsert -1));
329
						info.setChildren(newChildren);
330
						return;
331
					}
332
				}
333
 				info.addChild(child);
334
 			} catch (JavaModelException e) {
287
				// do nothing - we already checked if open
335
				// do nothing - we already checked if open
288
			}
336
			}
289
		}
337
		}
(-)src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java (+19 lines)
Lines 955-960 Link Here
955
		}
955
		}
956
		assertEquals(expected, actual);
956
		assertEquals(expected, actual);
957
	}
957
	}
958
	protected void assertPackageFragmentRootsEqual(IPackageFragmentRoot[] roots, String expected) {
959
		String actual;
960
		if (roots == null) {
961
			actual = "<null>";
962
		} else {
963
			StringBuffer buffer = new StringBuffer();
964
			int length = roots.length;
965
			for (int i=0; i<length; i++) {
966
				buffer.append(roots[i]);
967
				if (i < length-1)
968
					buffer.append('\n');
969
			}
970
			actual = buffer.toString();
971
		}
972
		if (!actual.equals(expected)) {
973
		 	System.out.print(displayString(actual, 2));
974
		}
975
		assertEquals(expected, actual);		
976
	}
958
	/**
977
	/**
959
	 * Ensures the element is present after creation.
978
	 * Ensures the element is present after creation.
960
	 */
979
	 */
(-)src/org/eclipse/jdt/core/tests/model/ClasspathTests.java (+40 lines)
Lines 6912-6915 Link Here
6912
	}	
6912
	}	
6913
}
6913
}
6914
6914
6915
/**
6916
 * @bug 338006: IJavaProject#getPackageFragmentRoots() should return roots in order
6917
 * 
6918
 * Test whether the {@link IJavaProject#getPackageFragmentRoots()} returns the package fragment
6919
 * roots in the same order as defined by the .classpath even when the elements are added in a 
6920
 * different order.
6921
 * 
6922
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=338006"
6923
 */
6924
public void testBug338006() throws Exception {
6925
	try {
6926
		IJavaProject p = this.createJavaProject("P", new String[] {}, "bin");
6927
6928
		String content = new String(
6929
				"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
6930
				+ "<classpath>\n"
6931
				+ "<classpathentry kind=\"src\" path=\"src a\"/>\n"
6932
				+ "<classpathentry kind=\"src\" path=\"src x\"/>\n"
6933
				+ "<classpathentry kind=\"lib\" path=\""
6934
				+ getExternalJCLPath()
6935
				+ "\"/>\n"
6936
				+ "<classpathentry kind=\"output\" path=\"bin\"/>\n"
6937
				+ "</classpath>\n");
6938
6939
		editFile("/P/.classpath", content);
6940
		p.open(null);
6941
		createFolder("/P/src x");
6942
		createFolder("/P/src a");
6943
6944
		IPackageFragmentRoot[] roots = p.getPackageFragmentRoots();
6945
		assertPackageFragmentRootsEqual(roots, 
6946
				"src a (not open) [in P]\n" + 
6947
				"src x (not open) [in P]\n" + 
6948
				""+ getExternalJCLPathString() + " (not open)");
6949
6950
	} finally {
6951
		deleteProject("P");
6952
	}
6953
}
6954
6915
}
6955
}

Return to bug 338006