# Automatic Refactoring Approach for Asynchronous Mechanisms with CompletableFuture
## 1 Introduction
Considering that manual refactoring is labor-intensive, time-consuming, and prone to errors, the application of automated refactoring tools becomes particularly important. Through automation, we can more efficiently promote the migration of existing code to CompletableFuture, thereby fully leveraging its advanced asynchronous programming features. In recent years, numerous studies have been conducted in the field of automated refactoring related to asynchronous constructs. Previous research has concentrated on (1) refactoring current constructs into more suitable alternatives [4; 5; 6; 7; 8] and (2) embedding asynchronous constructs within sequential code to enhance execution efficiency [9; 10; 11; 12; 13; 14; 15]. Our method expands the first area by diversifying asynchronous refactoring techniques, introducing a novel approach to transform traditional asynchronous constructs into CompletableFuture. Moreover, our strategy to identify non-JUC internal asynchronous constructs offers significant insights. To address the aforementioned issues, this paper proposes an automated refactoring method for the CompletableFuture asynchronous mechanism. By employing program analysis techniques such as visitor pattern analysis, executor inheritance structure analysis, and points-to analysis, we identify refactorable executor types within software projects poised for refactoring. Based on these analyses, we apply refactoring templates to automatically transform four types of traditional asynchronous constructs into CompletableFuture constructs. Utilizing the Eclipse JDT framework [16] and Soot [17], an automated refactoring tool, ReFuture, has been implemented in the form of an Eclipse plugin. In experiments, ReFuture was evaluated based on the number of refactorings, the correctness of refactorings, and the refactoring time. The results demonstrate that among 813 instances of traditional asynchronous constructs in nine large-scale open-source projects, ReFuture successfully refactored 639 instances without introducing errors into the refactored programs. This tool effectively facilitates the automatic refactoring from traditional asynchronous constructs to CompletableFuture. The main contributions of this paper are as follows. * Empirical research: We investigate the use of common asynchronous constructs in current open-source Java projects and analyze the complexity of asynchronous tasks in projects using traditional asynchronous constructs and CompletableFuture constructs. * Refactoring tool: We present an automated refactoring method from traditional asynchronous constructs to CompletableFuture constructs, specifically addressing executor types that exist outside of the JUC. We propose analysis methods from the perspectives of inheritance and composition, which are common in object-oriented program reuse, thereby enhancing the applicability of our refactoring method. Based on the Eclipse platform [18], we implement the ReFuture automatic refactoring tool, which has been made open-source [19]. * Experimental evaluation: We assess the applicability, correctness, and efficiency and conduct a side-by-side comparison with the existing method, all through answering several research questions. The rest of the paper is organized as follows. Section 2 discusses related work. Section 3 introduces the motivation of this study through an example. Section 4 presents the empirical research questions and results. Section 5 details the automated refactoring method proposed in this paper. Section 6 describes the experimental setup and evaluation results. The paper concludes with a summary of the entire work.