org.jetbrains.annotations.Contract可以处理Integer范围吗? &



我的目标是在运行时删除硬编码的参数检查。从我所看到的,@Contract只是一个Java注释,它不会在编译时注入任何逻辑。本例中greater thanless than参数检查

我的预期结果:我可以处理范围与@Contract,所以开发人员将被通知,如果他们使用规范之外的方法。

我的实际结果:intelliJ IDEA不期望我的合同。

我所做的:

  1. 我试图使用:
@Contract(_ < 1000 || _ > 4999,_ -> fail) // IntelliJ IDEA does not expect `_ < 1000 || _ 4999`
@Contract(number,_ -> fail) // IntelliJ IDEA does not expect `number` although in the documentation, it is a syntax.

硬编码参数检查。

/**
* @param code in the range 1000-4999 except 1005, 1006, 1015
* @param reason if reason != null, then code != null
* @return unmasked-ws-frame
*/
public byte[] encodeCloseFrame(Integer code, String reason) {
if (code == null || code == 1005) {
return null;
}
if (code == 1006 || code == 1015)
throw new IllegalArgumentException("/code/ " + code + " is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint");
if (code < 1000 || code > 4999)
throw new IllegalArgumentException("/code/ in the range 1000-4999");
if (reason != null) {
}
return null;
}

看一下org.jetbrains.annotations.Range注释。Return value is outside of declared range检验的描述提供了一些更有用的信息。

最新更新