2012-12-01 19 views
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(); 
      } 
     } 
    } 
} 
+0

这个插件和实际的Eclipse方法重新命名方法,属性和类/接口/枚举名称有什么区别? –

+1

没有区别,这只是我正在工作的一个项目。 – Schaliasos

回答

2

我有许多有用的Eclipse JDT搜索方法here,即使用的搜索引擎等的使用SearchEngine API。例如:

/** 
     * 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; 
    } 
+0

非常感谢。这非常有用。 – Schaliasos

+1

不客气。 Eclipse的JDT功能非常强大,但很难找到有关如何使用它们的信息。我很高兴能够提供帮助。 – kc2001

2

您可以从JDT核心

+0

是的,我看到了,它非常有用。我得到的唯一问题是结果是[SearchMatches]类型(http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi% 2Forg%2Feclipse%2Fjdt%2Fcore%2Fsearch%2FSearchMatch.html),我无法找到一种方法来使用这些结果来重命名引用。你能否提供一些我应该如何进行的信息?谢谢。 – Schaliasos