gpt4 book ai didi

java - 使用 JDT 删除方法

转载 作者:行者123 更新时间:2023-11-30 04:48:54 27 4
gpt4 key购买 nike

我正在创建一个实用程序来检测代码库中未使用的方法。通过使用下面的代码,我能够成功找到未使用的方法(没有引用)。但我还需要删除这些未使用的方法。如果可以通过 JDT,请告诉我。

// Get all the type declaration of the class.
IType [] typeDeclarationList = unit.getTypes();

for (IType typeDeclaration : typeDeclarationList) {
// Get methods under each type declaration.
IMethod [] methodList = typeDeclaration.getMethods();

for (IMethod method : methodList) {

final List<String> referenceList = new ArrayList<String>();

// loop through the methods and check for each method.
String methodName = method.getElementName();
if (!method.isConstructor()) {

// Finds the references of the method and returns the references of the method.
JDTSearchProvider.searchMethodReference(referenceList, method, scope, iJavaProject);
}
if (referenceList.isEmpty()) {
// delete method
}
}
}

最佳答案

关于 IMethod 的 Javadoc,它有一个来自 ISourceManipulation 的方法 delete()。

参见:

http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/IMethod.html

关于java - 使用 JDT 删除方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10280942/

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