Annotation class abstract final

Provides reflective access to annotations applied to declarations.

This interface abstracts annotation metadata access, allowing inspection of:

  • Annotation types and signatures
  • Field values (both user-provided and defaults)
  • Protection domains
  • Actual annotation instances

{@template annotation_interface_features}

Key Features

  • Type-safe field value access
  • Distinction between default and user-provided values
  • Protection domain awareness
  • Full metadata inspection
  • Instance retrieval when available

Implementation Notes

Concrete implementations typically wrap platform-specific reflection objects while providing this uniform interface.

Example Usage

// Get annotation from a reflected method
final annotation = method.getAnnotation<JsonSerializable>();

// Inspect field values
final explicitNulls = annotation?.getFieldValueAs<bool>('explicitToNull');
final fieldRename = annotation?.getFieldValue('fieldRename');

// Check value sources
if (annotation?.hasUserProvidedValue('constructor')) {
  print('Custom constructor specified');
}

// Get all configured values
final allValues = annotation?.getAllFieldValues();

{@endtemplate}

Inheritance
Implemented types

Constructors

Annotation.declared(AnnotationDeclaration declaration, ProtectionDomain domain)
Creates an Annotation instance from reflection metadata.
factory

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

checkAccess(String operation, DomainPermission permission) → void
Verifies access permissions before performing sensitive operations.
inherited
getAllFieldValues() → Map<String, dynamic>
Gets all field values (both user-provided and defaults).
getAuthor() → Author?
Retrieves the Author annotation applied to this element, if present.
inherited
getClass() → Class
getDeclaration() → AnnotationDeclaration
Gets the declaration metadata associated with this annotation.
getDeclaringClass() → Class
Gets the class metadata of the annotation type.
getDefaultValue(String fieldName) → dynamic
Gets the default value of a field.
getField(String name) → Field?
Gets a field by its name.
inherited
getFieldNames() → List<String>
Gets all field names declared on this annotation.
getFields() → Iterable<Field>
Gets all fields declared in this class.
inherited
getFieldValue(String fieldName) → dynamic
Gets the value of a field by name.
getFieldValueAs<T>(String fieldName) → T?
Gets a field value with type conversion.
getInstance<Instance>() → Instance
Attempts to retrieve the actual annotation instance.
getProtectionDomain() → ProtectionDomain
Gets the protection domain governing access to this element.
inherited
getSignature() → String
Gets the annotation's signature string.
getType() → Type
Gets the runtime type of the annotation.
getUserProvidedValue(String fieldName) → dynamic
Gets the user-provided value of a field.
getUserProvidedValues() → Map<String, dynamic>
Gets all user-provided values as a map.
getVersion() → Version
Retrieves the version metadata associated with this declaration, if any.
inherited
hasDefaultValue(String fieldName) → bool
Checks if a field has a default value.
hasField(String fieldName) → bool
Checks if a field exists on this annotation.
hasUserProvidedValue(String fieldName) → bool
Checks if a field has a user-provided value.
matches<A>([Class<A>? type]) → bool
Checks whether this annotation instance matches the type parameter A.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

clearInstanceFactories() → void
Clears or resets all registered instance factories (useful for test isolation).
registerInstanceFactory(Object? factory(String name, Annotation annotation)) → void
Registers a factory function that creates an annotation instance from its name and reflection metadata.