Eclipse plugin : Get the enclosing class and member name

前端 未结 1 1875
后悔当初
后悔当初 2021-02-10 17:28

I have created a Eclipse plug in to printout the object in selection on press of a short cut key. I have been able to do this ,but i also would like to add the current method a

1条回答
  •  礼貌的吻别
    2021-02-10 18:06

    It really hard to get that stuff from Breadcrumb, you would have to use reflection to get it.

    Here is the code to get current method from editor.

    ITextEditor editor = (ITextEditor) PlatformUI.getWorkbench()
            .getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    
    ITextSelection selection = (ITextSelection) editor
            .getSelectionProvider().getSelection();
    
    IEditorInput editorInput = editor.getEditorInput();
    IJavaElement elem = JavaUI.getEditorInputJavaElement(editorInput);
    if (elem instanceof ICompilationUnit) {
        ICompilationUnit unit = (ICompilationUnit) elem;
        IJavaElement selected = unit.getElementAt(selection.getOffset());
    
        System.out.println("selected=" + selected);
        System.out.println("selected.class=" + selected.getClass());
    }

    0 讨论(0)
提交回复
热议问题