# An Automated Refactoring Approach to Improve IoT Software Quality
## 1 Introduction  
The exponential growth of IoT devices is changing our world [1, 2]. According to a recent Gartner report [3], 8.4 billion devices including smart phones, tablets and laptops will be connected by 2020, and this number is expected to grow up to 20.4 billion by 2022. To enable smooth interaction between these IoT devices, IoT software should provide good support. [4, 5].  
Communication between IoT devices is primarily realized in a concurrent way. How to ensure the correctness of concurrent access becomes a major challenge in IoT software development [6, 7]. Java has become one of the most popular programming languages for IoT software development because of its capacity to handle concurrency-related problems [8]. Java virtual machine (JVM) provides support for Java-based IoT applications running almost on any chip. Java provides support for IoT software in different aspects, such as cloud computing, big data, sensors, and M2M computing. Java's ability of combining different devices makes it a good choice for development of IoT applications.  
Writing a high-quality concurrent program is still challenging. Developers usually employ coarse-grained locks which introduce lock contention and decrease performance.  
However, fine-grained locking is notoriously difficult. Usually, it makes programs more complicated, error-prone, and incurs other problems. Sometimes, improper use of fine-grained locking will increase the overhead of lock operations.  
Early works on optimization of concurrent programs focused on the compiler level [9; 10]. However, the compiler optimization is complicated and subject to limitation of many factors. Some researchers proposed methods, such as lock splitting [11] and class splitting [12], to reduce lock contention, and a method of merging synchronized blocks to reduce lock operation overhead [13]. [PERSON] et al. [14] have proposed an algorithm of refactoring for _ReentrantLocks_ and _ReentrantReadWriteLocks_, as well as a refactoring tool Belocker. But Belocker does not support fine-grained refactoring. Though this tool allows automation to some extent, it relies heavily on manual selection of codes to refactor. This paper aims to realize fine-grained lock refactoring via lock splitting and lock degrading of _ReentrantReadWriteLocks_, and implements an automatic tool.  
Our refactoring method for fine-grained _ReentrantReadWriteLocks_ is designed based on static program analysis. Our method identifies all built-in monitors in a program through visitor pattern analysis. The problem of alias with the same monitor object is resolved by alias analysis. The read and write patterns of critical section are analyzed by using effect side analysis. Our refactoring tool works on the source level to help developers implement the automated refactoring from coarse-grained locks to fine-grained _ReentrantReadWriteLocks_. Our tool is tested by five real-world applications, and a total of 1072 built-in monitors have been refactored.  
The main contributions of this paper include the following.  
* We developed algorithms that could convert built-in monitor locks to fine-grained _ReentrantReadWriteLocks_.
* We developed an automated refactoring tool implemented as Eclipse plugins.
* We evaluated our tool on several real-world applications.  
The remainder of this paper is organized as follows. The related works are examined in Section 2. Some advantages of the _ReentrantReadWriteLock_ over built-in monitor locks are presented in Section 3. Section 4 demonstrates our refactoring framework, and our refactoring and analysis algorithm design. Some practical problems are discussed in Section 5. Section 6 presents the evaluation of our proposed tool on a set of Java applications, and conclusions and future works are presented in Section 7.