How to determine which eclipse dependencies to use

血红的双手。 提交于 2019-12-11 15:45:01

问题


In the eclipse PDE, when I copy a snippet from the internet and I'm lacking dependencies, how do I figure out which dependencies I need to import? Say that I have this snippet:

public static IMethod getSelectedMethod() throws JavaModelException {
    IWorkbenchPage page = PlatformUI.getWorkbench()
            .getActiveWorkbenchWindow().getActivePage();
    ITextEditor editor = (ITextEditor) page.getActiveEditor();
    IJavaElement elem = JavaUI.getEditorInputJavaElement(editor
            .getEditorInput());
    if (elem instanceof ICompilationUnit) {
        ITextSelection sel = (ITextSelection) editor.getSelectionProvider()
                .getSelection();
        IJavaElement selected = ((ICompilationUnit) elem).getElementAt(sel
                .getOffset());
        if (selected != null
                && selected.getElementType() == IJavaElement.METHOD) {
            return (IMethod) selected;
        }
    }

    return null;
}

And I get syntax errors on IMethod, JavaModelException, IJavaElement, JavaUI, ICompulationUnit, IJavaElement and IMethod. I happen to know from the top of my head that I need the dependencies org.eclipse.jdt.core and org.eclipse.jdt.ui. But say that I didn't know that I needed these. How could I figure out what the right dependencies are?


回答1:


The simplest way is to just use the 'Open Type' dialog to open the type you are missing and then use the 'Show In > Package Explorer' option in the context menu. This should show you the location of the class in an Eclipse plugin project in the 'External Plug-ins'.

To make this work well you need to have the Eclipse source installed (install the 'Eclipse SDK' in Install New Software from the 'Eclipse Project Updates' site for your release (for example http://download.eclipse.org/eclipse/updates/4.9 for 2018-09). Some Eclipse downloads may already have this installed.

To make 'Open Type' include the Eclipse plugins select the 'Include all plug-ins from target in Java search' option in the 'Plug-in Development' page of the Preferences.



来源:https://stackoverflow.com/questions/52736233/how-to-determine-which-eclipse-dependencies-to-use

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