platform/packages/container/angel_container/lib/src/throwing.dart
Tobe O 9c36a7e981 Add 'packages/container/' from commit 'a7eb96a391cced0f9b2d6a9fbaffc3483c2558eb'
git-subtree-dir: packages/container
git-subtree-mainline: dd33154af1
git-subtree-split: a7eb96a391
2020-02-15 18:22:26 -05:00

34 lines
1.2 KiB
Dart

import 'package:angel_container/angel_container.dart';
/// A [Reflector] implementation that throws exceptions on all attempts
/// to perform reflection.
///
/// Use this in contexts where you know you won't need any reflective capabilities.
class ThrowingReflector extends Reflector {
/// The error message to give the end user when an [UnsupportedError] is thrown.
final String errorMessage;
static const String defaultErrorMessage =
'You attempted to perform a reflective action, but you are using `ThrowingReflector`, '
'a class which disables reflection. Consider using the `MirrorsReflector` '
'class if you need reflection.';
const ThrowingReflector({this.errorMessage = defaultErrorMessage});
@override
String getName(Symbol symbol) => const EmptyReflector().getName(symbol);
UnsupportedError _error() => UnsupportedError(errorMessage);
@override
ReflectedClass reflectClass(Type clazz) => throw _error();
@override
ReflectedInstance reflectInstance(Object object) => throw _error();
@override
ReflectedType reflectType(Type type) => throw _error();
@override
ReflectedFunction reflectFunction(Function function) => throw _error();
}