我在 Eclipse 插件中有这段代码。我需要获取任何文件的路径。对于IFile
它的实例,但ICompilationUnit
我不知道。
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
final IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection("org.eclipse.jdt.ui.PackageExplorer");
final Object firstElement = selection.getFirstElement();
String selectedFile = "";
if (firstElement instanceof IFile){
IPath loc = ((IFile) firstElement).getLocation();
if (loc != null){
selectedFile = loc.toOSString();
if(!selectedFile.endsWith(".java")){
selectedFile = "";
}
}
} else {
if(firstElement instanceof ICompilationUnit){
CompilationUnit comUnit = ( CompilationUnit)firstElement;
}
}