【问题标题】:Can Anyone tell me why my IFile always returns null?谁能告诉我为什么我的 IFile 总是返回 null?
【发布时间】:2021-04-16 12:09:52
【问题描述】:

我有一个插件。在这个插件中,我有一个创建一些标记的视图,以便我可以打开文件并自动导航到选定的行。但是,这种方法以前对我有用。它现在停止了吗?它只返回 null 作为我的 IFile。

这是我创建标记的方法 注意:此方法不在 FXML 文件的控制类中。它位于另一个外部文件中。

public static String openAbsoluteFileInEclipseEditor(String absoluteLocationP, int lineNumberP) {

    File absolute = new File(absoluteLocationP);
    if(absolute.isFile() && absolute.exists()) {  
        if(Globals.testing) {
            try {
                Desktop.getDesktop().open(absolute);
                return null;
            } catch (IOException e) {
                ErrorHandling.reportErrors(e);
                return "";
            }
        }else {
            IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            IWorkbenchPage page = window.getActivePage();
            IWorkspace workspace = ResourcesPlugin.getWorkspace(); 

            try {  
                if(lineNumberP != 0) {  

                    IPath location = Path.fromOSString(absolute.getAbsolutePath()); 
                    System.out.println("location " + location);
                    
                    IFile iFile = workspace.getRoot().getFileForLocation(location); 
                    System.out.println("iFile " + iFile);
                    
                    IMarker marker = iFile.createMarker(IMarker.TEXT);
                    marker.setAttribute(IMarker.LINE_NUMBER, lineNumberP);
                    IDE.openEditor(page, marker);
                    marker.delete();
                }else {
                    IFileStore fileStore = EFS.getLocalFileSystem().getStore(absolute.toURI());
                    IDE.openEditorOnFileStore( page, fileStore );
                }
                return null;
            } catch (PartInitException e) {
                ErrorHandling.reportErrors(e);
                return "";
            } catch (CoreException e) {
                ErrorHandling.reportErrors(e);
                return "";
            }
        }
    }else {
        return "File not found";
    }
}

这是您可以在方法中间看到的两个打印值。

位置 C:/SoftwareAG_Workspaces/workspace105/HOSPITAL/Natural-Libraries/HOSPITAL/Programs/XX021P01.NSP iFile 为空

谁能向我指出为什么它可能不再起作用以及为什么它只返回空值?如果可能的话,您能否建议一种可行的替代方法?该文件确实存在于我确定的那个位置。

提前致谢。

【问题讨论】:

  • 这和fx有什么关系?确保文件确实在您期望的位置

标签: java eclipse javafx plugins markers


【解决方案1】:

getFileForLocation 的 Javadoc 说:

当给定的文件系统位置不存在时,此方法返回 null 在工作区中任何现有项目的位置下。

那么该位置是否在当前工作区和有效项目中?

Javadoc 还说:

结果也将忽略明确排除在外的资源 根据现有资源过滤器的工作区。

所以检查所有资源过滤器。

最后,Javadoc 说:

警告:此方法忽略链接资源及其子资源。 由于链接的资源可能与其他资源重叠,因此唯一的映射 不能保证从文件系统位置到单个资源。 查找给定位置的所有资源,包括链接的 资源,使用方法findFilesForLocation

所以检查链接资源

【讨论】:

    猜你喜欢
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2020-02-16
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多