简体   繁体   中英

Create annotation that includes FindBugs' and Lombok's @NonNull

I would like to use Lombok's @NonNull annotation to generate the null-checking code automatically for method parameters while also using FindBugs' @NonNull to use static analysis tools and generate appropriate warnings whenver the case applies.

As of now, I need to do the following:

public void doSomething (@lombok.NonNull @edu.umd.cs.findbugs.annotations.NonNull Object parameter)
    {
    // Do something
    }

This is quite ugly, so I would like to avoid using this syntax. I read about nested annotations ( here and here ), but I can't seem to find a way to create my own custom annotation with both NonNull annotations as nested annotations. Am I trying to do something that cannot work?

Here's my latest attempt:

@Documented
@Retention (RetentionPolicy.CLASS)
@Target (value={ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
public @interface MyNonNull
    {
    public lombok.NonNull lombokNonNull () default @lombok.NonNull; 
    public edu.umd.cs.findbugs.annotations.NonNull findBugsNonNull () default @edu.umd.cs.findbugs.annotations.NonNull;
    }

您不能将注释与自定义注释“合并”,但是您可以在类范围上使用@ParametersAreNonnullByDefault ,这应该允许推断edu.umd.cs.findbugs.annotations.NonNull

I think neither of your links will help you. You would need to extend from both with your custom annotation, yet you cannot even extend from one, as all annotations have an implicit extends clause for Annotation.

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