← Reflection
Publishedrole: foundationlicense: BSD-3-Clause

tom_reflection

tom_reflection · v1.0.2

Runtime reflection support for Dart based on code generation, using capabilities to select which operations to support. It exposes class structure and metadata and invokes methods and properties by name.

View repository → See License
Status
Published
LOC
3.0k
Tests
0
Test LOC
0

Overview

`tom_reflection` provides runtime introspection and dynamic invocation for Dart. Code marked with a reflector annotation can be examined at runtime — its members, types, and metadata — and its methods, constructors, and properties can be called by name. Reflection support is opt-in through capabilities, so a program includes only the operations it needs and keeps generated code small. The mirror data itself is produced ahead of time rather than at runtime; in Tom that generation is handled by `tom_reflection_generator`. It is a fork of the `reflectable` package adapted to the Tom framework. As the base technology for runtime reflection, it underpins the auto-assembly of Tom applications.

What it enables

Enables Runtime introspection, Dynamic method invocation, Application auto-assembly.

Relationships

Standalone — no declared relationships.

Tom Reflection

Runtime reflection support for Dart based on code generation, using *capabilities* to specify which operations to support.

This package is a fork of the [reflectable](https://pub.dev/packages/reflectable) package with modifications to better suit the Tom framework's needs and bug fixes.

Features

  • **Introspection**: Examine object structure at runtime (class members, types, metadata)
  • **Dynamic invocation**: Call methods and access properties by name
  • **Capability-based**: Only include the reflection support you need, minimizing code size
  • **Code generation**: Generates efficient reflection data at build time

Getting Started

Prerequisites

  • Dart SDK ^3.9.0
  • `tom_build_tools` package for code generation

Installation

Add to your `pubspec.yaml`:

dependencies:
  tom_reflection: ^1.0.0

dev_dependencies:
  build_runner: ^2.4.0
  tom_build:
    path: ../tom_build  # or git/pub reference
  tom_build_tools:
    path: ../tom_build_tools  # or git/pub reference

Add a `build.yaml` to configure which files to generate reflection for:

targets:
  $default:
    builders:
      tom_build:reflection_generator:
        generate_for:
          - lib/main.dart
          - example/*.dart
        options:
          formatted: true

Usage

1. Define a Reflector

Create a class extending `Reflection` with the capabilities you need:

import 'package:tom_reflection/tom_reflection.dart';

class MyReflector extends Reflection {
  const MyReflector()
      : super(
          invokingCapability,
          declarationsCapability,
        );
}

const myReflector = MyReflector();

2. Annotate Classes

Mark classes for reflection with your reflector:

@myReflector
class Person {
  final String name;
  final int age;

  Person(this.name, this.age);

  String greet() => 'Hello, I am $name!';
}

3. Generate Reflection Data

Run build_runner to generate the reflection code:

dart run build_runner build

Or use the standalone generator directly:

dart run tom_build_tools:reflection_generator lib/main.dart

4. Use Reflection

import 'main.reflection.dart';

void main() {
  initializeReflection();

  final person = Person('Alice', 30);
  final mirror = myReflector.reflect(person);

  // Get member names
  final classMirror = mirror.type;
  print('Class: ${classMirror.simpleName}');

  // Invoke method dynamically
  final greeting = mirror.invoke('greet', []);
  print(greeting); // Hello, I am Alice!
}

Capabilities

Capabilities control what reflection operations are available:

CapabilityDescription
`invokingCapability`Invoke methods and constructors
`declarationsCapability`Access class declarations
`typeRelationsCapability`Access superclass/interface info
`metadataCapability`Access metadata annotations
`reflectedTypeCapability`Access `reflectedType` on mirrors

License

BSD 3-Clause License. See [LICENSE](LICENSE) for details.

This package is a fork of [reflectable](https://pub.dev/packages/reflectable) and maintains the same open-source license.

Additional Information

  • Part of the [Tom Framework](https://github.com/al-the-bear/tom)
  • Based on [reflectable](https://pub.dev/packages/reflectable)
  • See [example/](example/) for complete examples
License
Copyright (c) 2015, Dart
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of reflectable nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Refactoring, modifications and enhancements (c) 2026, Peter Nicolai Alexis Kyaw