java - 2 x @NotNull == 太多了?
问题描述
BytesUtil.bytesEqual参数使用 Jetbrains和 OpenHFT注释相同的参数:@NotNull
@NotNull
public static boolean bytesEqual(
@org.jetbrains.annotations.NotNull @NotNull RandomDataInput a, long offset,
@org.jetbrains.annotations.NotNull @NotNull RandomDataInput second, long secondOffset, long len)
throws BufferUnderflowException {
这似乎是多余的——有什么理由同时使用两者吗?这两个注释(当前)定义为:
package net.openhft.chronicle.core.annotation;
@Documented
@Retention(CLASS)
@Target({METHOD, FIELD, PARAMETER, LOCAL_VARIABLE})
public @interface NotNull {
}
和
package org.jetbrains.annotations;
@Documented
@Retention(CLASS)
@Target({METHOD, FIELD, PARAMETER, LOCAL_VARIABLE})
public @interface NotNull {
String value() default "";
}
所以 Jetbrains@NotNull
提供了一个默认的空字符串值,否则两个注释是相同的......那么为什么要同时指定呢?
解决方案
我们对 IntelliJ 注释的问题是,当启用字节码检测时,它会添加一个检查,该检查会抛出一个IllegalArgumentException
. 但是,当代码在另一个上下文中发布或运行时,它会触发一个NullPointerException
.
出于这个原因,我们在大部分代码库中添加了我们自己的注释,因此在 IntelliJ 中将进行代码分析检查,而无需添加额外的运行时检查。
很可能我们应该只在任何地方使用我们的注释来使抛出的异常具有确定性。
推荐阅读
- android - MPAndroidchart 两轴重叠
- elasticsearch - 如何从 Elasticsearch 索引中的两个字段派生一个字段?
- angular - 如何在不同的项目中使用自定义的 Angular 模块
- python - 如何在熊猫中读取excel文件,其中每个单元格中的列多于一行
- python - 如何从文本文件上下文菜单运行 python 脚本并将其与其他文本文件进行比较?
- julia - 如何在 Julia 中将纳秒精度的纪元时间戳转换为日期时间
- mysql - 如何计算患者点击出院按钮入院的天数?
- hyperledger-fabric - 启动 Hyperledger Explorer 时出现问题
- python - 为什么不在基本模板中使用 ListView?如何从 ech url 的基本模板调用视图?Django 2.1.5
- android - 带有 EMM 社区的 Android 管理 API