0

I'm creating own inspections based on Structural Replace. For example I want to make inspection of transforming code like:

if (!$Map$.containsKey($key$)){
    $Map$.put($key$, $value$);
}

and

if ($Map$.get($key$) == null){
    $Map$.put($key$, $value$);
}

into

$Map$.putIfAbsent($key$, $value$);

But I don't want it to react on code like:

if (!$Map$.containsKey($key$)){
    $Map$.put($key$, $value$);
}
else {
    // any logic
}

I tried to use

if (!$Map$.containsKey($key$)){
    $Map$.put($key$, $value$);
}
$else$

with option text "else" but it didn't work.

Is it possible? Also I have to make two different inspections with same replace result. Can we use multiple searh pattern?

UPDATE:

I tried replace next pattern

$Iterable$.forEach($value$ -> {
    if ($condition$) {
        $statement$;
    }
});

into

$Iterable$.stream()
        .filter($value$ -> $condition$)
        .forEach($value$ -> $statement$);

But after replace I'm getting:

$Iterable$.stream()
        .filter($value$ -> $condition$)
        .forEach($value$ -> $statement$;);

Is it possible to remove ";" from replace result?

agavlyukovskiy
  • 231
  • 3
  • 11
  • Regarding *Update* ; it looks like you are running into some bugs in Structural Search. Seems like this is not possible at the moment. – Bas Leijdekkers Jan 23 '16 at 10:29
  • Update on your update: the bug is fixed. In IntelliJ IDEA 2017.2 your structural replace does not produce extra semicolons any longer. – Bas Leijdekkers Aug 18 '17 at 13:36

1 Answers1

0

Use a search template like this:

if (!$Map$.containsKey($key$)){
    $Map$.put($key$, $value$);
} else {
    $statement$;
}

Edit variables and set the minimum and maximum count of statement to 0.

Using multiple search patterns for a single Structural Search Inspection is not possible at this time.

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
  • It works. Thanks a lot. Also do you know is it possible to make replace with import? For example replace $String$ == null || $String$.isEmpty() to org.apache.lang3.StringUtils.isEmpty($String$) – agavlyukovskiy Jan 22 '16 at 11:31
  • Enable the `shorten fully qualified names` checkbox in the Structural Replace dialog? – Bas Leijdekkers Jan 22 '16 at 12:21
  • Thanks, I tried it yesterday. But it didn't work, maybe I had another problem. Could you please answer on one more question in updates, as you are familiar with this topic. – agavlyukovskiy Jan 22 '16 at 13:39