Software refactoring is essential for maintaining code quality. However, API replacement refactoring, which replaces custom logic with API calls, remains underexplored. Existing refactoring tools provide limited support for detecting such opportunities because they rely on predefined templates and have difficulty capturing complex, multi-statement semantic equivalents. To address this limitation, we conduct the first empirical study of API replacement refactorings by mining 166,299 commits across six open-source Java projects and manually analyzing a curated subset of 1,800 commits, from which we identify 366 validated instances to characterize their scope, categories, and recurring patterns. Based on these insights, we propose AKIRA (Adaptive Knowledge Discovery and Retrieval), a hybrid framework that integrates pattern-deterministic heuristics with a refactoring-aware knowledge base to assess the practical feasibility of recommending API replacement refactorings. Our evaluation shows that AKIRA achieves 90% recall and 88% precision on a manually curated dataset. Furthermore, on the external RETIWA dataset, AKIRA significantly improves the state of the art by increasing recall from 21% to 81% and precision from 40% to 78%. These results demonstrate the effectiveness of combining static pattern matching with semantic reasoning to support the automation of recommending complex API replacement refactorings.
翻译:软件重构对维护代码质量至关重要。然而,将定制逻辑替换为API调用的API替换重构技术仍未得到充分探索。现有重构工具对检测此类机会的支持有限,因为它们依赖预定义模板,难以捕捉复杂的多语句语义等价关系。为解决这一局限,我们通过挖掘六个开源Java项目的166,299次提交并人工分析其中精心筛选的1,800次提交,开展了首个针对API替换重构的实证研究,从中识别出366个经过验证的实例,用以刻画其范围、类别和重复模式。基于这些洞察,我们提出AKIRA(自适应知识发现与检索)框架——一个融合模式确定性启发式方法与重构感知知识库的混合框架,用于评估推荐API替换重构的实际可行性。实验表明,在人工标注数据集上,AKIRA实现了90%的召回率和88%的精确率。此外,在外部RETIWA数据集上,AKIRA将召回率从21%提升至81%、精确率从40%提升至78%,显著改进了现有技术水平。这些结果证明了将静态模式匹配与语义推理相结合,以支持复杂API替换重构自动化推荐的可行性。