Tests if the specified package, program element or constructor/method's parameter has an annotation of the specified type.
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 returnsfalse
.If this parameter is not specified, the generator context element is assumed, i.e. the same as the call:
contextElement.hasAnnotation(qualifiedName)
qualifiedName
The fully qualified name of the annotation type to search for.
qualifiedNames
Instead of a single name, you may specify several fully qualified names in the form of an array (e.g. created withReturns:Array()
function). For example:In that case, the function will search for an annotation with the first type name specified in the array, then, if not found, for an annotation with the second type name and so on.hasAnnotation ( Array ( "project.util.Annotation1", "project.util.Annotation2" ) )
true
, if the specified element has an annotation of the specified type;false
, otherwise.
Example:
Let's suppose we have a method in our Java sources specified like this:
Then, the following call will tests if the methos has the@Deprecated void myMethod() { ... }
@Deprecated
annotation:
(provided that the method is currently the generator context element).findAnnotation("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.hasChild("AnnotationDesc", BooleanQuery( checkValueByLPath( "annotationType^::AnnotationTypeDoc/@qualifiedName", "java.lang.Deprecated" ) ))
Since checking Java annotations may be a rather frequent operation in templates, it was implemented a separate function.
See Also:
findAnnotation(), getAnnotation()