前面的一篇文章本是抱着“分析Eclipse Extract Method的实现,为我所用。”的目的去看的。然后发现的Invoke部分好像是没戏了。看看代码分析的部分是否可以借鉴。
上一次我找到了实现Extract Method这个Function的是一个 ExtractMethodAction类。构造函数就不想多看了。直接跳到他的run函数里面看看。这个Action到底是如何工作的。
if (!ActionUtil.isProcessable(getShell(), fEditor)) return; try{ ExtractMethodRefactoring refactoring= ExtractMethodRefactoring.create(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength()); if (refactoring == null) return; new RefactoringStarter().activate(refactoring, new ExtractMethodWizard(refactoring), getShell(), DIALOG_MESSAGE_TITLE, false); } catch (CoreException e){ ExceptionHandler.handle(e, DIALOG_MESSAGE_TITLE, RefactoringMessages.NewTextRefactoringAction_exception); } |
|
可见整个ExtractMethodAction的核心在于两个类(就目前来看)。
1. ExtractMethodRefactoring
看到他的构造函数包括如下参数:
SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength()
小瞄一下可以发现他的构造产生里包含了ICompilationUnit(整个Java Class),选中的起始位置和长度。有了这些信息以后就可以对代码进行分析了。猜测代码分析的动作在这里完成。打开一看:
private ICompilationUnit fCUnit; private CompilationUnit fRoot; private ImportRewrite fImportRewriter; private int fSelectionStart; private int fSelectionLength; private AST fAST; private ASTRewrite fRewriter; private IDocument fDocument; private ExtractMethodAnalyzer fAnalyzer; private int fVisibility; private String fMethodName; private boolean fThrowRuntimeExceptions; private List fParameterInfos; private Set fUsedNames; private boolean fGenerateJavadoc; |
看到了这些你还敢怀疑吗?就是他了,肯定是他干的了。还有ExtractMethodAnalyzer这个最得力的帮手。好了我们下面就来看看他们几个是怎么合谋完成获得外部变量(可参考Eclipse 插件开发 ——我的计划一文)。