Abstract
Software development frequently involves reusing code snippets from external sources or internal repositories. However, integrating these snippets often requires manual "code adaptation"—the tedious and error-prone process of renaming variables to match the local context. We present AdaptivePaste, a transformer-based model specifically designed for this task. By leveraging dataflow-aware deobfuscation and a novel parallel-decoder architecture, AdaptivePaste learns to map anonymized variable usage patterns to existing identifiers in the target environment. Our evaluation on 4.3 million Python files and a user study with professional developers demonstrates that AdaptivePaste achieves 67.8% exact match accuracy and reduces manual adaptation effort by nearly half.
Introduction
Knowledge reuse is a cornerstone of modern software engineering, yet code is rarely "plug-and-play." When a developer copies a logic block, the variable names in the source snippet (e.g., xpos1, ypos1) frequently conflict with those in the destination project (e.g., start_x, start_y). This mismatch necessitates manual renaming, a process known as code adaptation. While traditional "variable misuse" models can identify isolated errors, they struggle with pasted blocks where the entire variable set may be contextually "incorrect."
AdaptivePaste addresses this by treating adaptation as a translation task: converting "anonymized" variables within a snippet into the correct identifiers defined in the surrounding code.
Methodology
The core of AdaptivePaste is a transformer-based architecture trained on massive-scale GitHub data. The researchers introduced two primary variants:
- AP-uni (Uni-decoder): A standard encoder-decoder model that predicts the sequence of adapted variable names.
- AP-parallel (Parallel-decoder): A novel architecture utilizing multiple weight-tied decoders to predict each variable independently. This design allows the model to generate individual confidence scores, enabling "selective adaptation" where only high-confidence suggestions are presented to the user.
Key Innovations
- Dataflow-aware Deobfuscation: To force the model to learn semantic relationships rather than simple token patterns, AdaptivePaste anonymizes all occurrences of a variable (e.g.,
temp_varbecomes___v1). This emphasizes the usage patterns of variables. - Context Prioritization (eWASH): To handle large source files, the model uses eWASH to summarize the environment, prioritizing relevant method signatures, class fields, and imports.
Evaluation and Results
The model was trained on a dataset of 29 million "pastable" blocks extracted from 4.3 million Python files.
Quantitative Performance
AdaptivePaste significantly outperforms general-purpose code models. AP-uni achieved an exact match accuracy of 67.8%, compared to 39.5% for CodeT5. When using the parallel decoder with a confidence threshold, the model reached a precision of 85.9%. Furthermore, 83.9% of the model's suggestions resulted in semantically valid code (ValidPaste score).
User Study
A study involving 20 professional developers revealed that AdaptivePaste is particularly effective in "hard" cases involving unfamiliar APIs (e.g., PyTorch). By automating the mapping process, the tool reduced "dwell time"—the time spent manually fixing code—by approximately 50%.
Conclusion
AdaptivePaste demonstrates that learning semantics-aware variable representations can significantly alleviate the burden of code adaptation. By providing high-precision suggestions and reducing manual effort, it serves as a powerful utility for enhancing developer productivity. Future work will focus on expanding language support beyond Python and addressing structural code changes beyond simple variable renaming.
Summary updated