Reputation: 67310
I wrote a search like this:
preparedStatement = connect.prepareStatement($sql$);
$code1$;
$resultSet$ = preparedStatement.executeQuery();
$code2$;
if ($resultSet$.next()) {
$code3$;
}
And a replace like this:
try (PreparedStatement preparedStatement = connect.prepareStatement($sql$)) {
$code1$;
try (ResultSet $resultSet$ = preparedStatement.executeQuery()) {
$code2$;
if ($resultSet$.next()) {
$code3$;
}
}
}
When I run this, it finds code that has an if { foo } else { bar}
structure. I'm fine with this, but the problem is it removes the else { bar }
part if I replace. I'd like this search/replace to work sensibly on if statements with an else, multiple else-ifs, or any mixture of these. Is there a way to do this with one search and replace?
I tried changing the if
in the search to have an else and that fixed the issue, but then it skips the code that only has an if without an else.
Upvotes: 0
Views: 292
Reputation: 26462
It is currently not possible to do this with a single Structural Search & Replace. You can however do it with two. You already have the pattern for finding if
s with and else
working. For finding if
s without an else
branch, you can use a pattern like this:
PreparedStatement $preparedStatement$ = $connect$.prepareStatement($sql$);
$code1$;
$resultSet$ = $preparedStatement$.executeQuery();
$code2$;
if ($resultSet$.next()) {
$code3$;
} else $nothing$
Where the constraints (under the Edit Variables button) on the $nothing$
variable are:
min/max occurrences: 0, 0
And using a replace pattern omitting the else
branch:
try (PreparedStatement $preparedStatement$ = $connect$.prepareStatement($sql$)) {
$code1$;
try (ResultSet $resultSet$ = $preparedStatement$.executeQuery()) {
$code2$;
if ($resultSet$.next()) {
$code3$;
}
}
}
Upvotes: 1