I am creating an Eclipse plugin to rename Types, Methods and Fields. Using the following code I can rename the class and the source file but I don’t know how to find the usages of the class in other classes.
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();
}
}
}
}
I have a number of useful Eclipse JDT search methods here, that make use of SearchEngine, et al. For example: