针对手工重构存在容易引入错误且重构效率较低等问题,本文提出了一种以单例模式为导向的源代码自动重构方法.首先将源代码转换成抽象语法树,然后对抽象语法树进行操作,按照单例模式的要求修改抽象语法树,并自动生成重构后的单例类.该方法不仅可以对指定类进行自动单例化重构,还可以对源代码中所有创建该类实例的代码进行自动重构.对方法进行正确性检验并将其应用于两个不同规模的软件项目,实验结果表明该方法可以准确、高效地实现类的自动单例化重构,重构算法的执行时间与系统规模呈线性关系.
In order to reduce introducing errors and increase refactoring efficiency in manual refactoring, a novel automatic refactoring approach which is the Singleton pattern directed for source code is proposed in this paper. Source code is transformed to an abstract syntax tree firstly. Then the abstract syntax tree is handled and modified in accordance with the standard of the Singleton pattern, and the Singleton class is generated by an automatic refactoring program. Not only the target class is refactored to a Singleton class, but also the other classes which have statements for creating the instance of the Singleton are refactored automatically. The approach' s correctness is verified during the experiment stage, and this approach is applied to two software projects with different scales. The experimental results show that the approach can implement automatic refactoring to the Singleton pattern accurately and efficiently,and the execution time of the automatic algorithm has a linear relationship with the size of system.