Use Pattern Matching for Instanceof

jSparrow 4.2.0
This new jSparrow release supports upgrading to Java 16.

New refactoring rule to your toolbox:

Use Pattern Matching for Instanceof

This rule replaces instanceof expressions by Pattern Matching for instanceof introduced in Java 16.

It is common for Java programs to contain logic that combines type checking using instanceof with explicit type casting. Naturally, an instanceof expression is followed by a local variable declaration initialized with a casting expression. Pattern Matching for instanceof combines these three steps (i.e., type checking, variable declaration, and type casting) into a single step, thus reducing boilerplate code and eliminating sources of errors.

The following code:

if(athlete instanceof  Swimmer) {
    Swimmer swimmer = (Swimmer)athlete;		
    sendGoldMedal(swimmer);
}

is transformed to:

if(athlete instanceof  Swimmer swimmer) {
    sendGoldMedal(swimmer);
}

This new rule brings jSparrow to a total of 100 automatic refactoring rules.

Find out more information in the Release Notes for jSparrow Eclipse!

“Brevity is the soul of wit.” ― William Shakespeare