我正在创建一个 Eclipse 插件来重命名类型、方法和字段。使用以下代码我可以重命名类和源文件,但我不知道如何在其他类中找到该类的用法。
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 = null;
try {
selected = unit.getElementAt(selection.getOffset());
} catch (JavaModelException e) {
e.printStackTrace();
}
if(selected.getElementType() == IJavaElement.TYPE) {
IType type = (IType) selected;
InputDialog input = new InputDialog(HandlerUtil.getActiveShell(event), "Rename...",
"Enter the new name for Type: " + selected.getElementName() , selected.getElementName(), null);
if(input.open() == InputDialog.OK)
{
try {
type.rename(input.getValue(), true, null);
} catch (JavaModelException e) {
e.printStackTrace();
}
}
}
}