Bug 64011 - JavaProjectSourceContainer does not search for non-Java files
diff --git a/org.eclipse.jdt.debug.tests/testprograms/org/eclipse/debug/non-java.txt b/org.eclipse.jdt.debug.tests/testprograms/org/eclipse/debug/non-java.txt
new file mode 100644
index 0000000..eaa3571
--- /dev/null
+++ b/org.eclipse.jdt.debug.tests/testprograms/org/eclipse/debug/non-java.txt
@@ -0,0 +1 @@
+A non java file to test source lookup
\ No newline at end of file
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AutomatedSuite.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AutomatedSuite.java
index 6f33b2f..011cafb 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AutomatedSuite.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AutomatedSuite.java
@@ -38,6 +38,7 @@
 import org.eclipse.jdt.debug.tests.core.InstanceFilterTests;
 import org.eclipse.jdt.debug.tests.core.InstanceVariableTests;
 import org.eclipse.jdt.debug.tests.core.JavaBreakpointListenerTests;
+import org.eclipse.jdt.debug.tests.core.JavaProjectSourceContainerTests;
 import org.eclipse.jdt.debug.tests.core.LaunchConfigurationArgumentTests;
 import org.eclipse.jdt.debug.tests.core.LaunchConfigurationTests;
 import org.eclipse.jdt.debug.tests.core.LaunchDelegateTests;
@@ -141,6 +142,7 @@
 		addTest(new TestSuite(DirectorySourceLookupTests.class));
 		addTest(new TestSuite(ExternalArchiveSourceContainerTests.class));
 		addTest(new TestSuite(ArchiveSourceLookupTests.class));
+		addTest(new TestSuite(JavaProjectSourceContainerTests.class));
 		addTest(new TestSuite(MiscBreakpointsTests.class));
 		// removed for M5 - see bug 46991
 		//addTest(new TestSuite(WorkingDirectoryTests.class));
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/JavaProjectSourceContainerTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/JavaProjectSourceContainerTests.java
new file mode 100644
index 0000000..dc4532b
--- /dev/null
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/JavaProjectSourceContainerTests.java
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * Copyright (c) 2004 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jdt.debug.tests.core;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.debug.core.sourcelookup.ISourceContainer;
+import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.debug.tests.AbstractDebugTest;
+import org.eclipse.jdt.internal.launching.JavaSourceLookupDirector;
+import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;
+
+/**
+ * Tests Java project source containers
+ */
+public class JavaProjectSourceContainerTests extends AbstractDebugTest {
+	
+	public JavaProjectSourceContainerTests(String name) {
+		super(name);
+	}
+	
+	/**
+	 * Returns a Java project source container
+	 * 
+	 * @return
+	 * @throws Exception
+	 */
+	protected JavaProjectSourceContainer getContainer(IJavaProject project, boolean duplicates) throws Exception {
+		ISourceLookupDirector director = new JavaSourceLookupDirector();
+		director.initializeParticipants();
+		director.setFindDuplicates(duplicates);
+		JavaProjectSourceContainer container = new JavaProjectSourceContainer(project);
+		director.setSourceContainers(new ISourceContainer[]{container});
+		return container;
+	}
+	
+	/**
+	 * Tests creation and restoring from a memento.
+	 * 
+	 * @throws Exception
+	 */
+	public void testSourceContainerMemento() throws Exception {
+		ISourceContainer container = getContainer(getJavaProject(), false);
+		String memento = container.getType().getMemento(container);
+		ISourceContainer restore = container.getType().createSourceContainer(memento);
+		assertEquals("Directory source container memento failed", container, restore);
+	}	
+
+	public void testDefaultPackageLookup() throws Exception {
+		ISourceContainer container = getContainer(getJavaProject(), false);
+		Object[] objects = container.findSourceElements("Breakpoints.java");
+		assertEquals("Expected 1 result", 1, objects.length);
+		IFile file = (IFile) objects[0];
+		assertEquals("Wrong file", "Breakpoints.java", file.getName());
+	}
+	
+	public void testQualifiedLookup() throws Exception {
+		ISourceContainer container = getContainer(getJavaProject(), false);
+		Object[] objects = container.findSourceElements("org/eclipse/debug/tests/targets/CallLoop.java");
+		assertEquals("Expected 1 result", 1, objects.length);
+		IFile file = (IFile) objects[0];
+		assertEquals("Wrong file", "CallLoop.java", file.getName());		
+	}
+	
+	public void testNonJavaLookup() throws Exception {
+		ISourceContainer container = getContainer(getJavaProject(), false);
+		Object[] objects = container.findSourceElements("debug/non-java.txt");
+		assertEquals("Expected 1 result", 1, objects.length);
+		IFile file = (IFile) objects[0];
+		assertEquals("Wrong file", "non-java.txt", file.getName());		
+	}	
+}
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/containers/JavaProjectSourceContainer.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/containers/JavaProjectSourceContainer.java
index 60ea31f..2fb8651 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/containers/JavaProjectSourceContainer.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/sourcelookup/containers/JavaProjectSourceContainer.java
@@ -22,6 +22,7 @@
 import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
 import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer;
 import org.eclipse.debug.core.sourcelookup.containers.FolderSourceContainer;
+import org.eclipse.debug.core.sourcelookup.containers.ProjectSourceContainer;
 import org.eclipse.jdt.core.IClasspathEntry;
 import org.eclipse.jdt.core.IJavaProject;
 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
@@ -40,6 +41,11 @@
 		
 	// Java project
 	private IJavaProject fProject;
+	// Source folders
+	private ISourceContainer[] fSourceFolders;
+	// Generic project container
+	private ISourceContainer[] fOthers;
+	
 	/**
 	 * Unique identifier for Java project source container type
 	 * (value <code>org.eclipse.jdt.launching.sourceContainer.javaProject</code>).
@@ -97,6 +103,11 @@
 				}
 			}
 		}
+		// cache the Java source folders to search for ".java" files in
+		fSourceFolders = (ISourceContainer[]) containers.toArray(new ISourceContainer[containers.size()]);
+		ISourceContainer theProject = new ProjectSourceContainer(fProject.getProject(), false);
+		fOthers = new ISourceContainer[] {theProject};
+		containers.add(theProject);
 		return (ISourceContainer[]) containers.toArray(new ISourceContainer[containers.size()]);
 	}
 	/* (non-Javadoc)
@@ -118,25 +129,39 @@
 	 * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
 	 */
 	public Object[] findSourceElements(String name) throws CoreException {
-		Object[] objects = super.findSourceElements(name);
-		List filtered = null;
-		for (int i = 0; i < objects.length; i++) {
-			Object object = objects[i];
-			if (object instanceof IResource) {
-				if (!getJavaProject().isOnClasspath((IResource)object)) {
-					if (filtered == null) {
-						filtered = new ArrayList(objects.length);
-						for (int j = 0; j < objects.length; j++) {
-							filtered.add(objects[j]);
+		// force container initialzation
+		getSourceContainers();
+		
+		if (name.endsWith(".java")) { //$NON-NLS-1$
+			// only look in source folders
+			Object[] objects = findSourceElements(name, fSourceFolders);
+			List filtered = null;
+			for (int i = 0; i < objects.length; i++) {
+				Object object = objects[i];
+				if (object instanceof IResource) {
+					if (!getJavaProject().isOnClasspath((IResource)object)) {
+						if (filtered == null) {
+							filtered = new ArrayList(objects.length);
+							for (int j = 0; j < objects.length; j++) {
+								filtered.add(objects[j]);
+							}
 						}
+						filtered.remove(object);
 					}
-					filtered.remove(object);
 				}
 			}
+			if (filtered == null) {
+				return objects;
+			}
+			return filtered.toArray();			
+		} else {
+			// look elsewhere if non a ".java" file
+			return findSourceElements(name, fOthers);
 		}
-		if (filtered == null) {
-			return objects;
-		}
-		return filtered.toArray();
+	}
+	public void dispose() {
+		fSourceFolders = null;
+		fOthers = null;
+		super.dispose();
 	}
 }