How to programmatically find a java file in eclipse from full classname?

假装没事ソ 提交于 2019-11-28 05:22:46

问题


Inside an eclipse plugin, I'd like to open a file in editor. I know the full package and class name, how can I determine the path of the java file from this?


回答1:


Take a look at IJavaProject.findType( name ) method. Once you have an IType, you can use getPath() or getResource() methods to locate the file. This method searches across a project and everything visible from that project. To search the whole workspace, iterate through all the Java projects in the workspace, calling the findType() method on each in turn.




回答2:


You also need to know the source folder.

IProject prj = ResourcePlugin.getWorkspace().getRoot().getProject("project-name");
IFile theFile = prj.getFile(sourceFolder + packageName.replace('.','/') + className + ".java");

Generally you specify the file for an editor with an IFile. You can also ask an IFile for variants of the file's path.




回答3:


I know this is a bit old but I had the same need and I had a look at how eclipse does it for stack trace elements (they have a hyperlink on them). The code is in org.eclipse.jdt.internal.debug.ui.console.JavaStackTraceHyperlink (the link is "lazy" so the editor to open is resolved only when you click on it).

What it does is it first searches for the type in the context of the launched application, then for in the whole workspace (method startSourceSearch) :

IType result = OpenTypeAction.findTypeInWorkspace(typeName, false);

And then opens the associated editor (method processSearchResult, source is the type retrieved above) :

protected void processSearchResult(Object source, String typeName, int lineNumber) {
    IDebugModelPresentation presentation = JDIDebugUIPlugin.getDefault().getModelPresentation();
    IEditorInput editorInput = presentation.getEditorInput(source);
    if (editorInput != null) {
        String editorId = presentation.getEditorId(editorInput, source);
        if (editorId != null) {
            try { 
                IEditorPart editorPart = JDIDebugUIPlugin.getActivePage().openEditor(editorInput, editorId);
                if (editorPart instanceof ITextEditor && lineNumber >= 0) {
                    ITextEditor textEditor = (ITextEditor)editorPart;
                    IDocumentProvider provider = textEditor.getDocumentProvider();
                    provider.connect(editorInput);
                    IDocument document = provider.getDocument(editorInput);
                    try {
                        IRegion line = document.getLineInformation(lineNumber);
                        textEditor.selectAndReveal(line.getOffset(), line.getLength());
                    } catch (BadLocationException e) {
                        MessageDialog.openInformation(JDIDebugUIPlugin.getActiveWorkbenchShell(), ConsoleMessages.JavaStackTraceHyperlink_0, NLS.bind("{0}{1}{2}", new String[] {(lineNumber+1)+"", ConsoleMessages.JavaStackTraceHyperlink_1, typeName}));  //$NON-NLS-2$ //$NON-NLS-1$
                    }
                    provider.disconnect(editorInput);
                }
            } catch (CoreException e) {
                JDIDebugUIPlugin.statusDialog(e.getStatus()); 
            }
        }
    }       
}

Code has copyright from eclipse. Hopfully I'm allowed to reproduced it if this is mentionned.



来源:https://stackoverflow.com/questions/11801627/how-to-programmatically-find-a-java-file-in-eclipse-from-full-classname

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!