Eclipse插件: Get the enclosing class and member name

标签 eclipse plugins breadcrumbs

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

最佳答案

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

关于Eclipse插件: Get the enclosing class and member name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3953096/

相关文章:

java - 停止运行多个实例的 Eclipse/Java

java - 在 Eclipse 中调试单步时窗口移动

c++ - 使用 GNU C++ 为 Maya 编译时出现编译器错误

node.js - Hapi.js 按顺序加载插件

eclipse - 如何解决 Oracle weblogic 服务器中的 SplitsrcTargetPreparation 异常?

java - Maven & Eclipse Mars编译java 1.8错误

javascript - 使用 enscroll.js 自定义滚动条

Wordpress - 显示没有链接的分类术语 - (get_terms, the_terms, wp_tag_cloud)

grails - 面包屑

php - 抓取 MySQL 表中的单个元素