Bug 301562

Summary: [JSR269] Error in EclipseFileManager.collectAllMatchingFiles
Product: [Eclipse Project] JDT Reporter: Dmitry Balzer <balzerd>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: Olivier_Thomann
Version: 3.6   
Target Milestone: 3.6 M6   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Proposed fix + regression tests none

Description Dmitry Balzer CLA 2010-02-02 12:22:40 EST
Build Identifier: 20090920-1017

The EclipseFileManager.collectAllMatchingFiles function contains an error, because of which false results are returned:

===
          if (f.isDirectory() && recurse) {
            collectAllMatchingFiles(file, normalizedPackageName + '/'
                                          + f.getName(), kinds, recurse,
                collector);
          } else {
            final Kind kind = getKind(f);
            if (kinds.contains(kind)) {

// In the following code line, 'f' variable must be used instead of currentFile!
// We are iterating over a file now, but adding a parent directory 
// instead of the file to the list

              collector.add(new EclipseFileObject(normalizedPackageName
                                                  + currentFile.getName(), currentFile.toURI(),
                  kind, this.charset));
            }
          }
===

Reproducible: Always

Steps to Reproduce:
Found the bug in code
Comment 1 Olivier Thomann CLA 2010-02-02 12:28:38 EST
Would you have an example where the wrong result is reported? I would add it as a regression test.
Thanks for the report.
Comment 2 Dmitry Balzer CLA 2010-02-02 13:05:52 EST
(In reply to comment #1)
> Would you have an example where the wrong result is reported? I would add it as
> a regression test.
> Thanks for the report.

Hello,

for example, if you have different .java files in some directory, then the following code will print out just the directory name many times instead of file names:

===
public class EclipseFileManagerTest {

	@SuppressWarnings({ "unchecked", "restriction" })
	public static void main(String[] args) throws IOException {
		StandardJavaFileManager fileManager = new org.eclipse.jdt.internal.compiler.tool.EclipseFileManager(null,
				null);

		LinkedList<File> fins = new LinkedList<File>();
		fins.add(new File("C:\\CheckDepBin\\1\\autoCheckOut\\src"));

		JavaFileManager.Location sourceLoc = javax.tools.StandardLocation.SOURCE_PATH;
		fileManager.setLocation(sourceLoc, fins);

	
		HashSet<JavaFileObject.Kind> fileTypes = new HashSet<JavaFileObject.Kind>();
		fileTypes.add(JavaFileObject.Kind.SOURCE);

		Iterable<? extends JavaFileObject> compilationUnits = fileManager.list(
				sourceLoc, "", fileTypes, true);
		
		Iterator it = compilationUnits.iterator();
		while (it.hasNext())
		{
			System.out.println(it.next());
		}
	}
}
===
Comment 3 Olivier Thomann CLA 2010-02-03 15:32:16 EST
Created attachment 158095 [details]
Proposed fix + regression tests
Comment 4 Olivier Thomann CLA 2010-02-03 15:32:42 EST
Thanks for the test case.
Patch is ready.
Comment 5 Olivier Thomann CLA 2010-02-03 15:49:48 EST
Released for 3.6M6.
Regression tests added in both compiler.tools and compiler.apt tests.
Comment 6 Frederic Fusier CLA 2010-03-08 12:11:15 EST
Verified for 3.6M6 using build I20100307-2000.