Prefactory: Automated Discovery and Application of Library-Adoption Refactorings

📅 2026-07-19
📈 Citations: 0
Influential: 0
📄 PDF
🤖 AI Summary
This work addresses the challenge of systematically identifying refactoring opportunities that replace handwritten code with library APIs when syntactic similarity is absent. The authors propose Prefactory, a novel approach that leverages large language models to generate executable lexical and structural probes. These probes, combined with static analysis, metadata extraction, and differential testing, enable efficient scanning and validation of candidate functions within target projects. To ensure reproducibility and scalability, Prefactory employs heuristic rules to synthesize replacements rather than repeatedly invoking the model. Evaluated on the PrefactoryBench benchmark, the method discovers 75 file-level and 56 function-level refactoring instances, successfully producing 40 test-validated refactorings—substantially outperforming existing baselines.
📝 Abstract
Replacing hand-written code with library API calls is a common refactoring that can reduce code size, make code more idiomatic, and reuse well-tested implementations. Yet many library-adoption opportunities are hard to find automatically: the original code often does not mention the target library and may resemble the library API only in behavior, with little syntactic overlap. Existing tools, such as linters and static modernizers, cover only a small set of manually specified patterns. LLMs and LLM-based agents, on the other hand, can generalize to more patterns, but they are costly, difficult to reproduce and to apply systematically at scale. This paper introduces Prefactory, an automated approach for library-adoption refactoring in Python. The key idea is to use an LLM to synthesize executable search heuristics rather than relying on repeated LLM prompting over a codebase. Given a target project and a target library name, Prefactory collects library metadata and project vocabulary, then generates lexical and structural detectors. Prefactory executes the detectors during a scan phase to find candidate functions. It then heuristically ranks the candidate functions, generates refactorings for the highest-ranked ones using an LLM, and validates the results with project tests and newly generated differential tests. We evaluate Prefactory on PrefactoryBench, a benchmark of 100 real-world library-adoption refactorings from 61 open-source Python projects and 18 libraries. Prefactory detects 75 instances at the file level and 56 at the function level, compared with 35 and 32 for the strongest baseline (Codex CLI). From the 56 detected functions, Prefactory produces 40 test-validated refactorings.
Problem

Research questions and friction points this paper is trying to address.

library-adoption refactoring
code automation
API replacement
software modernization
refactoring detection
Innovation

Methods, ideas, or system contributions that make the work stand out.

library-adoption refactoring
automated code transformation
LLM-guided heuristics
differential testing
code modernization
🔎 Similar Papers
No similar papers found.