我创建了一个 Eclipse 插件,可以在按下快捷键时打印出选择中的对象。我已经能够做到这一点,但我也想在日志中添加当前方法和当前类名。我不知道如何进一步进行。我试图搜索面包屑 API,但我无法从我的项目中引用该包。我对插件开发很陌生,有人可以指导我如何实现我的目标。提前致谢。
问问题
1062 次
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());
}
于 2010-10-18T14:45:35.033 回答