简体   繁体   English

在Java中,为什么@Nullable具有运行时转储

[英]In Java, why does @Nullable have runtime rentention

正如标题所说,拥有@Nullable和@Nonull可以帮助你的ide或linter在编译时捕获bug,但为什么它有运行时保留?

Conceivably, runtime retention would allow an AOP proxy to detect null arguments being passed to a @Nonnull -annotated parameter, and throw an exception or otherwise handle it. 可以想象,运行时保留将允许AOP代理检测传递给@Nonnull -annotated参数的空参数,并抛出异常或以其他方式处理它。 You could argue that this is a nicer way of handling runtime null checking that using an explicit null check in the code. 您可以争辩说,这是一种处理运行时空检查的更好方法,即在代码中使用显式空检查。

Yes, these annotations are very useful for static compile checking, but there's no reason they couldn't be used at runtime for the same purpose. 是的,这些注释对于静态编译检查非常有用,但是没有理由它们不能在运行时用于相同的目的。 Their validity as annotations extends into the runtime domain, unlike annotations that are used for things like code generation. 它们作为注释的有效性延伸到运行时域,不像用于代码生成之类的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM