Eclipse插件:获取封闭类和成员名称

4

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

1个回答

6

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

1
非常感谢您的回答,对我帮助很大。如果您能分享一些相关链接让我深入了解,我将不胜感激。 - Ravisha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接