2012-10-15 4 views
1

메소드를 인라인해야 할 때 Refactor->Inine을 사용할 수 있습니다. enter image description here enter image description here enter image description hereJDT/LTK를 사용하여 프로그래밍 방식으로 인라인 리팩터링을 실행하는 방법은 무엇입니까?

이 내가 노력 코드 골격

enter image description here

, 나는이 게시물에 코드를 사용 - Is there any eclipse refactoring API that I can call programmatically?. 내가 코드를 실행하면

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

, 나는이 오류 나는 내가 API에 리팩토링 방법 이름을 제공 할 필요가 있다고 생각

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

을 얻었다. 코드에서 무엇이 잘못 되었을까요?

답변

2

위의 코드에서 리팩터링 작업에 메서드를 제공하지 않으면 프로젝트 컨텍스트에만 부여하면됩니다. 그러나 나는 그것을 위해 필요한 API를 모른다.

this source code을 보면 JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT을 사용할 수 있습니다.이 설정은 아마도 설정해야 할 수도 있습니다. 어쩌면 refactoring.ui 플러그인 소스에서 해당 속성에 대한 참조를 검색 할 수 있습니다.

+0

- 당신은 인라인 될 것입니다 메소드의 이름을 알게되면

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

, 당신의 코드를 사용할 수 있습니다. – prosseek