2012-09-14 41 views
2

我正在開發CDT eclipse插件,我試圖使用以下代碼獲取使用CDT代碼在eclipse項目資源管理器中存在的源文件的列表,空值。使用CDT從現有eclipse項目獲取源文件列表

情況1:

IFile[] files2 = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(new URI("file:/"+workingDirectory)); 
for (IFile file : files2) { 
    System.out.println("fullpath " +file.getFullPath()); 
} 

情況2:

IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(getProject().getRawLocationURI()); 
for (IFile file : files) { 
    System.out.println("fullpath " +file.getFullPath());    
} 

情形3:

IFile[] files3 = ResourceLookup.findFilesByName(getProject().getFullPath(),ResourcesPlugin.getWorkspace().getRoot().getProjects(),false); 
for (IFile file : files3) { 
    System.out.println("fullpath " +file.getFullPath()); 
} 

CASE4:

IFolder srcFolder = project.getFolder("src"); 

案例1,2,3給我輸出空,在我期待的文件列表;案例4中的 :我正在獲取「helloworld/src」文件的列表,但我期望從現有項目的主文件中獲取文件,例如:「helloworld」 請在此建議我。

回答

2

您可以通過worspace資源樹使用IResourceVisitor走路 - 或者你可以通過CDT模型走:

private void findSourceFiles(final IProject project) { 
    final ICProject cproject = CoreModel.getDefault().create(project); 
    if (cproject != null) { 
     try { 
      cproject.accept(new ICElementVisitor() { 

       @Override 
       public boolean visit(final ICElement element) throws CoreException { 
        if (element.getElementType() == ICElement.C_UNIT) { 
         ITranslationUnit unit = (ITranslationUnit) element; 
         if (unit.isSourceUnit()) { 
          System.out.printf("%s, %s, %s\n", element.getElementName(), element.getClass(), element 
            .getUnderlyingResource().getFullPath()); 
         } 
         return false; 
        } else { 
         return true; 
        } 
       } 
      }); 
     } catch (final CoreException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

注意可能會有更多的源文件,那麼你真的想(例如,你可能不關心繫統有關頭文件) - 你可以通過檢查底層資源是否過濾它們。

+0

尤金,感謝您的回覆;嗯,它列出了項目源文件,在單個項目的情況下,但我的情況我有標準的製作項目,在主標準制作項目內我已經創建了一個項目,我想主項目的源文件列表;也使用內部項目的給定代碼列表源文件;但我只想獲取相關的項目源文件;請建議; –

+0

@AnilKumar看看IResourceVisitor(和IResource :: accept) – Eugene

相關問題