是否有任何可以以编程方式调用的 Eclipse 重构 API? [英] Is there any Eclipse refactoring API that I can call programmatically?
问题描述
我需要从广义上重构代码.我知道从 Eclipse IDE 内部我可以重构我的类.但是有没有可以在java项目中使用的API,以便我可以通过代码动态重构项目?
I need to refactor code in a wide term. I know that from inside the Eclipse IDE I can refactor my classes. But is there any API that I can use in a java project so that I can refactor projects dynamically through code?
我需要一些关于如何实现以下目标的想法:一个调用所有 Eclipse 重构的程序,以便在循环中重命名和移动以一次性重构整个项目!
I need some idea on how to achieve the following: a program that calls all the Eclipse refactorings for renaming and moving in a loop to refactoring the entire project in one shot!
我不想通过扩展重构类来引入新的重构类型.我只想以编程方式调用它们.
I don't want to introduce new refactoring types by extending the refactoring classes. I just want to call them programmatically.
推荐答案
下面的答案很好,但我以更广阔的视角回答了那些需要更庞大、更美味的脆饼的人们蛋糕
:
RefactoringStatus status = new RefactoringStatus();
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject[] projects = root.getProjects();
然后:
for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
IType primary = unit.findPrimaryType();
IMethod[] methods = primary.getMethods();
int i = 1;
for (IMethod method : methods) {
if (method.isConstructor()) {
continue;
}
makeChangetoMethods(status, method,"changedMethodVersion_" + i);
++i;
}
}
之后:
IProgressMonitor monitor = new NullProgressMonitor();
status = new RefactoringStatus();
Refactoring refactoring = performMethodsRefactoring(status, methodToRename, newName);
然后:
Change change = refactoring.createChange(monitor);
change.perform(monitor);
在下面找到设置描述符
的代码:
String id = IJavaRefactorings.RENAME_METHOD;
RefactoringContribution contrib = RefactoringCore.getRefactoringContribution(id);
RenameJavaElementDescriptor desc = contrib.createDescriptor();
desc.setUpdateReferences(true);
desc.setJavaElement(methodToRename);
desc.setNewName(newName);
desc.createRefactoring(status);
这篇关于是否有任何可以以编程方式调用的 Eclipse 重构 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!