Bug 544578 - No JRE sources found with Java 8 compliance Java 11
runtime

The fix for bug 525840 introduces a regression for the case of debugging
a Java project that has Java 8 on build path but runs with Java 11 as
runtime: The JRE sources cannot be found when stepping through the code.

This patch tries to find a source attachment for given runtime entry
also for the "modular" JRE case if no direct match was found by
inspecting all opened Java projects.

Change-Id: I72a5aa79c1d5b287c0f6321b88008e382f28895e
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
Also-by: Simeon Andreev <simeon.danailov.andreev@gmail.com>
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaSourceLookupUtil.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaSourceLookupUtil.java
index 0639828..cbbbe50 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaSourceLookupUtil.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaSourceLookupUtil.java
@@ -62,29 +62,7 @@
 					} else {
 						IPackageFragmentRoot root = getPackageFragmentRoot(entry);
 						if (root == null) {
-							String path = entry.getSourceAttachmentLocation();
-							ISourceContainer container = null;
-							if (path == null) {
-								// use the archive itself
-								path = entry.getLocation();
-							}
-							if (path != null) {
-								// check if file or folder
-								File file = new File(path);
-								if (file.isDirectory()) {
-									IResource resource = entry.getResource();
-									if (resource instanceof IContainer) {
-										container = new FolderSourceContainer((IContainer) resource, false);
-									} else {
-										container = new DirectorySourceContainer(file, false);
-									}
-								} else {
-									container = new ExternalArchiveSourceContainer(path, true);
-								}
-								if (!containers.contains(container)) {
-									containers.add(container);
-								}
-							}
+							addSourceAttachment(entry, containers);
 						} else {
 							ISourceContainer container = new PackageFragmentRootSourceContainer(root);
 							if (!containers.contains(container)) {
@@ -117,6 +95,39 @@
 	}
 
 	/**
+	 * Tries to find and attach source containers for given runtime classpath entry
+	 *
+	 * @param entry
+	 *            non null
+	 * @param containers
+	 */
+	private static void addSourceAttachment(IRuntimeClasspathEntry entry, List<ISourceContainer> containers) {
+		String path = entry.getSourceAttachmentLocation();
+		ISourceContainer container = null;
+		if (path == null) {
+			// use the archive itself
+			path = entry.getLocation();
+		}
+		if (path != null) {
+			// check if file or folder
+			File file = new File(path);
+			if (file.isDirectory()) {
+				IResource resource = entry.getResource();
+				if (resource instanceof IContainer) {
+					container = new FolderSourceContainer((IContainer) resource, false);
+				} else {
+					container = new DirectorySourceContainer(file, false);
+				}
+			} else {
+				container = new ExternalArchiveSourceContainer(path, true);
+			}
+			if (!containers.contains(container)) {
+				containers.add(container);
+			}
+		}
+	}
+
+	/**
 	 * Returns whether the source attachments of the given package fragment
 	 * root and runtime classpath entry are equal.
 	 * <p>
@@ -212,6 +223,7 @@
 	private static void getPackageFragmentRootContainers(IRuntimeClasspathEntry entry, List<ISourceContainer> containers) {
 		IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
 		IPath entryPath = entry.getPath();
+		boolean found = false;
 		try {
 			IJavaProject[] jps = model.getJavaProjects();
 			for (int i = 0; i < jps.length; i++) {
@@ -225,6 +237,7 @@
 							PackageFragmentRootSourceContainer container = new PackageFragmentRootSourceContainer(root);
 							if (!containers.contains(container)) {
 								containers.add(container);
+								found = true;
 							}
 						}
 					}
@@ -234,5 +247,9 @@
 		catch (JavaModelException e) {
 			LaunchingPlugin.log(e);
 		}
+
+		if (!found) {
+			addSourceAttachment(entry, containers);
+		}
 	}
 }