Java 如何从ICompilationUnit获取文件路径信息?
我可以使用以下代码获取一组ICompilationUnit,但我需要从ICompilationUnit或IWorkspaceRoot获取物理文件路径 我该怎么做Java 如何从ICompilationUnit获取文件路径信息?,java,eclipse,eclipse-plugin,filepath,Java,Eclipse,Eclipse Plugin,Filepath,我可以使用以下代码获取一组ICompilationUnit,但我需要从ICompilationUnit或IWorkspaceRoot获取物理文件路径 我该怎么做 private static Set<ICompilationUnit> getFiles(String projname) throws CoreException { IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot(); IPro
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;
}
在GetUnderlineResource方法中发送ICompilationUnit。它返回一个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();
}
在GetUnderlineResource方法中发送ICompilationUnit。它返回一个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();
}
getType需要字符串作为参数,getElementType不起作用。在这种情况下,您需要将getType发送到IResource,如上所述,确保资源不为null,或者将引发NullPointException。getType需要字符串作为参数,getElementType不起作用。在这种情况下,您需要将getType发送到IResource,确保资源不为null,或将引发NullPointException。