@NotNull 和 @Nullable,与 Java 注释相矛盾 [英] @NotNull and @Nullable, contradicting java annotations

查看:26
本文介绍了@NotNull 和 @Nullable,与 Java 注释相矛盾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不小心这样做了:

@javax.annotation.Nullable
@javax.validation.constraints.NotNull

编译器没有抱怨,一切似乎都运行良好.

The compiler didn't complain and everything seemed to work well.

现在我想知道当两个或多个注释相互矛盾时内部发生了什么.此外,顺序重要吗?

Now I am wondering what is internally happening when two or more annotations contradict each other. Furthermore, does the order matter?

这是吗

@Nullable
@NotNull

和这个一样吗?

@NotNull
@Nullable

这对于一般的注释是如何工作的?谁赢了?

How is this working for annotations in general? Who wins?

我正在寻找一个通用的答案,不一定是关于这两个注释

I am looking for a general answer, not necessarily about these two annotations

推荐答案

注解本身不会为您的代码带来任何逻辑.每个注释都应使用适当的注释处理器进行处理.注释处理器是使注释有意义的人.

Annotations themselves don't bring any logic to your code. Each annotation should be processed with a proper annotation processor. Annotation processor is the guy who makes the annotation meaningful.

查看官方教程:

注释,一种元数据形式,提供有关程序的数据,该数据不属于程序本身.注解对它们注解的代码的操作没有直接影响.

Annotations, a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.

注解有多种用途,其中包括:

Annotations have a number of uses, among them:

  • 编译器信息 — 编译器可以使用注释来检测错误或抑制警告.
  • 编译时和部署时处理 — 软件工具可以处理注释信息以生成代码、XML 文件等.
  • 运行时处理 - 一些注释可在运行时检查.
  • Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
  • Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
  • Runtime processing — Some annotations are available to be examined at runtime.

让我们考虑一下您上面提到的这两个特定注释.首先,它们并不相互排斥,性质不同.

Let's consider these 2 particular annotations you mentioned above. First of all, they are not mutual exclusive and have the different nature.

@javax.annotation.Nullable 的一部分JSR-305 和它有相反的 - @javax.annotation.Nonnull.这些注释旨在注释您的类字段、方法参数和方法结果.您可以将它们与代码分析器一起使用,其中之一是 Findbugs

The @javax.annotation.Nullable is a part of JSR-305 and it has the opposite one - @javax.annotation.Nonnull. These annotations are designed to annotate your class fields, method params and method results. You can use them with code analyzers, one of them is Findbugs

@javax.validation.constraints.NotNullJSR-303 又名 Bean 验证.所以这个注解与 @javax.annotation.Nullable 没有什么共同之处,因为这些注解有不同的处理器.

The @javax.validation.constraints.NotNull is a part of JSR-303 aka Bean Validation. So this annotation has nothing common with @javax.annotation.Nullable, since these annotations have different processors.

每次你对注解语义有任何疑问时,你应该查看文档,当然你可以运行你的代码,看看会发生什么.

Each time you have any doubts on the annotation semantics, you should check the documentation, and of course you can run you code and see what would happen.

这篇关于@NotNull 和 @Nullable,与 Java 注释相矛盾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆