Class<T> class abstract final

Provides a unified, reflective interface to access Dart class types and their associated metadata at runtime.

The Class interface abstracts the complexities of runtime reflection and static analysis, allowing developers to:

  • Inspect class type information and hierarchy
  • Access constructors and create instances dynamically
  • Inspect and modify fields and methods
  • Read annotations and metadata
  • Resolve and manipulate generic type parameters

This interface is central to runtime introspection frameworks like JetLeaf, where classes must be analyzed, invoked, or transformed dynamically.

Key Features

  1. Type-Safe Reflection Access class members and hierarchy without unsafe casts.

  2. Generic Type Support Resolve type arguments and work with parameterized classes.

  3. Class Hierarchy Navigation Inspect superclasses, subclasses, interfaces, and mixins.

  4. Instance Creation Construct objects dynamically using resolved constructors.

  5. Member Inspection Read or invoke fields and methods at runtime.

  6. Annotation Access Discover metadata attached to classes, fields, and methods.

Implementation Notes

  • Concrete implementations wrap platform-specific reflection objects (e.g., dart:mirrors, analyzer elements, or runtime descriptors).
  • Provides a consistent, cross-platform interface for introspection and code generation pipelines.

Example Usage

// Obtain reflective metadata for the User class
final userClass = Class.forType<User>();

// Create a new instance using a constructor with named parameters
final user = userClass.newInstance({'name': 'Alice', 'age': 30});

// Access a field dynamically
final nameField = userClass.getField('name');
print(nameField?.get(user)); // Output: 'Alice'

// Update field dynamically
nameField?.set(user, 'Bob');
print(nameField?.get(user)); // Output: 'Bob'

// Check type hierarchy
if (userClass.isSubclassOf(Class.forType<Person>())) {
  print('User is a subclass of Person');
}

// Iterate over all methods
for (final method in userClass.getAllMethods()) {
  print(method.name);
}

// Access generic type parameters
final genericArgs = userClass.getAllGenericArguments();
for (final arg in genericArgs) {
  print(arg.getName());
}

// Discover annotations
final serializable = userClass.getAnnotation<Serializable>();
if (serializable != null) {
  print('User class is serializable');
}

Type Parameters:

  • T: The concrete Dart type that this Class instance represents.
Inheritance
Implemented types
Annotations

Constructors

Class([ProtectionDomain? domain, String? package, LinkDeclaration? link])
Provides a unified, reflective interface to access Dart class types and their associated metadata at runtime.
factory
Class.declared(ClassDeclaration declaration, ProtectionDomain domain)
Creates a Class instance from type declaration metadata.
factory
Class.fromQualifiedName(String qualifiedName, [ProtectionDomain? domain, LinkDeclaration? link])
Creates a Class instance from a fully qualified name.
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
componentType<C>() Class<C>?
Retrieves the element or value type (C) of a collection-like or container-like generic structure.
inherited
equalizedProperties() List<Object?>
Mixin-style contract for value-based equality, hashCode, and toString.
inherited
getAllAnnotations() Iterable<Annotation>
Returns:
getAllDirectAnnotations() Iterable<Annotation>
Gets all annotations applied to this element.
inherited
getAllInterfaceArguments() Iterable<Class>
Returns the generic type arguments from all implemented interfaces.
getAllInterfaces() Iterable<Class>
Returns all interfaces implemented by this class, including those inherited through superclasses.
getAllMethodsInHierarchy() Iterable<Method>
Gets all methods declared in this class and its hierarchy.
getAllMixinConstraintArguments() Iterable<Class>
Returns the generic type arguments from all declared mixin constraints.
getAllMixinConstraints() Iterable<Class>
Returns all mixin constraints applied to this mixin.
getAllMixins() Iterable<Class>
Returns all mixins applied to this class or mixin declaration.
getAllMixinsArguments() Iterable<Class>
Returns the generic type arguments from all applied mixins.
getAnnotation<A>() → A?
Gets a single annotation by type, if present.
getAnnotations<A>() Iterable<A>
Gets all annotations of a specific type.
getAuthor() Author?
Retrieves the Author annotation applied to this element, if present.
inherited
getBestConstructor(List<Class> paramTypes) Constructor?
Gets the best constructor for the given parameter types.
getCanonicalName() String
Gets the canonical name with complete generic type information.
getClassDeclaration() ClassDeclaration
Gets the type declaration of this class.
getConstructor(String name) Constructor?
Gets a constructor by its declared name.
getConstructorBySignature(List<Class> paramTypes) Constructor?
Gets a constructor by its parameter types.
getConstructors() Iterable<Constructor>
Gets all constructors declared in this class.
getDeclaration() Declaration
Gets the type declaration metadata for this class.
override
getDeclaredMembers() Iterable<Member>
Gets all members (fields, methods, constructors) declared in this class.
getDefaultConstructor() Constructor?
Gets the default unnamed constructor for this class.
getDirectAnnotation<A>() → A?
Gets a single annotation by type, if present.
inherited
getDirectAnnotations<A>() Iterable<A>
Gets all annotations of a specific type.
inherited
getEnumValues() Iterable<EnumValue>
Gets all enum values if this is an enum class.
getField(String name) Field?
Gets a field by its name.
inherited
getFields() Iterable<Field>
Gets all fields declared in this class.
inherited
getInterface<I>() Class<I>?
Returns the implemented interface that exactly matches type I, if present.
getInterfaceArguments<I>() Iterable<Class>
Returns the generic type arguments used when implementing the interface of type I.
getInterfaces<I>() Iterable<Class<I>>
Returns all implemented interfaces that match the given interface type I.
getIsNullable() bool
Indicates whether the function type itself is nullable.
getMethod(String name) Method?
Gets a method by its name.
getMethodBySignature(String name, List<Class> parameterTypes) Method?
Gets a method by its name and parameter types.
getMethodCall() Method?
Returns the reflective call() method if the function originates from a callable object.
getMethods() Iterable<Method>
Gets all methods declared in this class.
getMethodsByName(String name) Iterable<Method>
Gets all methods with a specific name.
getMixin<I>() Class<I>?
Returns the mixin that exactly matches type I, if present.
getMixinConstraint<I>() Class<I>?
Returns the mixin constraint that exactly matches type I, if present.
getMixinConstraintArguments<I>() Iterable<Class>
Returns the generic type arguments used by the mixin constraint I.
getMixinConstraints<I>() Iterable<Class<I>>
Returns all mixin constraints that match the given constraint type I.
getMixins<I>() Iterable<Class<I>>
Returns all applied mixins that match the given mixin type I.
getMixinsArguments<M>() Iterable<Class>
Returns the generic type arguments supplied to the applied mixin M.
getModifiers() List<String>
Returns the list of modifiers associated with this source element.
inherited
getName() String
Gets the declared name of the source.
inherited
getNoArgConstructor([bool acceptWhenAllParametersAreOptional = false]) Constructor?
Retrieves the no-argument constructor of this Constructor instance, if available.
getOriginal() Type
Returns the original, JetLeaf-resolved type requested by this Class.
getOverriddenMethods() Iterable<Method>
Gets all methods that are overridden in this class.
getPackage() Package
Retrieves the package metadata object containing this class.
getPackageUri() String
Retrieves the package URI where this class is defined.
getParameters() Iterable<Class<Object>>
Returns the ordered list of parameter types accepted by the function.
getProtectionDomain() ProtectionDomain
Gets the protection domain governing access to this element.
inherited
getQualifiedName() String
Returns the fully-qualified identifier for this object.
inherited
getRecordField(Object id) RecordField?
Retrieves a single record field by its identifier.
getRecordFields() Iterable<RecordField>
Returns a list of all fields in this record.
getReturnType() Class<Object>
Returns the declared return type of the function signature.
getSignature() String
Gets the executable signature as a string.
inherited
getSimpleName() String
Gets the simple class name without package or generic information.
getSubClass<S>() Class<S>?
Returns a specific direct subclass that matches type S, if present.
getSubClasses() Iterable<Class>
Returns all known direct subclasses of this class within the active reflection runtime.
getSuperClass<S>() Class<S>?
Returns the direct superclass of this class, if one exists.
getSuperClassArguments() Iterable<Class>
Returns the generic type arguments supplied to the superclass.
getType() Type
Returns the runtime-resolved Dart type represented by this Class.
Returns metadata links describing how generic type arguments are bound.
getTypeArguments() Iterable<Class<Object>>
Returns the concrete generic type arguments declared on this class.
getTypeParameters() Iterable<Class>
Returns all declared type parameters of this class in the order they appear in the class declaration.
inherited
getVersion() Version
Retrieves the version metadata associated with this declaration, if any.
inherited
hasAnnotation<A>() bool
Checks if this element has a specific annotation.
hasDirectAnnotation<A>() bool
Checks if this element has a specific annotation.
inherited
hasGenerics() bool
Checks if this class has generic type parameters.
inherited
isAbstract() bool
Checks whether this class is declared as abstract.
isArray() bool
Checks whether this class represents an array-like type.
isAssignableFrom(Class other) bool
Checks type assignability from another class.
isAssignableTo(Class other) bool
Checks if this type is assignable to another type.
isAsync() bool
Determines whether this method is asynchronous — i.e., declared using the async keyword or returning a Future-like type.
isBase() bool
Checks whether this class is declared as a base class.
isCanonical() bool
Checks if the display name matches the canonical name.
isClass() bool
Checks if this class represents a standard class type.
isClosure() bool
Checks whether this declaration represents a closure-backed class.
isDynamic() bool
Checks whether this class represents the dynamic type.
isEnum() bool
Checks whether this class represents an enum declaration.
isFinal() bool
Checks whether this class is declared as final.
isFunction() bool
Checks whether this class represents a function type.
isInstance(Object? obj) bool
Checks whether the given obj is an instance of this class at runtime.
isInterface() bool
Checks whether this class represents an interface.
isInvokable() bool
Checks whether this class is invokable.
isKeyValuePaired() bool
Checks if this class represents a key-value pair type.
inherited
isMixin() bool
Checks whether this class represents a mixin declaration.
isPrimitive() bool
Checks whether this class represents a primitive type.
isPublic() bool
Checks if this element is public.
inherited
isRecord() bool
Checks whether this class represents a record type.
isSealed() bool
Checks whether this class is declared as sealed.
isSubclassOf(Class other) bool
Determines whether this class is a subclass of other.
isSynthetic() bool
Indicates whether this class is synthetic (framework-generated) rather than directly declared in user source code.
isVoid() bool
Checks whether this class represents the void type.
keyType<K>() Class<K>?
Retrieves the key type (K) of a map-like or pair-like generic structure.
inherited
newInstance([Map<String, dynamic>? arguments, String? constructorName]) → T
Creates an instance using the default constructor.
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

forName<C>(String name, [ProtectionDomain? domain, String? package, LinkDeclaration? link]) Class<C>
Creates a Class instance for a runtime type name.
forObject(Object obj, [ProtectionDomain? domain, String? package, LinkDeclaration? link]) Class<Object>
Creates a Class instance for an object's runtime type.
forType<C>(C type, [ProtectionDomain? domain, String? package, LinkDeclaration? link]) Class<C>
Creates a Class instance for a runtime type.

Constants

GARBAGE_KEY → const String
The garbage key identifier used by Class in GC to store cache and perform any other actions in the garbage collector