In software development, it is common for programmers to copy-paste or port code snippets and then adapt them to their use case. This scenario motivates the code adaptation task -- a variant of program repair which aims to adapt variable identifiers in a pasted snippet of code to the surrounding, preexisting source code. However, no existing approach has been shown to effectively address this task. In this paper, we introduce AdaptivePaste, a learning-based approach to source code adaptation, based on transformers and a dedicated dataflow-aware deobfuscation pre-training task to learn meaningful representations of variable usage patterns. We evaluate AdaptivePaste on a dataset of code snippets in Python. Results suggest that our model can learn to adapt source code with 79.8% accuracy. To evaluate how valuable is AdaptivePaste in practice, we perform a user study with 10 Python developers on a hundred real-world copy-paste instances. The results show that AdaptivePaste reduces the dwell time to nearly half the time it takes for manual code adaptation, and helps to avoid bugs. In addition, we utilize the participant feedback to identify potential avenues for improvement of AdaptivePaste.
翻译:在软件开发中,程序员通常会复制粘贴或移植代码片段,然后根据具体使用场景进行适配。这一现象催生了代码适配任务——程序修复的一种变体,旨在将粘贴代码片段中的变量标识符适配到周围的现有源代码中。然而,现有方法尚未能有效解决这一问题。本文提出了AdaptivePaste,一种基于学习的源代码适配方法,该方法基于Transformer架构,并采用专用数据流感知的混淆消除预训练任务,以学习变量使用模式的有意义表示。我们在Python代码片段数据集上评估了AdaptivePaste,结果表明我们的模型能够以79.8%的准确率学习适配源代码。为评估AdaptivePaste的实际价值,我们针对100个真实世界复制粘贴实例,对10名Python开发者进行了用户研究。结果显示,AdaptivePaste将停留时间缩短至手动代码适配所需时间的一半,并有助于避免错误。此外,我们利用参与者反馈识别了AdaptivePaste的潜在改进方向。