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

Collapse All | Expand All

(-)model/org/eclipse/jdt/core/IJavaProject.java (-1 / +3 lines)
Lines 520-526 Link Here
520
	 * does not include package fragment roots in other projects referenced
520
	 * does not include package fragment roots in other projects referenced
521
	 * on this project's classpath.
521
	 * on this project's classpath.
522
	 *
522
	 *
523
	 * <p>NOTE: This is equivalent to <code>getChildren()</code>.
523
	 * <p>The package fragment roots appear in the order they are defined 
524
	 * by the classpath.
525
	 * </p>
524
	 *
526
	 *
525
	 * @return all of the  package fragment roots contained in this
527
	 * @return all of the  package fragment roots contained in this
526
	 * project, identified on this project's resolved classpath
528
	 * project, identified on this project's resolved classpath
(-)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 (+41 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
6927
					.createJavaProject("P", new String[] {}, "bin");
6928
6929
			String content = new String(
6930
					"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
6931
							+ "<classpath>\n"
6932
							+ "<classpathentry kind=\"src\" path=\"src a\"/>\n"
6933
							+ "<classpathentry kind=\"src\" path=\"src x\"/>\n"
6934
							+ "<classpathentry kind=\"lib\" path=\""
6935
							+ getExternalJCLPath()
6936
							+ "\"/>\n"
6937
							+ "<classpathentry kind=\"output\" path=\"bin\"/>\n"
6938
							+ "</classpath>\n");
6939
6940
			editFile("/P/.classpath", content);
6941
			p.open(null);
6942
			createFolder("/P/src x");
6943
			createFolder("/P/src a");
6944
6945
			IPackageFragmentRoot[] roots = p.getPackageFragmentRoots();
6946
			assertPackageFragmentRootsEqual(roots, 
6947
					"src a (not open) [in P]\n" + 
6948
					"src x (not open) [in P]\n" + 
6949
					""+ getExternalJCLPathString() + " (not open)");
6950
6951
		} finally {
6952
			deleteProject("P");
6953
		}
6954
	}
6955
6915
}
6956
}

Return to bug 338006