Towards Generating Transformation Rules Without Examples for Android API Replacement
Total Page:16
File Type:pdf, Size:1020Kb
View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by Institutional Knowledge at Singapore Management University Singapore Management University Institutional Knowledge at Singapore Management University Research Collection School Of Information Systems School of Information Systems 4-2019 Towards generating transformation rules without examples for android API replacement Thung Ferdian Singapore Management University, [email protected] Hong Jin KANG Singapore Management University, [email protected] Lingxiao JIANG Singapore Management University, [email protected] David LO Singapore Management University, [email protected] Follow this and additional works at: https://ink.library.smu.edu.sg/sis_research Part of the Software Engineering Commons Citation Ferdian, Thung; KANG, Hong Jin; JIANG, Lingxiao; and LO, David. Towards generating transformation rules without examples for android API replacement. (2019). Proceedings of the 35th IEEE International Conference on Software Maintenance and Evolution (ICSME), Cleveland, OH, USA, 2019 September 30 - October 4. 1-5. Research Collection School Of Information Systems. Available at: https://ink.library.smu.edu.sg/sis_research/4824 This Conference Proceeding Article is brought to you for free and open access by the School of Information Systems at Institutional Knowledge at Singapore Management University. It has been accepted for inclusion in Research Collection School Of Information Systems by an authorized administrator of Institutional Knowledge at Singapore Management University. For more information, please email [email protected]. Towards Generating Transformation Rules without Examples for Android API Replacement Ferdian Thung, Hong Jin Kang, Lingxiao Jiang, and David Lo Singapore Management University Email: fferdianthung, hjkang.2018, lxjiang, [email protected] Abstract—Deprecation of APIs in software libraries is common time when an API deprecation occurs and no code change when library maintainers make changes to a library and will no example is yet available from which to learn code changes longer support certain APIs in the future. When deprecation patterns. Thus, NEAT complements Lamonthe et al.’s approach occurs, developers whose programs depend on the APIs need to replace the usages of the deprecated APIs sooner or later. that requires code change examples. Moreover, different from Often times, software documentation specifies which new APIs Lamonthe et al.’s approach, NEAT produces code transforma- the developers should use in place of a deprecated API. However, tion rules based on semantic patches [3], [6], [14] that can replacing the usages of a deprecated API remains a challenge be easily reused, making it easier to replace deprecated APIs since developers may not know exactly how to use the new APIs. across many usage locations. The developers would first need to understand the API changes before they can replace the deprecated API correctly. Previous NEAT assumes that the API replacement mappings from work has proposed an approach to assist developers in deprecated a deprecated API to its replacement API are available, as Android API replacement by learning from examples. In this such mappings can be generated by previous approaches that work, we also focus on Android APIs and propose an approach perform API replacement mining [2], [10], [16], [17]. The named No Example API Transformation (NEAT) to generate mined mappings specify that a method A should be replaced transformation rules that can assist developers in deprecated API replacement even when code examples are not available (e.g., with a method B, but they do not specify how to actually when the deprecation has happened recently). We have validated perform this replacement. Thus, NEAT also complements those the effectiveness of NEAT on generating transformation rules approaches by providing code transformation rules that specify for deprecated Android APIs. Using NEAT, we can generate 37 how deprecated API replacement can actually be realized. transformation rules for 37 out of a selection of 100 deprecated NEAT uses a combination of API source code analysis, APIs and have validated these rules to be correct. Index Terms—API deprecation, API replacement, program type conversion, and heuristics. The approach distinguishes transformation, Android between two cases, depending on whether or not the affected API is an event handler (i.e., APIs that are called when an event occur). If both APIs are non event handlers, NEAT I. INTRODUCTION analyzes the source code of the API to check whether the Software maintenance is a daily activity for software de- implementation of the deprecated API involves the usage of velopers. One of the tasks that developers need to do while the replacement API. If such an implementation exists, NEAT maintaining a software project is to make sure that the APIs generates a transformation rule based on it. Otherwise, we that they use are up-to-date. However, API updates do not heuristically match the parameters of both APIs, and convert always guarantee backward compatibility and the maintainers deprecated API parameters and/or return values to the types can decide to deprecate older APIs. Thus, when APIs are used by the replacement API (if necessary). On the other hand, deprecated, developers need to replace the deprecated APIs if both APIs are event handlers, NEAT uses similar heuristics with other APIs that can provide equivalent functionality. to generate a transformation rule to implement the replacement Many works [2], [10], [16], [17] that support deprecated method based on the deprecated method’s implementation. To API replacement only focus on identifying APIs that can make the generated transformation rules easily reusable, we replace a deprecated API. There has been very limited work on express them in semantic patches, which is widely adopted and recommending how a deprecated API can actually be replaced has shown to be useful in the context of C. Semantic patches by its replacement APIs. Lamonthe et al. [5] has proposed an can be applied to client applications using Coccinelle [6], [14]. approach to assist deprecated API replacement by learning Specifically, since Android is typically written in Java, we from code change examples that have replaced deprecated use Coccinelle4J, a recent port of Coccinelle targeting Java APIs. Their approach learns API migration patterns from these programming language [3]. examples and applies these patterns to Android applications to We evaluated NEAT on a dataset of 100 deprecated Android replace deprecated API invocations in the applications. APIs with identified replacements as input. We manually In this work, we also deal with the problem of assisting validated whether the generated transformation rules can be developers in replacing usages of deprecated APIs. In contrast used to replace the deprecated APIs correctly. We found to the work of Lamonthe et al., we propose No Example that NEAT can generate correct transformation rules for 37 API Transformation (NEAT) that can provide assistance at the deprecated APIs. Comparing the effectiveness of NEAT to that of automated program repair [4], [7], [12], which also needs match any expression. The remaining lines are the context to transform a piece of code from a bad state to a good one and change operations. In this case, the patch specifies that and correctly repair programs with the effectiveness of 22.69- all calls to setBackgroundDrawable should be replaced 53.33%, our current preliminary result is promising. Note that with a call to setBackground, while keeping the argument different from automated program repair, we do not have any of the method intact. test cases and/or failures to guide our transformations. @@ The main contributions of our work are as follows: expression view, arg; @@ 1) To the best of our knowledge, we are the first to generate - view.setBackgroundDrawable(arg); transformation rules for replacing deprecated APIs when + view.setBackground(arg); there are no available examples to learn from. Fig. 1: An example of a simplified semantic patch replacing a deprecated 2) We propose an approach named No Example API Trans- method from android.view.View class formation (NEAT) to generate transformation rules given a SmPL supports more features for writing semantic patches. deprecated method and its replacement through a combi- Further details about SmPL are available in previous work [1], nation of API source code analysis, type conversion, and [6], [15]. some heuristics. 3) Evaluation on 100 deprecated Android APIs with identified III. APPROACH replacements shows that NEAT generates correct replace- ment rules for 37 deprecated Android APIs. NEAT focuses on one-to-one mapping (i.e., a mapping from one deprecated API to one replacement API), and takes as II. BACKGROUND input a deprecated API signature (i.e., the method name, the A. Coccinelle class name, and the parameters), its replacement API signature, Coccinelle is a program matching and transformation en- and the source code of the library containing the two APIs. gine [6], [11], [14]. It was initially developed to automate API The output of NEAT is a semantic patch written in SmPL, evolutions in the Linux kernel. Coccinelle accepts as input a that specifies the transformation rule to replace a usage of the program to transform, and a semantic patch that describes the deprecated API. transformation to be performed. The semantic patch gener- We observe that a deprecated