[英]JDT - How to get ICompilationUnit from CompilationUnit
[英]How to get file path information from ICompilationUnit?
我可以使用以下代碼獲取一組ICompilationUnit,但我需要從ICompilationUnit或IWorkspaceRoot獲取物理文件路徑。
我怎樣才能做到這一點?
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 getUnderlyingResource()方法。 它返回一個IResource,它可以告訴你它是否是一個文件,如果是,它的文件名和路徑的各種形式是什么。
請注意,也可以返回null,因此請注意。
這樣的事情:
// resource is an IResource returned by sending
// an iCompilationUnit the getUnderlyingResource() method
if (resource.getType() == IResource.FILE) {
IFile ifile = (IFile) resource;
String path = ifile.getRawLocation().toString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.