2024-12-16 01:34:28 +00:00
# Platform Reflection
2024-12-21 18:01:22 +00:00
A powerful cross-platform reflection system for Dart that serves as a drop-in replacement for `dart:mirrors` . This implementation offers a carefully balanced approach between functionality and performance, providing reflection capabilities with AOT compilation support.
2024-12-16 01:34:28 +00:00
## Table of Contents
- [Features ](#features )
- [Installation ](#installation )
2024-12-21 18:01:22 +00:00
- [Usage ](#usage )
2024-12-16 01:34:28 +00:00
- [API Reference ](#api-reference )
2024-12-21 18:01:22 +00:00
- [Performance Considerations ](#performance-considerations )
2024-12-16 01:34:28 +00:00
- [Limitations ](#limitations )
2024-12-21 18:01:22 +00:00
- [Migration from dart:mirrors ](#migration-from-dartmirrors )
2024-12-16 01:34:28 +00:00
- [Contributing ](#contributing )
- [License ](#license )
## Features
- ✅ Platform independent reflection system
2024-12-21 18:01:22 +00:00
- ✅ AOT compilation support
2024-12-16 01:34:28 +00:00
- ✅ No dependency on `dart:mirrors`
- ✅ Class reflection with inheritance
- ✅ Method invocation with named parameters
2024-12-21 18:01:22 +00:00
- ✅ Property access and mutation
2024-12-16 01:34:28 +00:00
- ✅ Constructor resolution and invocation
- ✅ Type introspection and relationships
2024-12-21 18:01:22 +00:00
- ✅ Explicit registration for performance
- ✅ Basic generic type support
- ✅ Comprehensive error handling
2024-12-16 01:34:28 +00:00
## Installation
2024-12-21 18:01:22 +00:00
Add this to your package's `pubspec.yaml` file:
2024-12-16 01:34:28 +00:00
```yaml
dependencies:
platform_reflection: ^0.1.0
```
2024-12-21 18:01:22 +00:00
## Usage
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
### Registration
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
Unlike `dart:mirrors` , Platform Reflection requires explicit registration of classes and their members. This enables AOT compilation support and fine-grained control over reflection.
2024-12-16 01:34:28 +00:00
```dart
@reflectable
class User {
String name;
int age;
final String id;
2024-12-21 18:01:22 +00:00
List< String > tags;
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
User(this.name, this.age, {required this.id, List< String > ? tags})
: tags = tags ?? [];
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
String greet() => "Hi $name!";
void addTag(String tag) {
tags.add(tag);
2024-12-16 01:34:28 +00:00
}
2024-12-21 18:01:22 +00:00
String getName() => name;
2024-12-16 01:34:28 +00:00
}
void registerUser() {
Reflector.register(User);
Reflector.registerProperty(User, 'name', String);
Reflector.registerProperty(User, 'age', int);
Reflector.registerProperty(User, 'id', String, isWritable: false);
2024-12-21 18:01:22 +00:00
Reflector.registerProperty(User, 'tags', List< String > , isWritable: false);
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
Reflector.registerMethod(User, 'greet', [], false);
Reflector.registerMethod(User, 'addTag', [String], true);
Reflector.registerMethod(User, 'getName', [], false);
2024-12-16 01:34:28 +00:00
Reflector.registerConstructor(
User,
'',
2024-12-21 18:01:22 +00:00
parameterTypes: [String, int, String, List< String > ],
parameterNames: ['name', 'age', 'id', 'tags'],
isRequired: [true, true, true, false],
isNamed: [false, false, true, true],
2024-12-16 01:34:28 +00:00
);
}
```
2024-12-21 18:01:22 +00:00
### Reflection Operations
2024-12-16 01:34:28 +00:00
```dart
2024-12-21 18:01:22 +00:00
void demonstrateReflection() {
2024-12-16 01:34:28 +00:00
final reflector = RuntimeReflector.instance;
// Create instance
final user = reflector.createInstance(
User,
2024-12-21 18:01:22 +00:00
positionalArgs: ['John Doe', 30],
namedArgs: {'id': 'user1', 'tags': ['admin', 'user']},
2024-12-16 01:34:28 +00:00
) as User;
// Get mirror
final mirror = reflector.reflect(user);
// Property access
final name = mirror.getField(const Symbol('name')).reflectee as String;
// Property modification
2024-12-21 18:01:22 +00:00
mirror.setField(const Symbol('age'), 25);
2024-12-16 01:34:28 +00:00
// Method invocation
2024-12-21 18:01:22 +00:00
final greeting = mirror.invoke(const Symbol('greet'), []).reflectee as String;
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
// Type information
final classMirror = reflector.reflectClass(User);
print('Type name: ${classMirror.name}');
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
// Type relationships
final entityType = reflector.reflectType(Entity);
print('User is subtype of Entity: ${classMirror.isSubclassOf(entityType)}');
2024-12-16 01:34:28 +00:00
}
```
## API Reference
### Core Classes
2024-12-21 18:01:22 +00:00
- `Reflector` : Static class for registration and metadata management
2024-12-16 01:34:28 +00:00
- `RuntimeReflector` : Runtime reflection operations
### Mirrors
- `InstanceMirror` : Instance reflection
- `ClassMirror` : Class reflection
- `MethodMirror` : Method reflection
- `LibraryMirror` : Library reflection
- `TypeMirror` : Type reflection
### Metadata
- `TypeMetadata` : Type information
- `PropertyMetadata` : Property information
- `MethodMetadata` : Method information
- `ConstructorMetadata` : Constructor information
### Exceptions
2024-12-21 18:01:22 +00:00
- `ReflectionException` : Base exception for reflection errors
- `NotReflectableException` : Thrown when attempting to reflect on an unregistered type
## Performance Considerations
- Explicit registration adds startup cost but improves runtime performance
- Cached class mirrors for efficient repeated use
- Minimal metadata storage for reduced memory footprint
- AOT compilation support allows for better optimization
2024-12-16 01:34:28 +00:00
## Limitations
2024-12-21 18:01:22 +00:00
- Requires explicit registration of reflectable types and members
- Limited support for complex generic types
- No support for extension methods
- No cross-package private member access
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
## Migration from dart:mirrors
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
To migrate from `dart:mirrors` to Platform Reflection:
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
1. Replace `import 'dart:mirrors'` with `import 'package:platform_reflection/mirrors.dart'` .
2. Add `@reflectable` annotation to classes you want to reflect on.
3. Implement a registration function for each reflectable class.
4. Call registration functions at the start of your application.
5. Replace `MirrorSystem.reflect()` with `RuntimeReflector.instance.reflect()` .
6. Update any code that relies on automatic discovery of classes or members.
2024-12-16 01:34:28 +00:00
## Contributing
2024-12-21 18:01:22 +00:00
Contributions are welcome! Please see our [Contributing Guidelines ](CONTRIBUTING.md ) for more details.
2024-12-16 01:34:28 +00:00
## License
2024-12-21 18:01:22 +00:00
This project is licensed under the MIT License - see the [LICENSE ](LICENSE ) file for details.