4

私は CDT Eclipse プラグインの開発に取り組んでいます。次のコードを使用して CDT コードを使用して Eclipse プロジェクト エクスプローラーに存在するソース ファイルのリストを取得しようとしていますが、結果は null になります。

ケース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());
}

ケース 4:

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

ケース 1、2、3 では出力 null が返されます。ファイルのリストが必要です。ケース 4: 「helloworld/src」ファイルのリストを取得していますが、既存のプロジェクトからメイン ルートを意味するファイルを取得することを期待しています。例:「helloworld」これについて提案してください。

4

1 に答える 1

4

IResourceVisitor を使用して worspace リソース ツリーをウォークスルーするか、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();
        }
    }
}

実際に必要なソース ファイルよりも多くのソース ファイルが存在する可能性があることに注意してください (たとえば、ヘッダーについてシステムを気にしない場合があります)。基になるリソースが何であるかを確認することで、それらをフィルター処理できます。

于 2012-09-14T16:02:36.603 に答える