platform/packages/mirrors/README.md

182 lines
5.2 KiB
Markdown
Raw Permalink Normal View History

# 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.
## Table of Contents
- [Features](#features)
- [Installation](#installation)
2024-12-21 18:01:22 +00:00
- [Usage](#usage)
- [API Reference](#api-reference)
2024-12-21 18:01:22 +00:00
- [Performance Considerations](#performance-considerations)
- [Limitations](#limitations)
2024-12-21 18:01:22 +00:00
- [Migration from dart:mirrors](#migration-from-dartmirrors)
- [Contributing](#contributing)
- [License](#license)
## Features
- ✅ Platform independent reflection system
2024-12-21 18:01:22 +00:00
- ✅ AOT compilation support
- ✅ 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
- ✅ 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
## Installation
2024-12-21 18:01:22 +00:00
Add this to your package's `pubspec.yaml` file:
```yaml
dependencies:
platform_reflection: ^0.1.0
```
2024-12-21 18:01:22 +00:00
## Usage
2024-12-21 18:01:22 +00:00
### Registration
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.
```dart
@reflectable
class User {
String name;
int age;
final String id;
2024-12-21 18:01:22 +00:00
List<String> tags;
2024-12-21 18:01:22 +00:00
User(this.name, this.age, {required this.id, List<String>? tags})
: tags = tags ?? [];
2024-12-21 18:01:22 +00:00
String greet() => "Hi $name!";
void addTag(String tag) {
tags.add(tag);
}
2024-12-21 18:01:22 +00:00
String getName() => name;
}
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-21 18:01:22 +00:00
Reflector.registerMethod(User, 'greet', [], false);
Reflector.registerMethod(User, 'addTag', [String], true);
Reflector.registerMethod(User, 'getName', [], false);
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-21 18:01:22 +00:00
### Reflection Operations
```dart
2024-12-21 18:01:22 +00:00
void demonstrateReflection() {
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']},
) 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);
// Method invocation
2024-12-21 18:01:22 +00:00
final greeting = mirror.invoke(const Symbol('greet'), []).reflectee as String;
2024-12-21 18:01:22 +00:00
// Type information
final classMirror = reflector.reflectClass(User);
print('Type name: ${classMirror.name}');
2024-12-21 18:01:22 +00:00
// Type relationships
final entityType = reflector.reflectType(Entity);
print('User is subtype of Entity: ${classMirror.isSubclassOf(entityType)}');
}
```
## API Reference
### Core Classes
2024-12-21 18:01:22 +00:00
- `Reflector`: Static class for registration and metadata management
- `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
## 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-21 18:01:22 +00:00
## Migration from dart:mirrors
2024-12-21 18:01:22 +00:00
To migrate from `dart:mirrors` to Platform Reflection:
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.
## Contributing
2024-12-21 18:01:22 +00:00
Contributions are welcome! Please see our [Contributing Guidelines](CONTRIBUTING.md) for more details.
## License
2024-12-21 18:01:22 +00:00
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.