【问题标题】:Eclipse plugin : Get the enclosing class and member nameEclipse 插件:获取封闭类和成员名称
【发布时间】:2010-10-17 11:56:54
【问题描述】:

我创建了一个 Eclipse 插件,以便在按下快捷键时打印出所选对象。 我已经能够做到这一点,但我也想在日志中添加当前方法和当前类名。我不知道如何进一步进行。我试图搜索面包屑 API,但我无法从我的项目中引用该包。我对插件开发很陌生,有人可以指导我如何实现我的目标。提前致谢。

【问题讨论】:

    标签: eclipse plugins breadcrumbs


    【解决方案1】:

    从 Breadcrumb 获取这些东西真的很难,你必须使用反射来获取它。

    这是从编辑器获取当前方法的代码。

    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());
    }

    【讨论】:

    • 您好,非常感谢您的回答。这对我帮助很大。如果您能分享一些链接,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2020-08-24
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    相关资源
    最近更新 更多