Returns an annotation of the specified type assigned to the specified package, program element or constructor/method's parameter.
Parameters:
element
ThePackageDoc
,ProgramElementDoc
orParameter
element whose annotation is requested.It may be also a
Type
element, which is automatically converted to theClassDoc
by calling the Doclet API method:Type.asClassDoc()
If the
element
is not an instance of one of those types, the function returnsnull
.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:
TheAnnotationDesc
element representing the found annotation ornull
, if no annotation found.
Example:
Let's suppose we have a method in our Java sources specified like this:
Then, the@Deprecated void myMethod() { ... }
AnnotationDesc
element representing the method's
@Deprecated
annotation can be obtained by the call:
(given that the method is currently the generator context element).getAnnotation("java.lang.Deprecated")
Note that the same result can be achieved with the following expression:
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.findChild("AnnotationDesc", BooleanQuery( checkValueByLPath( "annotationType^::AnnotationTypeDoc/@qualifiedName", "java.lang.Deprecated" ) ))
Since obtaining Java annotations may be a rather frequent operation in templates, it was implemented a separate function.
See Also:
hasAnnotation(), findAnnotation()