代码之家  ›  专栏  ›  技术社区  ›  Ravisha

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

  •  4
  • Ravisha  · 技术社区  · 14 年前

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

    1 回复  |  直到 14 年前
        1
  •  6
  •   IAdapter    14 年前

    很难从面包屑中得到这些东西,你必须使用反射来获得它。

    下面是从编辑器中获取当前方法的代码。

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