-1

I want to be able to type something like this:

if (object) {
    // some code...
}

and have the compiler see it as this:

if (object != null) {
    // some code...
}

Is there any way I can do this?

Alternatively: is there anything that can be done in Eclipse or IntelliJ that will do this?

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
Ky -
  • 30,724
  • 51
  • 192
  • 308
  • Alternatively: is there anything that can be done in Eclipse or IntelliJ that will do this? – Ky - May 28 '15 at 15:42
  • That should be possible as a shortcut, like `sout`, `fori` and such. – Binkan Salaryman May 28 '15 at 15:42
  • 1
    @BenC.R.Leggiero Just curious as to why you would want to do something like this? – Chetan Kinger May 28 '15 at 15:44
  • In Intellij you could create a live template, which is the IntelliJ name for `sout` like things. – Timo May 28 '15 at 15:44
  • @ChetanKinger I and others on my team complain that this is something we miss from C-based languages. – Ky - May 28 '15 at 15:46
  • If you have to check for nulls that frequently that you found it worth investing the time to look for a workaround suggests you should also ask if you're not excessively using nullable returns (instead of e.g. throwing, null-object or simply letting the exception bubbling up). Especially since you mention a prior C-background, not every C-idiom that can be copied directly to java, fits too well into a mostly OO-world. In the long run you might benefit more from shedding the old habits. – Durandal May 28 '15 at 15:57
  • @Durandal mostly, this is Android programming. When getting a GUI object, we must make sure it is not null before performing operations on it. – Ky - May 28 '15 at 16:28
  • 1
    This is going to be so fun when the entire team uses it, and your IDEs silently replace `Boolean b; if (b) { }` with `Boolean b; if (b != null) { }` (Yes, won't exactly work with LiveTemplates, but still) – Ordous May 28 '15 at 16:37

2 Answers2

2

There aren't any compiler settings you could set to get this behavior. I don't think you could really do this elegantly with a code generator (something like Lombok), either since you can't really annotate statements.

An alternative would be to use IntelliJ Live Templates for both if (args != null) and if (args == null)

  • ifn expands to if (args == null)
  • inn expands to if (args != null)
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • I believe you are missing the point. The OP is not looking for IDE shortcuts but a compiler option to allow if(object) where object is not a boolean – Chetan Kinger May 28 '15 at 15:51
  • 2
    @ChetanKinger Although you're correct if only looking at the question, don't ignore my comments – Ky - May 28 '15 at 15:52
  • @BenC.R.Leggiero: Please don't leave important bits of the question in comments. They're harder to see. – Michael Myers May 28 '15 at 16:19
  • @MichaelMyers I usually don't, but ever since including some meta information in a question and being told to leave that in a comment instead, I've been trying to find the line. – Ky - May 28 '15 at 16:27
  • 2
    @BenC.R.Leggiero: I see. A good rule of thumb is that if you can't understand the question without the comment (as in this case, where it affects the answers), then the comment should be part of the question. – Michael Myers May 28 '15 at 16:34
0

When in IntelliJ press ctrl + j, then type: inn - and voilà:

if (args != null) {

}

Btw. this is called a live template

Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
  • 1
    I believe you are missing the point. The OP is not looking for IDE shortcuts but a compiler option to allow `if(object)` where object is not a `boolean`. – Chetan Kinger May 28 '15 at 15:51
  • 1
    I didn't miss the point. This is an alternative as specified in the first comment by the author - "Alternatively: is there anything that can be done in Eclipse or IntelliJ that will do this? – " – Binkan Salaryman May 28 '15 at 15:52
  • 1
    My bad. I have edited the question and added the comment as part of the question to avoid the confusion. – Chetan Kinger May 28 '15 at 15:54