gpt4 book ai didi

java - 查找类用法

转载 作者:行者123 更新时间:2023-11-29 03:39:52 24 4
gpt4 key购买 nike

我正在创建一个 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();
}
}
}
}

最佳答案

我有一些有用的Eclipse JDT搜索方法here ,利用 SearchEngine , 等人例如:

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

关于java - 查找类用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13659435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com