【问题标题】:Is there any Eclipse refactoring API that I can call programmatically?是否有任何我可以以编程方式调用的 Eclipse 重构 API?
【发布时间】:2012-02-26 03:10:22
【问题描述】:

我需要从广义上重构代码。我知道从 Eclipse IDE 内部我可以重构我的类。但是有什么 API 可以在 java 项目中使用,以便我可以通过代码动态重构项目吗?


我需要一些关于如何实现以下目标的想法:一个调用所有 Eclipse 重构以进行重命名和循环移动以一次性重构整个项目的程序!


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

【问题讨论】:

    标签: java eclipse refactoring


    【解决方案1】:

    下面的答案很好,但我以更广阔的视角为那些需要这个美妙的cake 的人提供更庞大和美味的脆饼:

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

    在下面找到设置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);
    

    【讨论】:

      【解决方案2】:

      类似this?

      在基于 Eclipse 的 IDE 中支持编程语言的任何人 迟早会被要求提供自动重构 - 类似于 Java 开发工具 (JDT) 所提供的。自从 Eclipse 3.1 的发布,至少是这个任务的一部分(这是没有 意味着简单)由语言中立的 API 支持:Language 工具包 (LTK)。但是这个 API 是如何使用的呢?

      编辑:

      如果您想在不使用 UI 的情况下以编程方式运行重构,可以使用 RefactoringDescriptors(请参阅article)来填充参数并以编程方式执行重构。如果您创建一个依赖于org.eclipse.core.runtime 的插件并添加org.eclipse.core.runtime.applications 扩展名,您将能够在eclipse 中运行IApplication 类,类似于普通Java 应用程序中的main(String[]) 类。调用 API 的示例可以在 post 上找到。

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

      如果您对使用 JDT API(AST、重构等)有更详细的问题,建议您在 JDT Forum 上提问。

      【讨论】:

      • 我认为它是 eclipse 目录中的一个文件:Core and UI 必须始终创建 org.eclipse.ltk.core.refactoring.Refactoring 的子类。检查 Core 和 UI 中的文本以及“Action!”
      • LTK API 允许使用对象而不是 UI 来配置其重构。虽然它需要是一个 Eclipse 插件,但您可以编写一个无头 IApplication,它可以从命令行填写重构配置并针对您的工作区执行它。
      • eclipse.org/articles/… 是对重构 API 的讨论。 RefactoringDescriptors 可用于在不涉及 GUI 或向导的情况下以编程方式设置更改并在操作中执行它。
      • 如果您创建一个依赖于org.eclipse.core.runtime 的插件并添加org.eclipse.core.runtime.applications 扩展,您将能够在eclipse 中运行IApplication 类,类似于纯java 中的main(String[]) 类应用程序。
      • 我在 irc://freenode.net/#eclipse
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      相关资源
      最近更新 更多