简体   繁体   English

是否有可以通过编程方式调用的Eclipse重构API?

[英]Is there any Eclipse refactoring API that I can call programmatically?

I need to refactor code in a wide term. 我需要在很长一段时间内重构代码。 I know that from inside the Eclipse IDE I can refactor my classes. 我知道从Eclipse IDE里面我可以重构我的类。 But is there any API that I can use in a java project so that I can refactor projects dynamically through code? 但是我可以在java项目中使用任何API,以便我可以通过代码动态地重构项目吗?


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! 我需要了解如何实现以下内容:一个程序调用所有Eclipse重构进行重命名并在循环中移动以一次重构整个项目!


I don't want to introduce new refactoring types by extending the refactoring classes. 我不想通过扩展重构类来引入新的重构类型。 I just want to call them programmatically. 我只是想以编程方式调用它们。

Something like this ? 这样的东西?

Anyone who supports a programming language in an Eclipse-based IDE will be asked sooner or later to offer automated refactorings - similar to what is provided by the Java Development Tools (JDT). 任何在基于Eclipse的IDE中支持编程语言的人迟早都会被要求提供自动重构 - 类似于Java开发工具(JDT)提供的内容。 Since the release of Eclipse 3.1, at least part of this task (which is by no means simple) is supported by a language neutral API: the Language Toolkit (LTK). 自Eclipse 3.1发布以来,语言中性API(语言工具包(LTK))至少支持此任务的一部分(这绝不是简单的)。 But how is this API used? 但是这个API是如何使用的呢?

EDIT: 编辑:

If you want to programmatically run refactorings without using the UI, RefactoringDescriptors (see article ) can be used to fill in the parameters and execute the refactoring programmatically. 如果要在不使用UI的情况下以编程方式运行重构,可以使用RefactoringDescriptors(请参阅文章 )填写参数并以编程方式执行重构。 If you create a plugin that depends on org.eclipse.core.runtime and add the org.eclipse.core.runtime.applications extension, you will be able to run an IApplication class from eclipse similar to a main(String[]) class in plain java apps. 如果您创建一个依赖于org.eclipse.core.runtime的插件并添加org.eclipse.core.runtime.applications扩展,您将能够从eclipse运行一个类似于main(String[])类的IApplication类在纯Java应用程序中。 An example of calling the API can be found on the post . 可以在帖子上找到调用API的示例。

ICompilationUnit cu = ... // an ICompilationUnit to rename

RefactoringContribution contribution =
    RefactoringCore.getRefactoringContribution(IJavaRefactorings .RENAME_COMPILATION_UNIT);
RenameJavaElementDescriptor descriptor =
    (RenameJavaElementDescriptor) contribution.createDescriptor();
descriptor.setProject(cu.getResource().getProject().getName( ));
descriptor.setNewName("NewClass"); // new name for a Class
descriptor.setJavaElement(cu);

RefactoringStatus status = new RefactoringStatus();
try {
    Refactoring refactoring = descriptor.createRefactoring(status);

    IProgressMonitor monitor = new NullProgressMonitor();
    refactoring.checkInitialConditions(monitor);
    refactoring.checkFinalConditions(monitor);
    Change change = refactoring.createChange(monitor);
    change.perform(monitor);

} catch (CoreException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

If you have more detailed questions about using the JDT APIs (AST, Refactoring, etc) I'd suggest you ask on the JDT Forum . 如果您有关于使用JDT API(AST,Refactoring等)的更详细的问题,我建议您在JDT论坛上提问。

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 : 下面的答案很棒,但我对那些需要更美味和美味的美味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 : 在下面找到设置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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM