ASTの入手方法を共有してくれたJeffに感謝します。コードを調べたところ、ASTを取得する別の方法がありますが、パブリックAPIも使用しています。私もその方法を投稿したいと思います:
// Assume there is a variable, 'file', of type IFile
ICProject cProject = CoreModel.getDefault().create(file.getProject() );
ITranslationUnit unit = CoreModelUtil.findTranslationUnit(file);
if (unit == null) {
unit = CoreModel.getDefault().createTranslationUnitFrom(cProject, file.getLocation() );
}
IASTTranslationUnit ast = null;
IIndex index = null;
try {
index = CCorePlugin.getIndexManager().getIndex(cProject);
} catch (CoreException e) {
...
}
try {
index.acquireReadLock();
ast = unit.getAST(index, ITranslationUnit.AST_PARSE_INACTIVE_CODE);
if (ast == null) {
throw new IllegalArgumentException(file.getLocation().toPortableString() + ": not a valid C/C++ code file");
}
} catch (InterruptedException e) {
...
} catch (CoreException e) {
...
} finally {
index.releaseReadLock();
}
私のはもう少し複雑です。私は基本的に、物事が100%一貫して機能し始めるまで、物事を変え続けました。実際のリファクタリングについてあなたが言ったことをこれ以上追加する必要はありません。
編集:明確にするために:これは私がこれまでに翻訳ユニットを取得するための最も安全な方法です。