我希望使我的代码更具可读性,以及使用工具,如IDE代码检查和/或静态代码分析(FindBugs和Sonar),以避免nullpointerexception。许多工具似乎与彼此的@NotNull/@NonNull/ @NonNull注释不兼容,在我的代码中列出所有这些工具会让人难以阅读。有没有什么建议,哪一个是“最好的”?下面是我找到的等价注释列表:
javax.validation.constraints.NotNull
Created for runtime validation, not static analysis.
documentation
edu.umd.cs.findbugs.annotations.NonNull
Used by FindBugs (dead project) and its successor SpotBugs static analysis and therefore Sonar (now Sonarqube)
FindBugs documentation, SpotBugs documentation
javax.annotation.Nonnull
This might work with FindBugs too, but JSR-305 is inactive. (See also: What is the status of JSR 305?)
source
org.jetbrains.annotations.NotNull
Used by IntelliJ IDEA IDE for static analysis.
documentation
lombok.NonNull
Used to control code generation in Project Lombok.
Placeholder annotation since there is no standard.
source,
documentation
androidx.annotation.NonNull
Marker annotation available in Android, provided by annotation package
documentation
org.eclipse.jdt.annotation.NonNull
Used by Eclipse for static code analysis
documentation
如果你在做一个大项目,你最好创建自己的@Nullable和/或@NotNull注解。
例如:
@java.lang.annotation.Documented
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS)
@java.lang.annotation.Target({java.lang.annotation.ElementType.FIELD,
java.lang.annotation.ElementType.METHOD,
java.lang.annotation.ElementType.PARAMETER,
java.lang.annotation.ElementType.LOCAL_VARIABLE})
public @interface Nullable
{
}
如果您使用了正确的保留策略,那么注释在运行时将不可用。从这个角度来看,它只是一种内在的东西。
尽管这不是一门严格的科学,但我认为使用内部类是最有意义的。
这是一个内在的东西。(没有功能或技术影响)
有很多很多的用法。
像IntelliJ这样的IDE支持自定义的@Nullable/@NotNull注释。
大多数框架也喜欢使用自己的内部版本。
其他问题(见评论):
如何在IntelliJ中配置这个?
点击IntelliJ状态栏右下角的“警官”。在弹出窗口中单击“配置巡检”。下一个……
如果你在做一个大项目,你最好创建自己的@Nullable和/或@NotNull注解。
例如:
@java.lang.annotation.Documented
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS)
@java.lang.annotation.Target({java.lang.annotation.ElementType.FIELD,
java.lang.annotation.ElementType.METHOD,
java.lang.annotation.ElementType.PARAMETER,
java.lang.annotation.ElementType.LOCAL_VARIABLE})
public @interface Nullable
{
}
如果您使用了正确的保留策略,那么注释在运行时将不可用。从这个角度来看,它只是一种内在的东西。
尽管这不是一门严格的科学,但我认为使用内部类是最有意义的。
这是一个内在的东西。(没有功能或技术影响)
有很多很多的用法。
像IntelliJ这样的IDE支持自定义的@Nullable/@NotNull注释。
大多数框架也喜欢使用自己的内部版本。
其他问题(见评论):
如何在IntelliJ中配置这个?
点击IntelliJ状态栏右下角的“警官”。在弹出窗口中单击“配置巡检”。下一个……
One of the nice things about IntelliJ is that you don't need to use their annotations. You can write your own, or you can use those of whatever other tool you like. You're not even limited to a single type. If you're using two libraries that use different @NotNull annotations, you can tell IntelliJ to use both of them. To do this, go to "Configure Inspections", click on the "Constant Conditions & Exceptions" inspection, and hit the "Configure inspections" button. I use the Nullness Checker wherever I can, so I set up IntelliJ to use those annotations, but you can make it work with whatever other tool you want. (I have no opinion on the other tools because I've been using IntelliJ's inspections for years, and I love them.)