简体   繁体   English

龙目岛中的自定义`NonNull`注释

[英]Custom `NonNull` annotation in Lombok

Here's the java-doc of NonNull annotation of Lombok: 这是Lombok的NonNull注释的Java文档:

If put on a parameter, lombok will insert a null-check at the start of the method / constructor's body, throwing a {@code NullPointerException} with the parameter's name as message. 如果放置在参数上,则lombok将在方法/构造函数主体的开头插入空检查,并抛出一个带有参数名称作为消息的{@code NullPointerException}。 If put on a field, any generated method assigning a value to this field will also produce these nullchecks. 如果放在字段上,则任何为此字段分配值的生成方法也会产生这些nullcheck。 Note that any annotation named {@code NonNull} with any casing and any package will result in nullchecks produced for generated methods (and the annotation will be copied to the getter return type and any parameters of generated methods), but only this annotation, if present on a parameter, will result in a null check inserted into your otherwise handwritten method. 请注意,带有任何大小写和任何包的名为{@code NonNull}的任何注释都将导致为生成的方法生成nullcheck(并且注释将被复制到getter返回类型和生成的方法的任何参数),但是只有此注释,如果如果出现在参数上,则会导致将空检查插入您本来要手写的方法中。

WARNING: If the java community ever does decide on supporting a single {@code @NonNull} annotation (for example via JSR305), then this annotation will be deleted from the lombok package. 警告:如果Java社区确实决定支持单个{@code @NonNull}注释(例如,通过JSR305),则将从lombok软件包中删除该注释。 If the need to update an import statement scares you, you should use your own annotation named {@code @NonNull} instead of this one. 如果需要更新导入语句使您感到恐惧,则应使用自己的名为{@code @NonNull}的注释,而不要使用此注释。

What is the simplest way to have my own annotation, let's say 假设我拥有自己的注释的最简单方法是什么 NonNull Nonnull , and Lombok to inject null-check based on my annotation? Nonnull和Lombok根据我的注释注入null检查?

Update: my question is hot to have an annotation to use for method arguments. 更新:我的问题很热门,要有用于方法参数的注释。

First, you need to name it nonNull (casing is irrelevant). 首先,您需要将其命名为nonNull (不相关)。 NotNull will not be recognized by Lombok. NotNull无法识别NotNull Additionally you need to set another Lombok annotation (eg @Data , @Setter , ...), so that your type gets processed by Lombok. 此外,您需要设置另一个龙目岛标注(如@Data@Setter ,...),让你的类型得到由龙目岛处理。

Summarizing your custom annotation isn't probably as valuable as the @lombok.NonNull -annotation itself. 总结您的自定义注释可能不如@lombok.NonNull本身有价值。 An example where you benefit from the @lombok.NonNull -annotation, where your custom annotation wouldn't even be processed, is, when your type doesn't contain any other Lombok annotation, eg: 一个受益于@lombok.NonNull -annotation的示例是,当您的类型不包含任何其他Lombok注释时,甚至不处理自定义注释,例如:

class NoLombokAnnotationsAnywhere {
  void testMe(@lombok.NonNull String nonNull) { /* .. */ }
}

will produce a NullPointerException as soon as you call new NoLombokAnnotationsAnywhere().testMe(null) . 调用new NoLombokAnnotationsAnywhere().testMe(null)将立即生成NullPointerException Whereas this wouldn't throw anything with your custom annotation. 而这不会对您的自定义注释产生任何影响。 Of course this only applies as long as you don't have any other Lombok annotations there. 当然,这仅在您没有其他Lombok注释的情况下适用。 As soon as the type gets processed by Lombok, your annotation gets processed too. 一旦Lombok处理了类型,您的注释也将被处理。

If you have your own NonNull -annotation, then you can add just another Lombok-annotation that seems appropriate and Lombok adds a null-check for you, eg: 如果您有自己的NonNull ,则可以仅添加另一个似乎合适的Lombok注释,Lombok为您添加一个空检查,例如:

@Data
class NonNullData {
  @mycustom.Nonnull
  String value;
}

// Calling the following throws a NullPointerException as expected
new NonNullData(null);

You may also find the following issue relevant: Support annotations named @NotNull as well as @NonNull 您可能还会发现以下相关问题: 支持名为@NotNull和@NonNull的注释

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

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