Returns an annotation of the specified type assigned to the specified package, program element or constructor/method's parameter.

Parameters:

element

The PackageDoc, ProgramElementDoc or Parameter element whose annotation is requested.

It may be also a Type element, which is automatically converted to the ClassDoc by calling the Doclet API method: Type.asClassDoc()

If the element is not an instance of one of those types, the function returns null.

If this parameter is not specified, the generator context element is assumed, i.e. the same as the call: contextElement.getAnnotation(qualifiedName)

qualifiedName
The fully qualified name of the annotation type to search for.
Returns:
The AnnotationDesc element representing the found annotation or null, if no annotation found.

Example:

Let's suppose we have a method in our Java sources specified like this:


@Deprecated
void myMethod() { ... }
Then, the AnnotationDesc element representing the method's @Deprecated annotation can be obtained by the call:
getAnnotation("java.lang.Deprecated")
(given that the method is currently the generator context element).

Note that the same result can be achieved with the following expression:


findChild("AnnotationDesc", BooleanQuery(
  checkValueByLPath(
    "annotationType^::AnnotationTypeDoc/@qualifiedName", 
    "java.lang.Deprecated"
  )
))
That expression uses a much more universal approach based on Location Path (which is a variation of XML XPath). However, specifying it is more cumbersome and this will work slower.

Since obtaining Java annotations may be a rather frequent operation in templates, it was implemented a separate function.

See Also:

hasAnnotation(), findAnnotation()