| Multi-branch statements are commonly used in everyday software development,including if-else statements and switch statements,which enable dynamic control flow decision-making at program run-time according to the specific value of conditional expressions.Along with the systems development life cycle evolution,code quality issues in multi-branch statements are becoming exposed.Repeated or nested execution branches in multi-branch statements incur an heavy decrement of understandability.The fall-through semantics are applied to switch statements by default,which can result in logical errors if developers fail to add a termination statement at the end of the execution branch in time.The error-prone nature of the fall-through semantics outweighs the flexibility it brings.The release of JDK14 allows for the removal of fall-through semantics from switch statements and supports the switch expression,which is the multi-branch structure containing a clearer and safer control flow.However,no sound work exists to automatically optimize and refactor multi-branch statements,although optimizing multi-branch statements in the current program is valuable to the developer.Refactoring multi-branch statements manually by the developer is obviously time-consuming and error-prone.This thesis presents a novel approach to automatically refactor multi-branch statements.This thesis builds an automaton for the multi-branch statement by performing static program analysis.Branch pattern analysis based on the automaton is used to detect repeated execution branches and optimize nested if-else statements.Fall-through analysis is proposed to detect the effect of fall-through semantics on the execution of switch statements.Switch pattern analysis is proposed to identify refactoring opportunities for switch expressions by analyzing the execution patterns of the branches in switch statements.Following the above analysis,this thesis designs an automatic refactoring approach that includes refactoring to remove repeated execution branches,optimizing nested if-else statements,introducing arrow labels for switch statements to remove fall-through semantics,and refactoring into switch expressions by extracting consistent execution patterns for multi-branch statements.This thesis presents a prototype called Re Brancher implemented as an Eclipse plugin to provide automatic refactoring for multi-branch statements.Re Brancher is evaluated on8 real-world projects containing 24,973 multi-branch statements as benchmarks.The evaluation results show that a total of 1785 multi-branch statements are refactored in160.29 s.Re Brancher improves the average cyclomatic complexity by 4.46% and removes155 code smells,which both demonstrate its effectiveness in improving code quality. |