How to create Surround with Null Check Live Template

I am trying to create a Surround with Null Check Live Template.

It takes a Statement like this:

   <selection start>obj.getProperty()<selection end>.setValue(5);

and wraps it in a Null Check expression:

   if (obj.getPropery() != null) {
           obj.getProperty().setValue(5);
        }

In words: I want to select an expression, extract that expression into a null check if-statement, and then leave the entire statement in the body.

Is that possible in Live Templates?

4 comments
Comment actions Permalink

Check "surround" live templates section

0
Comment actions Permalink

looking at your usecase, I am not sure that is possible

0
Comment actions Permalink

That's only possible if you create a plugin that defines a new macro function, then it would look like this
if ($SELECTION$ != null) {
  $STMT$
}
where $STMT$=containingStatement($SELECTION$).

It would be perhaps better to have a quickfix that does it, when you have a possible NPE warning. You could file a request or even contribute such a quick fix :)

0

Please sign in to leave a comment.