I am creating a utility to detect unused methods in a code base. By using the below code i am able to succesfully find the unused methods (having no references) . But i also need to delete such unused methods. Please let me know if it is possible through 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
}
}
}
Regarding the Javadoc of IMethod it has a method delete() from ISourceManipulation.
see:
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/IMethod.html