@@ -50,6 +50,50 @@ class Annotation extends @annotation, Expr {
5050 /** Gets the value of the annotation element with the specified `name`. */
5151 Expr getValue ( string name ) { filteredAnnotValue ( this , this .getAnnotationElement ( name ) , result ) }
5252
53+ /**
54+ * If the value type of the annotation element with the specified `name` is an enum type,
55+ * gets the enum constant used as value for that element.
56+ */
57+ EnumConstant getValueEnumConstant ( string name ) {
58+ result = getValue ( name ) .( FieldRead ) .getField ( )
59+ }
60+
61+ /**
62+ * If the value type of the annotation element with the specified `name` is `String`,
63+ * gets the string value used for that element.
64+ */
65+ string getValueString ( string name ) {
66+ // Uses CompileTimeConstantExpr instead of StringLiteral because value can
67+ // be read of final variable as well
68+ result = getValue ( name ) .( CompileTimeConstantExpr ) .getStringValue ( )
69+ }
70+
71+ /**
72+ * If the value type of the annotation element with the specified `name` is `int`,
73+ * gets the int value used for that element.
74+ */
75+ int getValueInt ( string name ) {
76+ // Uses CompileTimeConstantExpr instead of IntegerLiteral because value can
77+ // be read of final variable as well
78+ result = getValue ( name ) .( CompileTimeConstantExpr ) .getIntValue ( )
79+ }
80+
81+ /**
82+ * If the value type of the annotation element with the specified `name` is `boolean`,
83+ * gets the boolean value used for that element.
84+ */
85+ boolean getValueBoolean ( string name ) {
86+ // Uses CompileTimeConstantExpr instead of BooleanLiteral because value can
87+ // be read of final variable as well
88+ result = getValue ( name ) .( CompileTimeConstantExpr ) .getBooleanValue ( )
89+ }
90+
91+ /**
92+ * If the annotation element with the specified `name` has a Java `Class` as value type,
93+ * gets the referenced type used as value for that element.
94+ */
95+ Type getValueClass ( string name ) { result = getValue ( name ) .( TypeLiteral ) .getReferencedType ( ) }
96+
5397 /** Gets the element being annotated. */
5498 Element getTarget ( ) { result = this .getAnnotatedElement ( ) }
5599
@@ -66,10 +110,24 @@ class Annotation extends @annotation, Expr {
66110 * be one of the elements of that array. Otherwise, the returned value will be the single
67111 * expression defined for the value.
68112 */
69- Expr getAValue ( string name ) {
113+ Expr getAValue ( string name ) { result = getAValue ( name , _) }
114+
115+ /**
116+ * Gets the value at a given index of the annotation element with the specified `name`, which must be
117+ * declared as an array type.
118+ *
119+ * If the annotation element is defined with an array initializer, then the returned value will
120+ * be the elements at the given index of that array. Otherwise, if the index is 0 the returned value
121+ * will be the single expression defined for the value.
122+ */
123+ Expr getAValue ( string name , int index ) {
70124 this .getType ( ) .getAnnotationElement ( name ) .getType ( ) instanceof Array and
71125 exists ( Expr value | value = this .getValue ( name ) |
72- if value instanceof ArrayInit then result = value .( ArrayInit ) .getAnInit ( ) else result = value
126+ if value instanceof ArrayInit
127+ then result = value .( ArrayInit ) .getInit ( index )
128+ else (
129+ index = 0 and result = value
130+ )
73131 )
74132 }
75133
0 commit comments