简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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