首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JDT/LTK以编程方式执行内联重构?

如何使用JDT/LTK以编程方式执行内联重构?
EN

Stack Overflow用户
提问于 2012-10-15 23:18:13
回答 2查看 1.8K关注 0票数 1

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

这是我尝试过的代码框架,我在这篇Is there any eclipse refactoring API that I can call programmatically?文章中使用了代码。

代码语言:javascript
复制
// 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();
} 

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

代码语言:javascript
复制
org.eclipse.core.runtime.CoreException: The refactoring script argument 'input' is missing 
in the refactoring script.  

我认为我需要为API提供重构后的方法名。代码中可能存在什么错误?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-17 11:36:21

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

代码语言:javascript
复制
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中的代码

票数 2
EN

Stack Overflow用户

发布于 2012-10-16 14:18:17

在上面的代码中,您从来没有为重构操作提供方法,您只为它提供了项目上下文。但我不知道实现这一点所需的API。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12898718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档