1

Refactor->Inine当我需要内联方法时可以使用。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

这是我尝试过的代码框架,我使用了这篇文章中的代码 -是否有任何可以以编程方式调用的 Eclipse 重构 API?.

// 1. Get ICompiationUnit for type "smcho.Hello"
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("Hello");
project.open(null /* IProgressMonitor */);

IJavaProject javaProject = JavaCore.create(project);
IType itype = javaProject.findType("smcho.Hello");
org.eclipse.jdt.core.ICompilationUnit icu = itype.getCompilationUnit();

// 2. Contribution and Description creation
RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(IJavaRefactorings.INLINE_METHOD);
InlineMethodDescriptor descriptor = (InlineMethodDescriptor) contribution.createDescriptor();

descriptor.setProject(icu.getResource().getProject().getName( ));

// 3. executing the refactoring
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();
} 

当我执行代码时,我得到了这个错误

org.eclipse.core.runtime.CoreException: The refactoring script argument 'input' is missing 
in the refactoring script.  

我想我需要将重构的方法名称提供给 API。代码中可能有什么问题?

4

2 回答 2

2

在上面的代码中,你从不提供重构操作的方法,你只给它项目上下文。但我不知道必要的 API。

如果您查看此源代码,您会注意到 JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT 的使用,这可能也是您需要设置的。也许您可以搜索 refactoring.ui 插件源以获取对该属性的引用。

于 2012-10-16T06:18:16.973 回答
2

这是与内联重构 JDT API 一起使用的代码。它需要内联起始位置和长度。

int[] selection= {start, length}; // getSelection();
InlineMethodRefactoring refactoring= InlineMethodRefactoring.create(this.icu, new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(this.icu, true), selection[0], selection[1]);
refactoring.setDeleteSource(true);
refactoring.setCurrentMode(Mode.INLINE_ALL); // or INLINE SINGLE based on the user's intervention

IProgressMonitor pm= new NullProgressMonitor();
RefactoringStatus res = refactoring.checkInitialConditions(pm);
res = refactoring.checkFinalConditions(pm);

final PerformRefactoringOperation op= new PerformRefactoringOperation(
refactoring, getCheckingStyle());
op.run(new NullProgressMonitor());

当您知道要内联的方法的名称时,您可以使用中的代码 - Getting startPosition and length of a method invocation using JDT

于 2013-01-17T03:36:20.763 回答