有没有任何可以以编程方式调用的eclipse重构API? [英] Is there any eclipse refactoring API that I can call programmatically?

查看:116
本文介绍了有没有任何可以以编程方式调用的eclipse重构API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一个广泛的范围内重构代码。我知道从Eclipse IDE里面我可以重构我的类。但是有没有可以在java项目中使用的API,以便我可以通过代码动态地重构项目?






I需要一些想法,如何实现以下:一个程序,调用所有的eclipse重构,重命名和移动一个循环,重构整个项目一次!






我不想通过扩展重构类来引入新的重构类型。我只想以编程方式调用它们。






再次感谢Paul Webster,Leif Frenzer,Mohsen Vakilian和Arno为他们伟大的文章和帮助!

解决方案

下面的答案是伟大的,但我回答了更广泛的观点

<$重新构建状态= new RefactoringStatus();
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IProject [] projects = root.getProjects();

然后:

$ (ICompilationUnit单位:mypackage.getCompilationUnits()){
IType primary = unit.findPrimaryType(); b
$ b

  
IMethod [] methods = primary.getMethods();
int i = 1;
for(IMethod method:methods){
if(method.isConstructor()){
continue;
}
makeChangetoMethods(status,method,changedMethodVersion_+ i);
++ i;
}
}

之后: em>

  IProgressMonitor monitor = new NullProgressMonitor(); 
status = new RefactoringStatus();
重构重构= performMethodsRefactoring(status,methodToRename,newName);

然后:

 更改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);


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?


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.


Thanks again to Paul Webster, Leif Frenzer, Mohsen Vakilian, and Arno for their great articles and help!

解决方案

The answer below is great, but I answered with a wider perspective for the people who need a more bulky and tasty crunch of this wonderful cake:

    RefactoringStatus status = new RefactoringStatus();
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject[] projects = root.getProjects();

then:

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

After that:

IProgressMonitor monitor = new NullProgressMonitor();
status = new RefactoringStatus();
Refactoring refactoring = performMethodsRefactoring(status, methodToRename, newName);

then:

Change change = refactoring.createChange(monitor);
change.perform(monitor);

find below the code for setting the descriptor:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆