-1

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;
    }
Community
  • 1
  • 1
Laf
  • 7,965
  • 4
  • 37
  • 52

2 Answers2

1

You cannot "merge" annotations with custom annotations, however you can use @ParametersAreNonnullByDefault on the class scope, which should allow edu.umd.cs.findbugs.annotations.NonNull to be inferred.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
0

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.

Vampire
  • 35,631
  • 4
  • 76
  • 102