首页 > 解决方案 > 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提供了一个默认的空字符串值,否则两个注释是相同的......那么为什么要同时指定呢?

标签: javaintellij-ideachronicle

解决方案


我们对 IntelliJ 注释的问题是,当启用字节码检测时,它会添加一个检查,该检查会抛出一个IllegalArgumentException. 但是,当代码在另一个上下文中发布或运行时,它会触发一个NullPointerException.

出于这个原因,我们在大部分代码库中添加了我们自己的注释,因此在 IntelliJ 中将进行代码分析检查,而无需添加额外的运行时检查。

很可能我们应该只在任何地方使用我们的注释来使抛出的异常具有确定性。


推荐阅读