private static Set< ICompilationUnit> getFiles(String projname)
        throws CoreException {
    IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot();
    IProject proj = ws.getProject(projname);
    IJavaProject javaProject = JavaCore.create(proj);
    Set< ICompilationUnit> files = new HashSet< ICompilationUnit>();
    javaProject.open(new NullProgressMonitor());
    for (IPackageFragment packFrag : javaProject.getPackageFragments()) {
        for (ICompilationUnit icu : packFrag.getCompilationUnits()) {
            files.add(icu);
        }
    }
    javaProject.close();
    return files;
}
ICompilationUnit file = …;
IResource resource = null;
try {
	resource = file.getUnderlyingResource();
} catch (JavaModelException e) {
    e.printStackTrace();
}
if (resource.getType() == IResource.FILE) {
    IFile ifile = (IFile) resource;
    String path = ifile.getRawLocation().toString();
}
What it does is get
IWorkspaceRoot, and from it, you can get IJavaProject. Then, open with NullProgressMonitor(). For all of the PackageFragment, we can find ICompilationUnit, and add it to the files HashSet. 
The issue is how to get the physical path from the compilation unit?
private String getWorkbenchDirectory() throws FileNotFoundException
{
	IPath path = Platform.getLocation();
	String filePath = path.toOSString();
	return filePath;
}
 
No comments:
Post a Comment