How to determine which eclipse dependencies to use

2019-08-20 02:47发布

问题:

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.