CODE REFACTORING UNDER CONSTRAINTS by YAN LIANG
Total Page:16
File Type:pdf, Size:1020Kb
CODE REFACTORING UNDER CONSTRAINTS by YAN LIANG NICHOLAS A. KRAFT, CO-CHAIR RANDY K. SMITH, CO-CHAIR JEFFREY C. CARVER ALLEN S. PARRISH UZMA RAJA A DISSERTATION Submitted in partial fulfillment of the requirements for the degree of Doctor of Philosophy in the Department of Computer Science in the Graduate School of The University of Alabama TUSCALOOSA, ALABAMA 2011 Copyright Yan Liang 2011 ALL RIGHTS RESERVED ABSTRACT Code refactoring is the process of changing the internal structure of the program without changing its external behaviors. Most refactoring tools ensure behavior preservation by enforcing preconditions that must hold for the refactoring to be valid. However, their approaches have three drawbacks that make the refactoring results far from satisfactory and reduce the utilization of refactoring tools in practice. Firstly, programmers are not sure how code will be changed by those tools due to the invisible refactoring rules hidden behind the interfaces of tools. Secondly, current refactoring tools have limited extensibility to accommodate new refactorings. Lastly, most refactoring tools lack mechanisms to allow programmer to specify their own preconditions to indicate as to which properties of a program are of interest. We consider refactoring a code change activity that, as with other constraints imposed on code during software development and maintenance such as naming rules, should be visible, easily extensible, and adaptable. It should also combine the developers’ opinions, implementation styles of existing code and other good coding practice. We propose a model- based approach to precondition specification and checking in which preconditions can be declared explicitly and dynamically against the designated program metamodel, and verified against concrete program models. This dissertation applies the approach of model-based refactoring precondition specification and checking on C++ source code refactoring. Based on the analysis of primitive refactorings, we design a C++ language metamodel to support constraint specification and code inspection for refactoring purposes. We then specify preconditions of 18 primitive refactorings against the ii metamodel, with primary concerns on syntax error prevention and semantic preservation. The impact of a programmer’s perspective on these specifications is discussed. As another example to demonstrate the importance and necessities of supporting visible, extensible and adaptable precondition specification and checking, we use template method and singleton patterns to discuss how design patterns can affect refactoring decisions. We set up an experimental environment in which we build the language metamodel, develop a program model extraction tool and simulate the process of precondition specification and verification following the proposed approach. iii LIST OF ABBREVIATIONS AND SYMBOLS AST Abstract Syntax Tree ASG Abstract Semantic Graph CBTD Component-Based Tool Development CDT C++ Development Tools: C++ IDE based on Eclipse platform DMM Dagstuhl Middle Metamodel EMF Eclipse Modeling Framework ERM Entity-Relationship Model MDT Eclipse Model Development Tools MDTD Model-Driven Tool Development OCL Object Constraint Language RE Reverse Engineering iv ACKNOWLEDGMENTS First and foremost, I would like to express thanks to my esteemed advisor Dr. Smith. It has been an honor to be in his PhD program and to participate in my favorite research area: software engineering. This dissertation would not have been possible without his professional guidance. The passion he has shown for cutting edge Software technologies has provided me with an excellent example of how to be a professional software researcher, which will benefit my career in the future. My special thanks go to Dr. Kraft. His expertise in reverse engineering and code refactoring has added to my understanding of research in this area. I would also like to express my gratitude to my committee members, Dr. Uzma Raja, Dr. Jeffery C. Carver and Dr. Allen S. Parrish for their guidance and advice on my dissertation and research. I am also so grateful to my father Wenjie Liang and mother Qiaoxiu Zhang for being so supportive during my graduate study. I appreciate my husband for his encouragement and support, enabling me to complete my PhD study with less stress and more enthusiasm. There are no words to show my appreciation for all the time and effort he has put forth for the family, especially for our daughter Sharon’s education during these years. Finally, I thank all the faculty and staff of the computer science department for their friendship, encouragement and kindly support. The dedication and commitment of faculty and staff make the University of Alabama a great place to study and do research. v Contents LIST OF ABBREVIATIONS AND SYMBOLS ................................................................... iv ACKNOWLEDGMENTS ....................................................................................................... v LIST OF TABLES ................................................................................................................... x LIST OF FIGURES ................................................................................................................ xi CHAPTER 1 INTRODUCTION ..................................................................................... 1 1.1 The Problem ................................................................................................................. 1 1.2 Our Approach ............................................................................................................... 4 1.3 Contribution .................................................................................................................. 5 1.4 Roadmap ....................................................................................................................... 6 CHAPTER 2 BACKGROUND ....................................................................................... 8 2.1 Code Refactoring .......................................................................................................... 8 2.1.1 Code Analysis .................................................................................................... 9 2.1.2 Code Transformation ....................................................................................... 12 2.2 Reverse Engineering ................................................................................................... 14 2.3 Design Pattern............................................................................................................. 15 2.4 Metamodels for Code Analysis .................................................................................. 17 2.4.1 UML Metamodel ............................................................................................. 17 2.4.2 Columbus ......................................................................................................... 19 2.4.3 Datrix ............................................................................................................... 20 vi 2.4.4 Chen’s Metamodel ........................................................................................... 21 2.4.5 Famix ............................................................................................................... 21 2.4.6 Dagstuhl Middle Metamodel ........................................................................... 22 CHAPTER 3 A METAMODEL FOR C++ SOURCE CODE REFACTORING ......... 24 3.1 Requirements on the New Metamodel ....................................................................... 24 3.2 The Features of C++ ................................................................................................... 27 3.3 Analysis of Primitive Refactorings............................................................................. 28 3.4 Metamodel Design ...................................................................................................... 32 3.5 Refactoring Process Featured with Model-Based Precondition Specification ........... 35 CHAPTER 4 PRECONDITION SPECIFICATIONS FOR PRIMITIVE REFACTORINGS ................................................................................................................. 37 4.1 Object Constraint Language Overview ...................................................................... 37 4.2 Description Template for Precondition Specifications ............................................... 38 4.3 addClass ...................................................................................................................... 40 4.4 renameClass ................................................................................................................ 41 4.5 removeClass................................................................................................................ 42 4.6 addMethod .................................................................................................................. 44 4.7 renameMethod ............................................................................................................ 46 4.8 removeMethod ............................................................................................................ 47 4.9 pullUpMethod ............................................................................................................. 49 4.10 pushDownMethod .....................................................................................................