@@ -227,10 +227,43 @@ class AnnotationType extends Interface {
227
227
228
228
/** Holds if this annotation type is annotated with the meta-annotation `@Inherited`. */
229
229
predicate isInherited ( ) {
230
- exists ( Annotation ann |
231
- ann .getAnnotatedElement ( ) = this and
232
- ann .getType ( ) .hasQualifiedName ( "java.lang.annotation" , "Inherited" )
233
- )
230
+ getADeclaredAnnotation ( ) .getType ( ) .hasQualifiedName ( "java.lang.annotation" , "Inherited" )
231
+ }
232
+
233
+ /** Holds if this annotation type is annotated with the meta-annotation `@Documented`. */
234
+ predicate isDocumented ( ) {
235
+ getADeclaredAnnotation ( ) .getType ( ) .hasQualifiedName ( "java.lang.annotation" , "Documented" )
236
+ }
237
+
238
+ /**
239
+ * Gets the retention policy of this annotation type, that is, the name of one of the
240
+ * enum constants of `java.lang.annotation.RetentionPolicy`. If no explicit retention
241
+ * policy is specified the result is `CLASS`.
242
+ */
243
+ string getRetentionPolicy ( ) {
244
+ if getADeclaredAnnotation ( ) instanceof RetentionAnnotation
245
+ then result = getADeclaredAnnotation ( ) .( RetentionAnnotation ) .getRetentionPolicy ( )
246
+ else
247
+ // If not explicitly specified retention is CLASS
248
+ result = "CLASS"
249
+ }
250
+
251
+ /**
252
+ * Holds if the element type is a possible target for this annotation type.
253
+ * The `elementType` is the name of one of the `java.lang.annotation.ElementType`
254
+ * enum constants. If no explicit target is specified for this annotation type
255
+ * it is considered to be applicable to all elements.
256
+ */
257
+ // Note: Cannot use a predicate with string as result because annotation type without
258
+ // explicit @Target can be applied to all targets, requiring to hardcode element types here
259
+ bindingset [ elementType]
260
+ predicate isATargetType ( string elementType ) {
261
+ if getADeclaredAnnotation ( ) instanceof TargetAnnotation
262
+ then elementType = getADeclaredAnnotation ( ) .( TargetAnnotation ) .getATargetElementType ( )
263
+ else
264
+ // No Target annotation means "applicable to all contexts" since JDK 14, see https://bugs.openjdk.java.net/browse/JDK-8231435
265
+ // The compiler does not completely implement that, but pretend it did
266
+ any ( )
234
267
}
235
268
}
236
269
0 commit comments