Java 触发';提取方法';在Eclipse重构/LTK API中

Java 触发';提取方法';在Eclipse重构/LTK API中,java,eclipse-plugin,refactoring,eclipse-jdt,ltk,Java,Eclipse Plugin,Refactoring,Eclipse Jdt,Ltk,我目前正在开发一个Eclipse插件,它将允许我使用手势触发重构。 我已经尝试在Eclipse中以编程方式触发“extract method”一段时间了,但我经常遇到问题。 我在搜索解决方案时发现的大多数建议都需要使用内部类 我现在被这个代码模板卡住了。问题是,我找不到任何地方可以将我想要提取的代码作为ISelection或类似的东西 RefactoringContribution rc = RefactoringCore.getRefactoringContribution(IJavaRefa

我目前正在开发一个Eclipse插件,它将允许我使用手势触发重构。 我已经尝试在Eclipse中以编程方式触发“extract method”一段时间了,但我经常遇到问题。 我在搜索解决方案时发现的大多数建议都需要使用内部类

我现在被这个代码模板卡住了。问题是,我找不到任何地方可以将我想要提取的代码作为ISelection或类似的东西

RefactoringContribution rc = RefactoringCore.getRefactoringContribution(IJavaRefactorings.EXTRACT_METHOD);
ExtractMethodDescriptor rd = (ExtractMethodDescriptor) rc.createDescriptor();

rd.setProject(staticHelper.getIProject().getName());
//There should be some more rd.setXXXXX() here.

RefactoringStatus rs = new RefactoringStatus();

try {
    Refactoring r = rd.createRefactoring(rs);
    IProgressMonitor pm = new NullProgressMonitor();

    r.checkInitialConditions(pm);
    r.checkFinalConditions(pm);

    Change change = r.createChange(pm);
    change.perform(pm);
}

catch(Exception e) {e.printStackTrace();}
}
以下方法可行,但它使用内部API:

@SuppressWarnings("restriction")    //Works but is INTERNAL USE ONLY
public static void extractMethodRefactoring() {
    ITextSelection selection = staticHelper.getITextSelection();
    int start = selection.getOffset();
    int length = selection.getLength();


    //The following line is part of the internal API
    ExtractMethodRefactoring tempR = new ExtractMethodRefactoring(staticHelper.getICompilationUnit(), start, length);

    try {
        NullProgressMonitor pm = new NullProgressMonitor();
        tempR.checkAllConditions(pm);
        Change change = tempR.createChange(pm);
        change.perform(pm);
    } catch (Exception e) {e.printStackTrace();}

}

同样,这需要内部类ExtractMethodRefactoring,不应该使用它。

我不太担心使用内部类。在使用JDT时,您迟早需要调用一些内部API。感谢您的输入。除非真的没有其他方法,否则我想阻止使用内部API。什么是内部调用?实际上只有ExtractMethodRefactoring tempR=new ExtractMethodRefactoring(staticHelper.getICompilationUnit(),start,length);