是否有 Java 编译器选项允许 if(object)?

Is there a Java compiler option to allow if(object)?

我希望能够输入如下内容:

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

并让编译器将其视为:

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

有什么办法可以做到吗?

或者:是否有任何可以在 Eclipse 或 IntelliJ 中完成的事情可以做到这一点?

在 IntelliJ 中按 ctrl + j,然后键入:inn - 瞧:

if (args != null) {

}

顺便说一句。这叫做 live template

您无法设置任何编译器设置来实现此行为。我不认为你真的可以用代码生成器(比如 Lombok)优雅地做到这一点,因为你不能真正注释语句。

另一种方法是对 if (args != null)if (args == null)

使用 IntelliJ Live Templates
  • ifn 扩展为 if (args == null)
  • inn 扩展为 if (args != null)