1

タイプ、メソッド、フィールドの名前を変更する 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();
            }
        }
    }
}
4

2 に答える 2

2

ここには、 SearchEngineなどを利用する便利な Eclipse JDT 検索メソッドがいくつかあります。例えば:

/**
      * Find all classes that access methods or fields in this class
      * from within the same project.
      * @param element the Java element the search pattern is based on
      * @param scope the elements being examined, e.g. this class or this package
      * @return the handles of the classes that have methods that
      *  reference methods or fields in this class
      */
     public static Set<String> calculateCallingClasses(IJavaElement element,
                     IJavaSearchScope scope)
                     throws CoreException {
             SearchEngine engine = new SearchEngine();
             SearchParticipant[] participants =
                     new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
             SearchPattern pattern =
                     SearchPattern.createPattern(element, REFERENCES);
             IType enclosingType =
                     (IType)element.getAncestor(IJavaElement.TYPE);
             ClientClassCollector collector = new ClientClassCollector(enclosingType);
             try{
                     engine.search(pattern, participants, scope, collector, null);
             } catch (Exception e) {
                     System.err.println(e.toString() + " for " + element.getElementName());
             } 
            Set<String> clients = collector.getResult();
            return clients;
     }  
于 2012-12-06T13:16:05.763 に答える
2

JDT Core からSearchEngine APIを使用できます

于 2012-12-04T23:42:07.160 に答える