Javadoc doclet : Get “@Nullable” annotation (javax). How to get that annotation in the doclet?

StackOverflow https://stackoverflow.com/questions/11311041

  •  18-06-2021
  •  | 
  •  

Вопрос

I am creating my own doclet, and I need to show when a parameter from a method is nullable or not. For that, I want to use the @Nullable annotation from javax.annotation. Somthing like what the people of Google say here:

public Object myMethod(String firstName, String lastName, @Nullable Phone phone) { 
//code
}

Is there any way in my Doclet to get that "nullable" annotation for that Phone parameter?

Это было полезно?

Решение

Found it!

The Parameter interface has already a pretty annotations() method, from which we can get an AnnotationDesc array from all the annotations related to that parameter. So, we just have to take the desired parameter from the MethodDoc (MethodDoc.parameters()), and from there, the annotations from that parameter whith the annotations() method (AnnotationDesc[] annotations = pParam.annotations();). From there, it is easy to get if an annotation related to that parameter is "nullable" (:

for(AnnotationDesc annotation : annotations) {
      if(annotation.toString().equals("@javax.annotation.Nullable")) { ...

For example.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top