@@ -38,7 +38,7 @@ abstract class TypeChecker {
3838 /// package like in the `dart:` SDK.
3939 const factory TypeChecker .fromUrl (dynamic url) = _UriTypeChecker ;
4040
41- /// Returns the first constant annotating [element] that is this type.
41+ /// Returns the first constant annotating [element] assignable to this type.
4242 ///
4343 /// Otherwise returns `null` .
4444 DartObject firstAnnotationOf (Element element) {
@@ -49,18 +49,30 @@ abstract class TypeChecker {
4949 return results.isEmpty ? null : results.first;
5050 }
5151
52- /// Returns every constant annotating [element] that is this type.
52+ /// Returns the first constant annotating [element] that is exactly this type.
53+ DartObject firstAnnotationOfExact (Element element) {
54+ if (element.metadata.isEmpty) {
55+ return null ;
56+ }
57+ final results = annotationsOfExact (element);
58+ return results.isEmpty ? null : results.first;
59+ }
60+
61+ /// Returns annotating constants on [element] assignable to this type.
5362 Iterable <DartObject > annotationsOf (Element element) => element.metadata
63+ .map ((a) => a.computeConstantValue ())
64+ .where ((a) => isAssignableFromType (a.type));
65+
66+ /// Returns annotating constants on [element] of exactly this type.
67+ Iterable <DartObject > annotationsOfExact (Element element) => element.metadata
5468 .map ((a) => a.computeConstantValue ())
5569 .where ((a) => isExactlyType (a.type));
5670
57- /// Returns `true` if the type of [element] can be assigned to the type
58- /// represented by `this` .
71+ /// Returns `true` if the type of [element] can be assigned to this type.
5972 bool isAssignableFrom (Element element) =>
6073 isExactly (element) || _getAllSupertypes (element).any (isExactlyType);
6174
62- /// Returns `true` if [staticType] can be assigned to the type represented
63- /// by `this` .
75+ /// Returns `true` if [staticType] can be assigned to this type.
6476 bool isAssignableFromType (DartType staticType) =>
6577 isAssignableFrom (staticType.element);
6678
@@ -97,7 +109,7 @@ abstract class TypeChecker {
97109 bool isSuperTypeOf (DartType staticType) => isSuperOf (staticType.element);
98110}
99111
100- //TODO(kevmoo) Remove when bug with `ClassElement.allSupertypes` is fixed
112+ // TODO(kevmoo) Remove when bug with `ClassElement.allSupertypes` is fixed
101113// https://github.com/dart-lang/sdk/issues/29767
102114Iterable <InterfaceType > _getAllSupertypes (Element element) sync * {
103115 if (element is ClassElement ) {
0 commit comments