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

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

问题描述

发生在我身上的事情是我不小心这样做的:

It happened to me that I accidentally did this:

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

让我们考虑一下您上面提到的这2个特殊注释.首先,它们不是互斥的,并且具有不同的性质.

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.这些注释旨在注释您的类字段,方法参数和方法结果.您可以将它们与代码分析器一起使用,其中之一是查找错误

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.NotNull JSR-303 aka 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天全站免登陆