Community
Participate
Working Groups
I create a compilation unit via some codegeneration in my plugin and save it out. Then from Project Explorer, I delete that java file. Then if in my code I do: ICompilationUnit cu = JavaCore.createCompilationUnitFrom(myDeletedFile); IType currentType = cu.findPrimaryType(); I get back the contents of the deleted file as if they had not been deleted. It seems that the contents are being cached somehow and not flushed when the resource is deleted. I also see similar behavior where the Project Explorer will get out of sync with files that have been created programatically, where the explorer will show a Java file that has been deleted, and when you click on that file it opens up in the editor with a message saying "this file has been deleted".
Are you deleting the files from outside of the Eclipse workspace ?
No, the file is not deleted outside of the workspace. I am always deleting from the Project Explorer. FYI, not sure if it matters, but I am creating the java classes programatically via: ICompilationUnit cu = pack.createCompilationUnit(clName + ".java", myContent, true, monitor);
Sorry Tom I cannot reproduce. I added DeleteTests.testDeleteCompilationUnit5() that hopefully reflects the problem you're describing, and this test passed. public void testDeleteCompilationUnit5() throws CoreException { try { IPackageFragment pkg = getPackage("/P"); pkg.createCompilationUnit( "X.java", "public class X {\n" + "}", true, null ); // force opening getCompilationUnit("P/X.java").open(null); IFile file = getFile("P/X.java"); file.delete(true, null); ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file); IType type = cu.findPrimaryType(); assertNull("Should not find primary type", type); } finally { deleteFile("P/X.java"); } }
Please reopen if you have more details.
I do still see this... I'm wondering if in my case maybe someone is holding onto a model or something like the WorkingCopy document model and not releasing it. Would that cause this behavior? So it's likely not an eclipse issue, but probably another client. Just curious if that could be the culprit.
You're right. If someone made the cu a working copy (using ICompilationUnit#becomeWorkingCopy()) and the working copy was not discarded (using ICompilationUnit#discardWorkingCopy()) before you deleted the file, then the content of the working copy will be used when calling findPrimaryType().
Okay, thanks. That does appear to be what is happening. I will follow up with the offending participant. Appreciate your taking the time to help.
Verified for 3.4 M2 using build I20070917-0010.