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) {

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?


Check "surround" live templates section


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


That's only possible if you create a plugin that defines a new macro function, then it would look like this
if ($SELECTION$ != null) {
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 :)


Please sign in to leave a comment.