Abstract
The extit{instanceof} pattern matching is periodically previewed in
the latest published JDK by removing the redundant casting and
optimizing its usage in different scenarios, which improves the code
quality and readability. However, the existing integrated development
environment (IDE) does not provide sufficient support for refactoring of
extit{instanceof} pattern matching in all situations. This paper first
identifies several cases that cannot be handled by existing IDEs, and
then proposes a novel approach called extit{ReInstancer} to refactor
extit{instanceof} pattern matching automatically. extit{ReInstancer}
conducts visitor pattern analysis for extit{if} statement blocks with
extit{instanceof}. For the extit{instanceof} expression, a pattern
variable is obtained after removing the redundant type casting by
pattern analysis. For multi-branch statements, extit{ReInstancer}
employs class hierarchy analysis and control flow analysis to infer the
relationship of instance type in each branch and then optimize these
branches. Finally, the multi-branch statements are converted into switch
statements or switch expressions. extit{ReInstancer} is evaluated with
20 real-world applications. The experimental results demonstrate a total
of 3558 extit{instanceof} expressions and 228 multi-branch statements
applying the refactoring, and each application takes an average of
10.8s. extit{ReInstancer} not only reduces the redundant type casting
but also improves the code quality.