getDirectAnnotation<A> method

A? getDirectAnnotation<A>()

Gets a single annotation by type, if present.

Type Parameters:

  • A: The annotation type to look for

Returns:

  • The annotation instance of type A if found
  • null if no matching annotation exists

Example:

final deprecated = method.getDirectAnnotation<Deprecated>();
if (deprecated != null) {
  print('Deprecation message: ${deprecated.message}');
}

Implementation

A? getDirectAnnotation<A>() {
  checkAccess('getDirectAnnotation', DomainPermission.READ_ANNOTATIONS);

  final annotations = getAllDirectAnnotations();
  for (final annotation in annotations) {
    if (annotationMatches<A>(annotation)) {
      try {
        return annotation.getInstance<A>();
      } catch (_) {
        return null;
      }
    }
  }

  return null;
}