| Pattern matching has been periodically previewed in several of the latest JDK versions.This technique can eliminate redundant castings generated during the use of instanceof,optimize its usage in different scenarios,and thus improve code quality and readability.However,existing integrated development environments do not provide sufficient support for refactoring of instanceof pattern matching in special cases,and the optimization of multi-branch statements with instanceof is not yet perfect.When programmers try to refactor existing programs using integrated development environments,they need to manually select the code fragments that need to be refactored and complete the refactoring through built-in plugins,which is time-consuming,labor-intensive,and may introduce new errors.Therefore,research on refactoring methods for instanceof pattern matching is needed.To address the problem that the readability and code quality of programs are seriously affected by the use of instanceof,this thesis presents an automatic refactoring approach for instanceof pattern matching.The method first traverses and analyzes statement blocks that use instanceof as a conditional expression and redundant type castings in the statement blocks using the visitor pattern.Then,the redundant castings that match the instanceof pattern are obtained through instanceof pattern matching analysis.Next,special patterns containing multi-branch statements with instanceof are obtained through class hierarchy analysis and control flow analysis,and different analysis algorithms for each pattern are designed.Finally,a refactoring algorithm is designed to eliminate redundant castings produced during the use of instanceof and convert multi-branch statements with instanceof to switch statements or switch expressions.Based on this method,an automatic refactoring tool,Re Instancer,is implemented using the Eclipse JDT framework.In order to verify the effectiveness and applicability of Re Instancer,20 large-scale practical applications were evaluated in five metrics,including the number of refactorings,the number of changed lines of code,refactoring time,correctness,and changes in code quality after refactoring.The experiments showed that Re Instancer can refactor 3558 instanceof and 228 multi-branch statements with instanceof,with an average time of 10.8 seconds per program.Re Inatancer can effectively improve the efficiency of refactoring,not only eliminating redundant castings but also improving the quality of the code. |